0

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++);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 4
    `Don't override paint() on a JFrame`. Custom painting is done by overriding `paintComponent(...)` on a JPanel. Then you add the panel to the frame. Read the Swing tutorial on [Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for complete working examples to get you started. 2) the painting method should NOT generate random colors. A painting method should not change the state of the painting only paint the current state. – camickr Nov 27 '22 at 16:19
  • If you want multiple circles, then use an ArrayList to keep track of all the circles you want to paint. Check out: https://stackoverflow.com/a/54028681/131872 for an example of this approach. – camickr Nov 27 '22 at 16:22
  • `rita.paint(null);` will not paint a circle - it will lead to a NullPointerException when calling `g.setColor(randomColor)`. The circles are drawn because Swing will eventually call `Rita.paint()` with a proper `Graphics` instance. And since Swing is free to call `Rita.paint()` as often as it wants you will eventually see even more random circles being drawn. – Thomas Kläger Nov 27 '22 at 16:46

0 Answers0