I'm having trouble getting what I draw to a graphics object on a picturebox to appear like it should. Currently the form opens and everything appears rightly laid out but the Clear() method and any drawing methods afterwards don't seem to be producing any response.
The picture boxes just remain the same grey colour as the window that contains them.
My understanding of some OOP concepts are still solidifying at the moment so I have a suspicion it's something to do with how I've used a thread or a misunderstanding on how exactly the visual studio desiigner puts together things for me. I think anyhoo.
This is the MainWindow class of my code, hope it's not too much code to have to sift through but I'm not sure which parts are responsible:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace ZortlisWin
{
public partial class MainWindow : Form
{
private Graphics grphPicBoxGameArea, grphPicBoxNextBlock;
private Block blockinuse;
private Thread dropthread;
private int timeinterval, currentlevel, currentscore = 0;
public MainWindow()
{
InitializeComponent();
initiate();
}
private void initiate()
{
//Setup window components according to specified game area size from options
int gameareawidthmulti = Options.getGameAreaWidth() * 30,
gameareaheightmulti = Options.getGameAreaHeight() * 30;
picboxGameArea.Size = new Size(gameareawidthmulti, gameareaheightmulti);
picboxNextBlock.Location = new Point(gameareawidthmulti + 60, 50);
nextLabel.Location = new Point(gameareawidthmulti + 60, 20);
ClientSize = new Size(gameareawidthmulti + 220, gameareaheightmulti + 70);
//Prepare picture boxes, POSSIBLE PROBLEM AREA?
grphPicBoxGameArea = picboxGameArea.CreateGraphics();
grphPicBoxGameArea.Clear(Color.White);
grphPicBoxNextBlock = picboxNextBlock.CreateGraphics();
grphPicBoxNextBlock.Clear(Color.White);
blockinuse = new Block();
//Make pile and clear it
Pile.pile = new Square[Options.getGameAreaWidth(), Options.getGameAreaHeight() + 3];
for (int i = 0; i < Pile.pile.GetLength(0); i++) {
for (int j = 0; j < Pile.pile.GetLength(1); j++) {
Pile.pile[i, j] = null;
}
}
//Starts up the falling time thread and initialises it's time interval and level
dropthread = new Thread(timeflow);
timeinterval = 2200 - (Options.getStartLevel() * 200);
currentlevel = Options.getStartLevel();
dropthread.Start();
}
private void timeflow()
{
while (true)
{
bool blockdropped = blockinuse.dropBlock();
if (!blockdropped) blockinuse = Pile.settleBlock(blockinuse);
if (blockinuse == null) break;
Pile.checkCompleteLines();
if (Pile.checkGameOver()) break;
paint();
Thread.Sleep(TimeSpan.FromMilliseconds(timeinterval));
}
}
public void paint()
{
for (int i = 0; i < Pile.pile.GetLength(0); i++)
{
for (int j = 0; j < Pile.pile.GetLength(1) - 3; j++)
{
if (Pile.pile[i, j] != null)
{
//POSSIBLE PROBLEM DRAWING TO GRAPHICS?
SolidBrush squarepen = new SolidBrush(Pile.pile[i, j].getColour());
grphPicBoxGameArea.FillRectangle(squarepen, i * 30, (Options.getGameAreaHeight() * 30) - (j * 30), 30, 30);
}
}
}
Console.WriteLine("Am painting!");
}
private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
{
if (dropthread.IsAlive) dropthread.Abort();
}
}
}
I've tried to comment the parts that actually use the graphics to be noticeable, hope that makes it a bit easier to notice. Any help would be appreciated. Sorry if it's a really stupid fix too, if you haven't already noticed I'm still learning so your patience will be appreciated. =]