How can I prevent overlapping circles in a JApplet program?

  • Thread starter Thread starter FritoTaco
  • Start date Start date
  • Tags Tags
    Circles Random
Click For Summary

Discussion Overview

The discussion revolves around a programming issue in a JApplet where circles are drawn randomly upon clicking a button. The main problem is that new circles overlap with previously drawn ones, and the user seeks a solution to clear the previous set of circles before drawing new ones.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant suggests that the paint method needs to include a way to clear previous drawings, mentioning a "clear method" for the Graphics object.
  • Another participant proposes a specific method to clear the graphics context by filling a rectangle with the background color, indicating that it may require some manipulation.
  • A link to an external thread is provided for additional context on repaint issues in Java graphics.
  • A later reply indicates that adding "super.paint(g)" to the paint method resolved the issue for the original poster.

Areas of Agreement / Disagreement

Participants generally agree that the paint method needs modification to prevent overlapping circles, but there are different suggestions on how to achieve this. The discussion includes both proposed methods and a reference to external resources, indicating no single consensus on the best approach.

Contextual Notes

Some participants mention the need for additional manipulation of the proposed clear method, suggesting that there may be nuances in implementation that are not fully resolved.

FritoTaco
Messages
132
Reaction score
23
Hello,

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.

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
        add(Button1);
       
        // listens for the button
        Button1.addActionListener(this);
    }
    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) {
        //repaint
        if (e.getSource() == Button1)
            repaint();
    }
}
 
Technology news on Phys.org
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.
 
  • Like
Likes   Reactions: FritoTaco
Borg said:
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.

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.

Java:
public void clear(Graphics g)
    {
        // if(!clear)return; // optional
        g.setColor(getBackground());
        g.fillRect(0,0,(int)this.getWidth(),(int)this.getHeight());
        clear=true;
    }
 
Thank you! I needed to add super.paint(g) in the paint method. This helped!
 

Attachments

  • 1jtxmu.gif
    1jtxmu.gif
    187.9 KB · Views: 462
  • Like
Likes   Reactions: Borg

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
8
Views
5K