I just started to study Java. I am trying to make a program that will show five circles (random color, size and placement). I thought I would try to draw one circle (before I try five). But I draw two circles! What is going on? How is it drawing two circles?
Here is my code:
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import java.util.Random;
public class Rita extends JFrame
{
public Rita()
{
setTitle("Rita");
setSize(960, 960);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
// Initiateea random object
Random rand = new Random();
// loop to draw cicles
//for (int i = 0; i<5; i++);
//Initiate random color
int red = rand.nextInt(256);
int green = rand.nextInt(256);
int blue = rand.nextInt(256);
Color randomColor = new Color (red, green, blue);
// Generate random circle width 20 and X pixels
int width = rand.nextInt(201) + 20;
// Generate random X/Y koordinater
int radius = (int)(Math.random()*49) + 2;
int x = (int)(Math.random()*(960-radius*2) + radius);
int y = (int)(Math.random()*(960-radius*2) + radius);
//int x = rand.nextInt(960);
//int y = rand.nextInt(960);
// set the color
g.setColor(randomColor);
// Draw the circle
g.fillOval(x, y, width, width); // aka rita oval, storlek
}
public static void main(String[] args)
{
Rita rita= new Rita();
rita.paint(null);
}
}
I tried to put this code in to hopefully override the two circles drawn or something. It didn't work (of course I deleted the comment-slashes first). It still draws two circles.
// loop to draw cicles
//for (int i = 0; i<5; i++);