| | |
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
Views: 913 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
3d @param affinetransform android api apple applet application arc arguments array arrays automation binary bluetooth byte c# chat class classes click client code compare component corrupted database detection draw eclipse error event exception file fractal game givemetehcodez graphics gui guitesting helpwithhomework html ide image input integer j2me java java.xls javaprojects jmf jni jpanel julia keytool linux list loop map method methods mobile netbeans newbie number object oracle os pong print problem producer program programming project projectideas read recursion reflection replaysolutions rim scanner screen server set size sms socket sort sql string swing terminal test threads time transfer tree web windows






