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:
 
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?
Why are we only hearing about this now? If you really want us to help you then don't you think we should have all the relevant information from the word go? Please, in future take the time and make the effort to provide a proper, clear description of exactly what you want in the first post. You'll save us and yourself a lot of time.
 
I wasn't interested in being too specific concerning which way my graphics should be animated, it is a clear post without all that you mention.

There is something callled imagination, really it ain't hard to do something that someone has asked, now you can just say well your not interested go elsewhere, that would be the backfiring of what I'm saying.

"Animate piece of gfx"

Code,

intuition.

No offence people.

All I'm saying in the end, is that just to animate this it looked so easy in the end, and perhaps had I thought about it or had the right docs around.

As for 2003 WM? I don't suppose anyone could port some code for me?
 
Could someone actually give me a fully analysis on that piece of code please? As best you can.

"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"

Now, urm private means that you are creating or declaring something of itself? Like um original?

Thank you.
 
Anyone at all?

Why couldn't the code in my first post just move via key simply?
 
Why couldn't the code in my first post just move via key simply?

Just move the code I've given you to the appropriate place. Handle the form's KeyDown event instead of a Button's Click event.

VB.NET:
    Private rect As New Rectangle(0, 0, 25, 50)

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Dim oldRect As Rectangle = Me.rect

        Select Case e.KeyCode
            Case Keys.Up
                Me.rect.Offset(0, -5)
            Case Keys.Down
                Me.rect.Offset(0, 5)
            Case Keys.Left
                Me.rect.Offset(-5, 0)
            Case Keys.Right
                Me.rect.Offset(5, 0)
        End Select

        Dim newRect As Rectangle = Me.rect

        oldRect.Inflate(1, 1)
        newRect.Inflate(1, 1)

        Me.Invalidate(oldRect)
        Me.Invalidate(newRect)

        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
 
Yes and?

How do I create another one? I did try and use the same code, just called it rectangle 2? That didn't work. And also renamed all the other bits to "rect2".
 

And what? Both I and jmcilhinney answered your question. You asked for a solution to move the rectangle using the keys - jmcilhinney explained how to do it, and I posted the exact solution.

How do I create another one? I did try and use the same code, just called it rectangle 2? That didn't work. And also renamed all the other bits to "rect2".

We arent mind readers - post the code you used and we'll look at it.
 
You know the same code? That "Jmcilhinney" posted

In your posted code above you have all this? I've only got one.
 
"""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 Rectangle2 = Me.rect2

'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" "

"Dim oldRect As Rectangle2 = Me.rect2"

As an example if you rename that and all the rest it should make another one? And then just press a different key for it to move?
 
Back
Top