0

I am trying to make the effect of gravity but it just looks like there are growing streaks of circles instead of individual circles moving down. I do not know how to remove the circles I have already drawn. There are no errors in the code btw.

import javax.swing.Timer;
import javax.swing.*;

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
public class Tester {


    static JFrame frame;
    static JPanel panel;
    static JButton button;
    static ArrayList<Ellipse2D.Double> circles = new       ArrayList<Ellipse2D.Double>();

    static void init(){
        frame = new JFrame();
        panel = new JPanel(new BorderLayout());
        button = new JButton("South");
        panel.add(button, BorderLayout.SOUTH);

        frame.add(panel);

        frame.setVisible(true);
        panel.setVisible(true);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setBackground(Color.LIGHT_GRAY);



}

public static void main(String[] args) {
     init();

class MeteorMover extends JPanel{

         Ellipse2D.Double m;
         int x = 40,y=40;
         boolean isSettingGravity=true;

         public MeteorMover(){
             m = new Ellipse2D.Double(x,y,30,30);

         }

         void createNewMeteor(int n){
            repaint(); 
         }

         void setGravity(){
             isSettingGravity = true;
             for (int i=0;i<circles.size();i++){
                 Ellipse2D.Double m = circles.get(i);
                 m= new Ellipse2D.Double(m.getX(),m.getY()+1,30,30);
                circles.set(i, m);

             }
             repaint();
         }

         protected void paintComponent(Graphics g){
             Graphics2D g2 = (Graphics2D) g;
             g2.setColor(Color.WHITE);
             if (isSettingGravity){
             for (Ellipse2D.Double c:circles){
                g2.draw(c); 
             }
             isSettingGravity = false;
             }
             else{
             m = new Ellipse2D.Double(x,y,30,30);
             circles.add(m);


             g2.fill(m);
             g2.draw(m);

             Random r = new Random();
             x = r.nextInt(500);
             y=r.nextInt(100);
             }

         }

     }

     final MeteorMover m = new MeteorMover();
     panel.add(m);
     panel.repaint();


     class TimerListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent arg0) {

             m.createNewMeteor(1);
        }

     }
     TimerListener cListener = new TimerListener();
     Timer timer = new Timer(1000,cListener);
     timer.start();

     class TimerListener2 implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent arg0) {

            m.setGravity();
        }

     }



     TimerListener2 gListener = new TimerListener2();
     Timer gTimer = new Timer(100,gListener);
     gTimer.start();

}

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Perhaps you should have a look at [Painting in AWT and Swing](http://www.oracle.com/technetwork/java/painting-140037.html) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for more details about how painting works in Swing – MadProgrammer Mar 13 '16 at 03:30
  • Also, I'd remove all logic from your painting routines, painting is painting, nothing else – MadProgrammer Mar 13 '16 at 03:30

2 Answers2

3

call super.paintComponent(g);

protected void paintComponent(Graphics g) {
                super.paintComponent(g);

read more about super.paintComponent .

enter image description here

Community
  • 1
  • 1
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
0

There's no direct way to erase with graphics, you have two options:

  1. If you always know which ellipse you need to erase, AND the ellipses never intersect, then you could keep in memory which is the next ellipse to erase and call g.setColor(bgColor); g.fill(erasedEllipse);

  2. This is option is more reliable, You could keep an ArrayList of ellipses to draw. and you could clear all the pane and repaint all the ellipses in the ArrayList, and if you want to erase one, you just call ArrayList.remove(erasedElipseIndex)

Maljam
  • 6,244
  • 3
  • 17
  • 30