repaint() leaves trace of object initial position

40 views Asked by At

I'm implementing a JFrame program that is supposed to mimic balls bouncing with wall & between other balls in a concurrent setting. While the balls are correctly displayed and updated, it leaves the trace of the very first position. Seems like it's the edge plus some gradient gray color. Current look

I've followed steps here Transparent JFrame background to set background as transparent

Frame instantiation:

   public static void main(String[] args) {
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            JFrame frame = new JFrame("Bouncing Ball");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(new BouncingBall(640, 480));
            frame.setUndecorated(true);
            frame.setBackground(new Color(0, 0, 0, 0));
            frame.pack();
            frame.setVisible(true);
         }
      });
   }

JPanel repaint:

    class PaintCanvas extends JPanel {
        @Override
        public void paintComponent(Graphics graphics) {
            super.paintComponent(graphics);
            // wall.drawWall(graphics);
            for (int i = 0; i < numBalls; i++) {
                Ball ball = balls.get(i);
                ball.drawBall(graphics);
            }  
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(width, height);
        }
    }

Ball drawing:

    public void drawBall(Graphics graphics) {
        graphics.setColor(new Color(1, 1, 0, 1f));
        graphics.fillOval((int)(posX - radius), (int)(posY - radius), radius * 2, radius * 2);
    }

I could not find any simlar posts on Stack Overflow or anywhere... Any idea greatly appreciated!

0

There are 0 answers