Results 1 to 7 of 7

Thread: Draw a rhombus

  1. #1
    jack DANIEL's is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default Draw a rhombus

    Hello everybody,

    I need to draw a rhombus into a JComponent. I ain't so familiar with the Graphics & Graphics2D classes, so I'd like to ask you if somebody could help me. I thought of drawing each of the four lines that will be the rhombus' faces, but I don't know how to have them really painted into a component, or similar.

    Thank you in advance for the answers

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default

    Check out this tutorial: Painting in AWT and Swing

    But basically, you just want to extend a JPanel (or JComponent), override paintComponent, and do your custom painting in that method.

    Check out the API for Graphics and Graphics2D for useful drawing methods.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I second Kevin's recommendations since you need to understand the concepts in these tutorials before anyone can help you, and in fact I recommend the Swing tutorial and API in general as the first resource you should check out for questions such as these. Then if you get stuck and your code doesn't work, please come on back, show us your code and we'll be better able to help you.

    Best of luck!

  4. #4
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    You should do some tutorials on how to draw first, that is how I learnt.

    An easy way would be to make a rectangle, and then rotate it, and then scale it.

    Look up the graphics API for scale, rotate, draw, JFrames and rectangles.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by Rectal Exambot View Post
    An easy way would be to make a rectangle, and then rotate it, and then scale it.
    Sorry, but that's about the most difficult way I could think of (and you would start with a square, not a rectangle).

    The Polygon class might be worth a try. But as already mentioned, only after going through some tutorials on custom painting.

    db

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Rotated and scaled rectangles are still rectangles... (or are we thinking of stretching them?) STarting with a rectangle, what you would do is join its midpoints.

    but tutorials++

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,369
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Sorry, but that's about the most difficult way I could think of (and you would start with a square, not a rectangle).
    Using Graphics2D it isn't that difficult:

    1) rotate around 45 degrees
    2) apply a nice scaling
    3) draw a rectangle with equal sides (a square)

    kind regards,

    Jos

Similar Threads

  1. Draw Graphics on top of another
    By child in forum AWT / Swing
    Replies: 9
    Last Post: 01-17-2010, 04:14 PM
  2. Finding where to draw
    By flok in forum AWT / Swing
    Replies: 4
    Last Post: 11-24-2009, 05:55 PM
  3. how to draw an arc
    By Baker in forum New To Java
    Replies: 1
    Last Post: 04-16-2009, 09:05 PM
  4. Need help coding a rhombus in stars
    By LinxuS in forum New To Java
    Replies: 5
    Last Post: 10-18-2008, 12:12 AM
  5. help me draw... please...
    By kureikougaiji in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 12:22 PM

Posting Permissions

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