getWidth && getHeight
Hello! My first question here (I did 'search':)):
X = getWidth()/2;
Y = getHeight()/2;
put a circle in the center of JFrame (no matter what its setLocation and setSize are)? (The code is put in the JPanel, which is then added to JFrame.)
g.drawOval(X, Y, r1, r1)
What "width" and "height" exactly do the methods
return (yes, I've checked the API documentation)?
Feeling I'm missing something basic here... :o
Not quite. The X and Y set the upper left corner of the box that holds the oval. If you want to center it, you'll need to do some rudimentary algebra that involves your x, y, and r1. Also, your r1 represents the diameter rather than radius.
I did something like so in my app:
int x = (mainPanel.getWidth() - DIAMETER)/2;
int y = (mainPanel.getHeight() - DIAMETER)/2;
Graphics2D g2 = (Graphics2D)g;
// smooth out the drawings jagged edges
g2.drawOval(x, y, DIAMETER, DIAMETER);