| | |
repaint()
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 60
Reputation:
Solved Threads: 0
Hi, I am sending a oval shaped ball across a screen and use the repaint() method controlled by a timer to move it.
The green ball does move across the screen but draws a line in its wake. Only when I resize the window does the wake vanish.
Is there something new? I have seen suggestions of a revalidate() method but that method does not seem to exist.
Any help would be welcome.
Many thanks
public class Court extends JFrame implements Runnable
{
private int x = 10;
private int y = 200;
private int r = 3;
/** Creates new form MainContainer */
public Court()
{
this.setBackground(Color.BLACK);
this.setVisible(true);
start();
}
public void run()
{
while(true)
{
x++;
repaint();
//this.paint(this.getGraphics());
try
{
Thread.sleep(10);
}
catch (InterruptedException ex)
{
}
}
}
public void start()
{
Thread thread = new Thread(this);
thread.start();
}
void p(Graphics g)
{
super.paintComponents(g);
}
public void paint(Graphics g)
{
g.setColor(Color.GREEN);
g.fillOval(x-r, y-r, 2*r, 2*r);
}
}The green ball does move across the screen but draws a line in its wake. Only when I resize the window does the wake vanish.
Is there something new? I have seen suggestions of a revalidate() method but that method does not seem to exist.
Any help would be welcome.
Many thanks
That's because you aren't repainting the background rectangle before you paint the oval. Even so, you really should do your custom painting on a lightweight component such as a JPanel or small JComponent class that overrides paintComponent(), rather then painting directly on the JFrame. Here is your code with a small canvas component for the painting:
java Syntax (Toggle Plain Text)
import java.awt.Color; import java.awt.EventQueue; import java.awt.Graphics; import javax.swing.JComponent; import javax.swing.JFrame; public class Court extends JFrame implements Runnable { private int x = 10; private int y = 20; private int r = 3; public Court() { setBounds(0, 0, 200, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); this.setBackground(Color.BLACK); // adding the new component here getContentPane().add(new CanvasComponent()); this.setVisible(true); start(); } // Custom component for your paint work. // JPanel is also a good candidate for this. class CanvasComponent extends JComponent { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.BLACK); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.GREEN); g.fillOval(x - r, y - r, 2 * r, 2 * r); } } public void run() { while (true) { x++; repaint(); try { Thread.sleep(10); } catch (InterruptedException ex) {} } } public void start() { Thread thread = new Thread(this); thread.start(); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { new Court(); } }); } }//end
![]() |
Similar Threads
- Repaint() not repainting. (Java)
- JFrame paint(), repaint() etc. (Java)
- Java Swing Problem - won't repaint (Java)
- I have some examples who would like to see some cool stuff (Java)
- Win Explorer tree flickers (Windows NT / 2000 / XP)
- W2K screen freeze and icon repaint (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: Dynemic Image on JPanel
- Next Thread: Java GUI Run Error
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac main map method methods mobile myregfun netbeans newbie notdisplaying number online page print problem program programming project qt recursion scanner screen server set singleton size sms sort spamblocker sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






