# Draw a rhombus

• 10-12-2010, 03:19 PM
jack DANIEL's
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
• 10-12-2010, 03:38 PM
KevinWorkman
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.
• 10-12-2010, 06:11 PM
Fubarable
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!
• 10-13-2010, 12:30 AM
Rectal Exambot
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.
• 10-13-2010, 04:36 AM
DarrylBurke
Quote:

Originally Posted by Rectal Exambot
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
• 10-13-2010, 04:40 AM
pbrockway2
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++
• 10-13-2010, 07:57 AM
JosAH
Quote:

Originally Posted by Darryl.Burke
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