Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: how to unpaint my circle

  1. #1
    vettera is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default how to unpaint my circle

    i animated a circle that goes down the page and when it goes down. It does not erase the previous circle. I was wondering if there was a certain function for it.
    Code:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.Ellipse2D;
    public class ball extends JFrame implements Runnable {
    	int y = 0;
    	final int Delay = 5;
    	Thread anima;
    	
        public ball(){
        super("Moving Ball");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500,500);
        setVisible(true);
        setResizable(false);
        setBackground(Color.white);
        }
        public void addNotify(){
        	super.addNotify();
        	anima = new Thread(this);
        	anima.start();
        }
    	public void paint(Graphics g){
    		Graphics2D ga = (Graphics2D)g;   
    		Ellipse2D e = new Ellipse2D.Float(200,y,100,100);
    		ga.setColor(new Color(75,0,130));
    		ga.fill(e);
    	    ga.draw(e);
    	}
        public void cycle(){
        	y++;
        	if(y >= 500){
        		y = 0;
        	}
        }
        public void run(){
        	long starttime , total , sleep;
        	starttime = System.currentTimeMillis();
        	while(true){
        		cycle();
        		repaint();
        		total = System.currentTimeMillis() - starttime;
        		sleep = Delay - total;
        		if(sleep < 0){
        			sleep = 2;
        		}
        		try {
    				Thread.sleep(sleep);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			starttime = System.currentTimeMillis();
        	}
        }
        public static void main(String[] args ){
        	ball b = new ball();
        }
    }
    Last edited by Norm; 09-11-2011 at 02:51 PM. Reason: added code tags

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to unpaint my circle

    Your primary problem is that your paint method doesn't call the super.paint(g); method as its first method call. But you have other problems as well:

    • You shouldn't be drawing directly in a JFrame and
    • You especially shouldn't be animating in a JFrame, unless you want choppy animations.
    • Instead draw/animate in a JPanel
    • and do your drawing in its paintComponent method override, not its paint method.
    • The first line of your paintComponent method override should be a call to the super's paintComponent method (analogous to for the paint method).
    • Rather than use background threads for simple animations, consider using a Swing Timer.


    Also, please check out my link on using code tags when posting code here.

    Much luck!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: how to unpaint my circle

    • To improve efficiency, don't construct a new Ellipse2D every time through the painting method. Maintain one Ellipse2D as an instance field, possibly final, and mutate it before painting.
    • Similarly, cache the Color(75,0,130), possibly as a static final field.


    db
    Fubarable likes this.

Similar Threads

  1. Why is there no circle?
    By JohnPringle83 in forum New To Java
    Replies: 3
    Last Post: 05-23-2011, 08:07 AM
  2. is this circle okeh ?
    By SHE in forum New To Java
    Replies: 0
    Last Post: 04-14-2011, 05:34 AM
  3. Josephu's Circle
    By Pyrexkidd in forum New To Java
    Replies: 8
    Last Post: 08-01-2010, 08:44 AM
  4. Circle arrays
    By n00b in forum New To Java
    Replies: 15
    Last Post: 05-05-2010, 05:04 PM
  5. Circle and line
    By c_walker in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 03:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •