Animating a piece of graphics

myblueocean

Well-known member
Joined
Jun 29, 2007
Messages
161
Location
England
Programming Experience
Beginner
"'Draw a 200 pixel red rectangle 20 pixels from the top of the form.
e.Graphics.DrawRectangle(Pens.Red, 10, 20, 10, 25)
'fill the rectangle with a different colour.
e.Graphics.FillRectangle(Brushes.Red, 10, 20, 10, 25)"

:confused:
 
I think I may of got it. No problem for the moment.
 
I actually now don't know how to go about this? Any example?
 
I would like to animate that piece of code on a form.

I'm completely confused, I did check out John's link "Bob blah blah", about the GDI+, but nothing.
 
You don't animate a piece of code. What is important here is what you want the user to see. If we don't know that then how can we tell you how to achieve it? Do you want this rectangle to bounce up and down? move side to side? change colour? something else entirely?
 
I would to like to animate it right?
 
You appear unwilling to post more than a few words at a time. If you really want help then you will make the effort to post s proper, clear description of exactly what you want to happen. I for one am not prepared to guess.
 
I did check out John's link "Bob blah blah", about the GDI+, but nothing.

I've never done GDI programming before, but I've just been to Bob Powell's website as shown in JohnH's signature, and managed to animate your rectangle in 10 minutes.

I suggest you go and re-read this page http://www.bobpowell.net/animation.htm - everything you need to animate your rectangle is on that page.
 
I don't remember reading any of that on that page, I may of missed it.
 
What about moving that piece of graphics? with a press of a key?
 
Create a new project and add a Button. Now add this code:
VB.NET:
Private rect As New Rectangle(0, 0, 25, 50)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Get the area that was previously drawn in.
    Dim oldRect As Rectangle = Me.rect

    'Move the drawing area.
    Me.rect.Offset(5, 10)

    'Get the area that is now going to be drawin in.
    Dim newRect As Rectangle = Me.rect

    'Make each area for invalidation 1 pixel larger than the actual drawing area in every direction
    oldRect.Inflate(1, 1)
    newRect.Inflate(1, 1)

    'Notify the form that the old and new areas require repainting.
    Me.Invalidate(oldRect)
    Me.Invalidate(newRect)

    'Tell the form to repaint invalidated areas.
    Me.Update()
End Sub

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    With e.Graphics
        .FillRectangle(Brushes.Red, Me.rect)
        .DrawRectangle(Pens.Black, Me.rect)
    End With
End Sub
Run the project and press the Button a few times. Hopefully that should give you the idea. If you want to move the drawing without having to press a Button then you would most likely use a Timer.
 
Um, but that is using a button placed on the form? A key board key?
 
Alrighty, I've now done something, well truthfully you have.

Why isn't something like this posted up somewhere?

I mean I have been confused for a while now about all this, and probably still am. I wanted this for the pocket pc? Windows mobile 2003?
 
Back
Top