![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 4
| Trouble With Graphics And Picture Boxes 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();
}
}
}
|
| ThePermster is offline | |
| | #2 |
| Registered User Join Date: Jun 2008 Location: RING 0
Posts: 468
| You usually don't draw to a picturebox through a DC, but just supply it a bitmap as it does a lot of special optimizations to help you out. Also do you set a breakpoint in the if (Pile.pile[i, j] != null) block? Do you actually get here? The reason I ask is I see you set every pile to null, and never set it to anything else. So unless you are not showing all the code, nothing will be drawn. |
| valaris is offline | |
| | #3 |
| Registered User Join Date: Jul 2009
Posts: 4
| What's a DC? Pardon my ignorance, like I said I'm fairly new to this and this is my first somewhat large endeavour. I managed to fix my problem though just by adding a call to Clear() in the paint() method but my question wasn't very clear anyhoo given the nature of the code segment. In future I shan't post whilst tired. x] Thanks for the help anyhoo though. |
| ThePermster is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Graphics in C# | mithrandir_hr | C# Programming | 5 | 05-15-2009 11:19 AM |
| Having some real trouble with this code for a game I'm working on. | Swarvy | Game Programming | 3 | 05-09-2009 08:37 PM |
| brought over from the c++ board... windows graphics help. | uvacow | Windows Programming | 1 | 08-11-2003 07:02 PM |
| hope this is the right forum... mfc graphics question | uvacow | C++ Programming | 6 | 08-11-2003 04:11 PM |
| Borland Graphics | drdroid | C++ Programming | 1 | 04-13-2002 01:43 PM |