Thread: Graphics does not draw over the whole screen

  1. #16
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Look at the code I've added:
    Code:
            private void newToolStripMenuItem_Click(object sender, EventArgs e)
            {
                drawSnake = true;
                ClearPlayground();
                InitializeGame();
                timer1.Start();
            }
    
            private void FormGame_Paint(object sender, PaintEventArgs e)
            {
                if (drawSnake)
                    snake.Draw();
            }
    It's not drawing the snake.
    gavra.

  2. #17
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Ok, Im going to assume you connected the FormGame_Paint to the Paint event?

    Now what you do is you connect a handler to the timer1.Tick event. If you dont have that event change the type of timer1 to be System.Windows.Forms.Timer (important). Set a good interval (in milliseconds) before you call Start aswell (this will decide how fast the snake is going to move).

    Now in the timer1.Tick handler you set up there you simply make a call to Invalidate();

    Hopefully this will get your snake moving

  3. #18
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    I'm not sure about what's to connect methods.
    Could you explain?
    gavra.

  4. #19
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Ok to connect a handler to say the timer1.Tick event you do this:
    Code:
    // This is the handler we want to attack
    private void TimerEventProcessor(Object myObject,
                                                EventArgs myEventArgs) {
           Invalidate();
    }
    
    private EventHandler timerEventHandler;
    
    // This goes into constructor of class
    timerEventHandler = new EventHandler(TimerEventProcessor);
    
    // And here we attach it:
    timer1.Tick += timerEventHandler;
    The same approach goes for Paint.

  5. #20
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    The designer of the form does it:

    Code:
    private System.Windows.Forms.Timer timer1;
    this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
    Let's not talk about the moving, just drawing the snake once without any movement.
    I put the snake's draw method in the Paint method (which calls when the form need repainting automatically) and in certain locations of the form on the screen it doesn't draw it.
    I used the OnPaint thing as you all said and it still not working.
    gavra.

  6. #21
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    We are going to have to see the drawing code then if it still doesn't work.

  7. #22
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    This is 'event driven' programming, code only is called after a message or event (ie user input, time span, OS, network etc).
    [very different from 'structured' where the code executes line after line, until it exits.]

    In the background the .NET framework is handling all the events (in a default manner).

    To make .NET call your new code instead you need to 'link' your method to the event (done in the designer or by hand).

    You then have to send messages or create 'events' to make the code actually run (or respond to events the framework or OS sends automatically).

    Quote Originally Posted by gavra View Post
    The designer of the form does it:
    Did it create a similar one for your FormGame_Paint() method? (to link the OnPaint event to the FormGame_Paint code)

    Quote Originally Posted by gavra View Post
    FormGame_PaintLet's not talk about the moving, just drawing the snake once without any movement.
    Did you send a paint event? (to make the drawing code actually run)

    Code:
    //something like??
    this.Invalidate() //send paint event
    this.Update() //'do it now!'
    Last edited by novacain; 01-09-2011 at 01:02 AM. Reason: clarity
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #23
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Are you using e.Graphics to draw the snake or are you doing CreateGraphics() somewhere and using that Graphics object? You should definitely be using e.Graphics, which became available once you hooked into the Paint event.
    If you understand what you're doing, you're not learning anything.

  9. #24
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    I make the graphics object of the form in its constructor and pass its reference as I construct the snake. The snake object uses it to draw.
    Actually, I've solved the problem. I just set the RightToLeft property of the form to No and it fixed the problem.
    Now I suffer with flickering: A slow drawing problem
    gavra.

  10. #25
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Seeing your other tread you are drawing the snake, the background and the food in different methods.

    How do you ensure that it draws everything at one given time point?

    Try making it draw everything every 1 sec and see what happens.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  2. SDL segfault when trying to draw to screen
    By Blizzarddog in forum Game Programming
    Replies: 2
    Last Post: 12-09-2005, 07:53 AM
  3. draw function HELP!!!
    By sunoflight77 in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 11:28 PM
  4. saving screen to file
    By z0diac in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-23-2003, 07:00 PM
  5. Text on Graphics screen
    By bob5845 in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 09:08 PM