Emma Lee
Emma Lee

Reputation: 35

Random Circle Printer Java: Graphics not printing

I tried to create a code that printed n random circles in a 500 x 500 frame but it didn't work.

Can somebody tell me why this code isn't running?

When I run this code, it lets me enter the number of random circles I want but the frame always appear to be empty - no circles are drawn.

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Scanner;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class RandomCircles extends JComponent
{
    private int n;

    public RandomCircles(int N)
    {
        n = N;
    }

    public void PaintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
    
        double x = Math.random() * 500;
        double y = Math.random() * 500;
        double diameter = Math.random() * 500;
    
        // Making sure the circle stays within the frame
        for (int i = 0; i < n; i++)
        {
            while(x + diameter <= 500 || y + diameter <= 500)
            {
                Ellipse2D.Double circle 
                    = new Ellipse2D.Double(x, y, diameter, diameter);
                g2.draw(circle);
            }
        }
    }

    public static void main(String[]args)
    {
        Scanner in = new Scanner(System.in);
    
        System.out.println("Enter number of circles here: ");
        int n = in.nextInt();
    
        JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setTitle("Random Circles");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        RandomCircles circle = new RandomCircles(n);
        frame.add(circle);
        
        // Add PaintComponent method somewhere here? 

        frame.setVisible(true);
    }
}

I have a feeling that I need to add in the public void PaintComponent(Graphics g) somewhere to print it out, but I am not sure how.

Upvotes: 1

Views: 90

Answers (1)

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103145

The problem is on this line:

public void PaintComponent(Graphics g)

You are attempting to override the paintComponent(Graphics) method. You need to be careful that you get the name and the parameters right. Notice you spelled your method with an upper case P.

It is advisable that you add the annotation @Override on methods that are supposed to override a super class' method. That way you get a notification if you get the signature wrong.

So your method should look like this:

@Override
public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;
    ...
}

== Edit after comments ==

The while loop is also causing an issue. Try doing this instead.

for (int i = 0; i < n; i++)
    {
        double x = Math.random() * 500;
        double y = Math.random() * 500;
        double diameter = Math.random() * 500;
       
        Ellipse2D.Double circle
                    = new Ellipse2D.Double(x, y, diameter, diameter);
        g2.draw(circle);
     }

Notice that I moved the random number generation inside the loop. This does not guarantee the circle fitting in the frame but that is something you can modify later.

Upvotes: 1

Related Questions