Results 1 to 12 of 12

Thread: Z-order when drawing graphics??

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Z-order when drawing graphics??

    Hello,

    I have a situation in which I will be moving painted graphics around on a form. Is there a way to change the z-order of painted graphics like you do controls?

    Just a sample..

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    VB Code:
    1. Dim g As Graphics = Me.CreateGraphics()
    2. Dim str As String = "135"
    3.  
    4. g.FillRectangle(Brushes.Black, 15, 5, 25, 17)
    5. g.DrawString(str, New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Point), Brushes.White, 15, 5)
    6.  
    7. g.Dispose()

    How can I switch the DrawString and FillRectangle so that one happens before the other (other than just changing the code order of when they are drawn)?

    Thanks!



    End Sub

  2. #2
    Addicted Member
    Join Date
    Sep 2004
    Location
    UK
    Posts
    129

    Re: Z-order when drawing graphics??

    Hi, Z order is based on the order that the primitives are draw, so in your case you will have to swap them around, perhaps with a couple of if statement.
    Drawing packages would typically store the primitives in a Collection or ArrayList making it easy to move them around, to affect the Z order.

    Hope this helps

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Z-order when drawing graphics??

    The best way would be to create a simple class for each type of thing you want to draw. Each class should inherit from a common class that has a Draw method. The derived classes override the Draw method and accept a Graphics parameter, to which the class draws itself.

    Then store a load of these derived classes in an arraylist and draw them all in a loop. To change the drawing order, simply change the order of the objects in the array.

    I can write you a sample project if you want. Let me know.
    I don't live here any more.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Re: Z-order when drawing graphics??

    Are you kidding? YES PLEASE write me a sample program. That would be great!

    Thanks!

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Z-order when drawing graphics??

    OK.

    I made it as simple as possible just to demonstrate the ZOrder principle and a bit of inheritance that lets you treat diamonds and circles in the same way.

    Any questions just PM me.
    Attached Files Attached Files
    I don't live here any more.

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Z-order when drawing graphics??

    A screeny for those too lazy to download it
    Attached Images Attached Images  
    I don't live here any more.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Re: Z-order when drawing graphics??

    Thanks! If nothing else..it is very pretty!

    I will have to look at closely, but what I need (to clarify further) is to draw an image at startup. While my program is running...another image will be drawn to the screen. This image must be (behind) the image that was drawn first. Will you example do that? To make it worse...I will have another image that will be moving around the screen as the program runs. This image must pass over (and under) other drawn images. That is why having a z-order to change on the fly would be nice.

  8. #8
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Z-order when drawing graphics??

    Quote Originally Posted by birthjay
    This image must pass over (and under) other drawn images
    I wish you had said that earlier.

    You'd be better off thinking about transparent colours and alphablending instead of zorder. You cannot draw something 'behind' something else but you can draw one thing and then something else on top. You have to draw from the back to the front in that order.

    My demo will not change the z order for you but it is ready to have that feature added simply by shuffling the contents of an arraylist in whatever sequence you like. Just look at the Button's code and you see what I mean.

    I didn't code the shuffling because i wasn't sure how you wanted to change the zOrder I've left it open ended for you.

    Feel free to give me a rating if you want
    I don't live here any more.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Re: Z-order when drawing graphics??

    Thanks! I thought when I said "z-order" that would be a clue to "over and under"...but anyway....

    I really appreciate your efforts and will give a great rating!

    Thanks again..

  10. #10
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Z-order when drawing graphics??

    Quote Originally Posted by birthjay
    Thanks! I thought when I said "z-order" that would be a clue to "over and under"...but anyway....

    I really appreciate your efforts and will give a great rating!

    Thanks again..
    I knew you meant "over and under" that is the very definition of the Z axis Its just the way you mentioned animation that makes alphablending more suitable. You can easily use BOTH z ordering AND alphablending to great effect though, give it a shot
    I don't live here any more.

  11. #11
    Addicted Member
    Join Date
    Sep 2004
    Location
    UK
    Posts
    129

    Re: Z-order when drawing graphics??

    Hi, you should be able to animate with Z order, as long as you redraw all the primitives every frame. This is well within the capabilities of GDI+, well up to approx 2000 – 3000 thousand primitives anyway!

    See the attached project for a demo. I have little experience with animation but you will need to address the animation speed issue, at the moment I just change the number of frames, I'm sure there is a more refined way of doing this.
    I’ve not commented but should be clear enough, let me know if you have any questions!
    Attached Files Attached Files

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Re: Z-order when drawing graphics??

    Very nice! Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width