digiboos...@gmail.com wrote:
Hi, I designed this code, but I cannot put all of the circles on same
center. I think they are on different dimensions, what do you think?I think you need to examine the JavaDocs of the
methods you are using..
<sscce>
import javax.swing.*;
import java.awt.*;
import java.util.Random;
/* CirclePanel.java
Design and implement a program that draws 10 concentric circles of
random radius.
*/
class CirclePanel extends JPanel
{
public CirclePanel()
{
setPreferredSize (new Dimension(300,300));
setBackground (Color.white);
}
public void paintComponent(Graphics page)
{
super.paintComponent (page);
int x = 0, y = 0;
int diameter;
page.setColor (Color.white);
for (int count = 0; count <10; count++)
{
Random generator = new Random();
diameter = generator.nextInt(150)+5;
page.setColor (Color.black);
/* x,y is not the center, it is the
co-ord of the upper left of a 'box'
that surround the oval */
page.drawOval (x, y, diameter, diameter);
int centerX = getWidth()/2;
int centerY = getHeight()/2;
page.setColor (Color.red);
page.drawOval (centerX-(diameter/2),
centerY-(diameter/2),
diameter,
diameter);
}
}
public static void main (String[] args)
{
JFrame frame = new JFrame ("Concentric");
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new CirclePanel());
frame.pack();
frame.setVisible(true);
}}</sscce>
Andrew T.