Results 1 to 3 of 3
Thread: how to unpaint my circle
- 09-11-2011, 05:09 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
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
-
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!
- 09-12-2011, 05:43 AM #3
Similar Threads
-
Why is there no circle?
By JohnPringle83 in forum New To JavaReplies: 3Last Post: 05-23-2011, 08:07 AM -
is this circle okeh ?
By SHE in forum New To JavaReplies: 0Last Post: 04-14-2011, 05:34 AM -
Josephu's Circle
By Pyrexkidd in forum New To JavaReplies: 8Last Post: 08-01-2010, 08:44 AM -
Circle arrays
By n00b in forum New To JavaReplies: 15Last Post: 05-05-2010, 05:04 PM -
Circle and line
By c_walker in forum New To JavaReplies: 1Last Post: 01-27-2010, 03:56 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks