C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-30-2009, 07:26 PM   #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. =]
ThePermster is offline   Reply With Quote
Old 07-31-2009, 12:05 AM   #2
Registered User
 
valaris's Avatar
 
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   Reply With Quote
Old 07-31-2009, 07:37 AM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:29 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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