Trouble With Graphics And Picture Boxes

This is a discussion on Trouble With Graphics And Picture Boxes within the C# Programming forums, part of the General Programming Boards category; I'm having trouble getting what I draw to a graphics object on a picturebox to appear like it should. Currently ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    Trouble With Graphics And Picture Boxes

    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. =]

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    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.

  3. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Graphics in C#
    By mithrandir_hr in forum C# Programming
    Replies: 5
    Last Post: 05-15-2009, 11:19 AM
  2. Replies: 3
    Last Post: 05-09-2009, 08:37 PM
  3. brought over from the c++ board... windows graphics help.
    By uvacow in forum Windows Programming
    Replies: 1
    Last Post: 08-11-2003, 07:02 PM
  4. hope this is the right forum... mfc graphics question
    By uvacow in forum C++ Programming
    Replies: 6
    Last Post: 08-11-2003, 04:11 PM
  5. Borland Graphics
    By drdroid in forum C++ Programming
    Replies: 1
    Last Post: 04-13-2002, 01:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21