Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

JApplet: Random Circles

  1. Feb 17, 2017 #1

    I have this program where you run the JApplet and it makes circles at random. When you click the "Generate" button, it makes another set of random circles. The problem is, it overlaps the previous set of circles. I want the program to get rid of the previous set and I can't figure out how to do it. Thank you for your help.

    Code (Java):
    import java.awt.*;
    import javax.swing.*;
    import java.applet.Applet;
    import java.net.URL;
    import java.awt.event.*;

    public class MilkyWay extends JApplet implements ActionListener
        Button Button1;
        public void init() {
            setLayout( new FlowLayout( ) );
            // New button
            Button1 = new Button("Generate");
            // add button in japplet
            // listens for the button
        public void drawCircle(Graphics g, int x, int y, int radi) {
            // draws a circle at x,y of radius r
            int red, green, blue;
            // choose random colors from rgb
            red = (int) (Math.random() * 256);
            green = (int) (Math.random() * 256);
            blue = (int) (Math.random() * 256);
            // set the foreground color
            g.setColor(new Color(red, green, blue));
            // draws the Oval
            g.fillOval(x, y, 2*radi, 2*radi);
        } // end of drawCircle
        public void paint( Graphics g ) {
            int x1, y1, radi;
            for (int i=0; i < 100; i++) {
                // displays circles in x/y coordinate boundaries
                x1 = (int) (Math.random()*800);
                y1 = (int) (Math.random()*800);
                // math choose random radius
                radi = 5 + (int)(Math.random()*35);
                // draw the circle
                drawCircle(g, x1, y1, radi);
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == Button1)
  2. jcsd
  3. Feb 17, 2017 #2


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    You need to put something in your paint method to clear out the previous data. I don't have the method at the top of my head but something like a clear method on g.
  4. Feb 17, 2017 #3
    I've looked around online and come across this, would I use this? I would need to manipulate it, but I just wanted to know if you're referring to this.

    Code (Java):
    public void clear(Graphics g)
            // if(!clear)return; // optional
  5. Feb 17, 2017 #4


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

  6. Feb 17, 2017 #5
    Thank you! I needed to add super.paint(g) in the paint method. This helped!

    Attached Files:

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Threads - JApplet Random Circles Date
C/++/# How to check if a list of numbers is random? Sep 13, 2017
String Array for JApplet Feb 27, 2017
Random generated doubles: can I get a number more than once? May 3, 2016
Random walk Jan 30, 2016
Random.randrange() is really random? Dec 24, 2015