I wrote a code about circle starts to grow followed by finner circles, with each new one disappearing earlier while growing but in my code it is only growing how can I disappear the early one??
I didnt put the main method only classes

import java.util.ArrayList;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DotsPanel extends JPanel
{
   private final int MAX_WIDTH = 300;
   private final int RING_WIDTH = 10;
	int x = 300, y = 300, diameter=0;
	Color clr;
	int f;

   public ArrayList<Circle> pointList,pointList1;


   public DotsPanel(Color c,int a)
   {
      pointList = new ArrayList<Circle>();

      setBackground (Color.white);
      setPreferredSize (new Dimension(300, 200));

      PaintAction action = new PaintAction();
      Timer timer = new Timer(a, action);

      timer.start();

   }
   private class PaintAction implements ActionListener {
      public void actionPerformed(ActionEvent evt) {
         repaint();  // Call the repaint() method in the panel class.
      }
   }

   public void paintComponent (Graphics page)
   {
      super.paintComponent(page);
      
        Circle c=new Circle(diameter,x,y,clr);
      	pointList.add(c);

         diameter += (2 * RING_WIDTH);
         x -= RING_WIDTH;
         y -= RING_WIDTH;

      
      for (Circle a : pointList) {
            a.draw(page);
        }
   }

}
import java.awt.*;

//Circle
class Circle {
    //=========================================================== fields
    int   _x;           // x coord of bounding rect upper left corner.
    int   _y;           // y coord of bounding rect upper left corner.
    int   _diameter;    // Height and width of bounding rectangle.
    Color _color;
    //====================================================== constructor
    Circle( int diameter,int x, int y,Color color) {
    
        _x        = x;
        _y        = y;
        _diameter = diameter;
        _color=color;
    }
    
    //============================================================= draw
    void draw(Graphics g) {
        
        g.setColor(_color);
        g.drawOval(_x, _y, _diameter, _diameter);
    }
}

Recommended Answers

All 4 Replies

Can you restate the problem because it's a bit unclear what the goal is and what the actual behavior is (at least to me)? Also, please upload the main function and anything else needed to run the program so we can try it out. Thanks.

You can make earlier Circles disappear by removing this code:

for (Circle a : pointList) {
            a.draw(page);
        }

It seems like that code draws every single circle that you added to pointList.

Whenever you click the mouse button, an effect like a drop falling on a water surface is created, i.e. a circle starts to grow at the position of the click, followed by a couple of inner circles, with each new one disappearing earlier while growing. You need a Timer object and a way to represent the current situation of the circles

Main class of program is here:

import java.awt.*;
import javax.swing.JFrame;

public class Dots
{

   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Dots");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
	  DotsPanel p=new DotsPanel(Color.black,1000);
      frame.getContentPane().add (p);

      frame.pack();
      frame.setVisible(true);
   }
}

You need to update the coordinates of existing circles if you wish for them to change size or position. I'd recommend taking all code that creates or changes the size out of the paintComponent() method. That should just render the circles you have created. Handle the adding of a new circle and growing of existing circles in other methods.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.