2 Replies - 3104 Views - Last Post: 07 October 2012 - 01:39 AM Rate Topic: -----

#1 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

JPanel super.paintComponent(g) Method Not Clearing the Screen

Posted 06 October 2012 - 09:28 PM

Hello everyone. I am currently trying to code a very basic animation where a ball bounces around the screen. I'm trying to implement this as an application via JNLP/WebStart instead of as an applet. I'm running into a bit of trouble for which I have searched extensively for a solution, but have had no luck.

My code:

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

public class BouncingBall extends JFrame {

	JFrame frame;
	Ball mainpanel;
	
	protected Color ballColor = Color.blue;
	protected int radius = 20;
	protected int x, y, dx, dy;
	
	protected Image image;
	protected Graphics offscreen;	
	
	private Thread ballThread;


	public static void main(String[] args) {
		BouncingBall bb = new BouncingBall();
		bb.go();
	}

	public void go(){
		x = 250;
		y = 225;
		dx = 2;
		dy = 2;
		frame = new JFrame("Bouncing Ball");
		mainpanel = new Ball();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500, 450);
		frame.getContentPane().add(mainpanel);
		frame.setVisible(true);
		mainpanel.start();		
		
	}

	public class Ball extends JPanel implements Runnable{
		
		public void start(){
			ballThread = new Thread(this);
			ballThread.start();
		}
		
		public void stop(){
			ballThread = null;
		}
		
		public void run(){
			while(Thread.currentThread() == ballThread){
				try{
					Thread.currentThread().sleep(10);
				}catch (InterruptedException e){}
				repaint();
			}
		}
		
		public void paintComponent(Graphics g){
            super.paintComponent(g);
			Dimension d = getSize();
			if(image == null){
				image = createImage(d.width, d.height);
				offscreen = image.getGraphics();
			}
			//offscreen.setColor(Color.white);
			//offscreen.fillRect(0, 0, d.width, d.height);
			offscreen.setColor(ballColor);
			offscreen.fillOval(x, y, 2*radius, 2*radius);
			g.drawImage(image, 0, 0, this);
			if (x <= 0 || x >= d.width - 2*radius)
				dx = -dx;
			if (y <= 0 || y >= d.height - 2*radius)
				dy = -dy;
			x+=dx;
			y+=dy;
		}
	}

}



Basically, in the paintComponent() method of the Ball class, the
super.paintComponent(g)
that is supposed to clear the screen before drawing the circle isn't clearing the screen. I've searched fairly extensively for solutions to this, but I keep coming across threads where the ANSWER is to use
super.paintComponent(g)
. However, in this case, it is not clearing and the ball streaks across the screen leaving a trail wherever it goes. As you can see, I used a bandaid solution of filling a white rectangle the size of the frame, but that is not acceptable.

Can anyone explain why the JPanel's super.paintComponent(g) isn't clearing the screen as it should (as it has been said in many threads)? Thanks in advance.

Is This A Good Question/Topic? 1
  • +

Replies To: JPanel super.paintComponent(g) Method Not Clearing the Screen

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 850
  • View blog
  • Posts: 2,597
  • Joined: 29-July 11

Re: JPanel super.paintComponent(g) Method Not Clearing the Screen

Posted 06 October 2012 - 10:26 PM

The JPanel is redrawing its background! Remember that you are drawing to an offscreen image which has its own background that you must redraw yourself. Your objects are moving within the images background, not the Jpanel's. Your fillRect the width and height of the image is exactly what you should do. It's also how paintComponent redraws the background of a JComponent. You can verify this by examining the source code for JComponent's paintComponent() method.

EDIT: This was a good question!

This post has been edited by farrell2k: 07 October 2012 - 07:40 AM

Was This Post Helpful? 1
  • +
  • -

#3 axnjxn  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 144
  • Joined: 04-February 12

Re: JPanel super.paintComponent(g) Method Not Clearing the Screen

Posted 07 October 2012 - 01:39 AM

Thanks a lot, man! I really appreciate it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1