I'm just starting to learn Java and I need help. I'm trying to draw a rectangle in an applet window and have it centered so that if the applet is repainted when it is resized, the rectangle will stay centered in the applet window. I know how to retrieve the size of the applet, but I'm not quite sure what to do to center the rectangle. Can someone link me to a tutorial that explains something like this?
packetpirate
0
Junior Poster in Training
Recommended Answers
Jump to PostThe math isn't difficult. You have the dimensions of the applet, so you know the center point. That center point is also the center point of the rectangle.
A rectangle in Swing is defined by the top left point and its two sides. The sides will not change - all …
Jump to PostCalculate the center point (appW/2, appH/2) and then use those to figure where to place your upper left corner of the rect.
All 7 Replies
jon.kiparsky
326
Posting Virtuoso
packetpirate
0
Junior Poster in Training
jon.kiparsky
326
Posting Virtuoso
packetpirate
0
Junior Poster in Training
packetpirate
0
Junior Poster in Training
Ezzaral
2,714
Posting Sage
Team Colleague
Featured Poster
packetpirate
0
Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.