943,550 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1832
  • Java RSS
Feb 4th, 2009
0

repaint()

Expand Post »
Hi, I am sending a oval shaped ball across a screen and use the repaint() method controlled by a timer to move it.
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
Similar Threads
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
Grub is offline Offline
60 posts
since Oct 2007
Feb 4th, 2009
0

Re: repaint()

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)
  1. import java.awt.Color;
  2. import java.awt.EventQueue;
  3. import java.awt.Graphics;
  4. import javax.swing.JComponent;
  5. import javax.swing.JFrame;
  6.  
  7. public class Court extends JFrame implements Runnable {
  8.  
  9. private int x = 10;
  10. private int y = 20;
  11. private int r = 3;
  12.  
  13. public Court() {
  14. setBounds(0, 0, 200, 200);
  15. setDefaultCloseOperation(EXIT_ON_CLOSE);
  16. this.setBackground(Color.BLACK);
  17.  
  18. // adding the new component here
  19. getContentPane().add(new CanvasComponent());
  20.  
  21. this.setVisible(true);
  22. start();
  23.  
  24. }
  25.  
  26. // Custom component for your paint work.
  27. // JPanel is also a good candidate for this.
  28. class CanvasComponent extends JComponent {
  29. @Override
  30. protected void paintComponent(Graphics g) {
  31. super.paintComponent(g);
  32. g.setColor(Color.BLACK);
  33. g.fillRect(0, 0, getWidth(), getHeight());
  34. g.setColor(Color.GREEN);
  35. g.fillOval(x - r, y - r, 2 * r, 2 * r);
  36. }
  37. }
  38.  
  39. public void run() {
  40. while (true) {
  41. x++;
  42. repaint();
  43.  
  44. try {
  45. Thread.sleep(10);
  46. } catch (InterruptedException ex) {}
  47. }
  48. }
  49.  
  50. public void start() {
  51. Thread thread = new Thread(this);
  52. thread.start();
  53. }
  54.  
  55. public static void main(String[] args) {
  56. EventQueue.invokeLater(new Runnable() {
  57. public void run() {
  58. new Court();
  59. }
  60. });
  61. }
  62. }//end
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Feb 5th, 2009
0

Re: repaint()

So I have used a JPanel and a JFrame and paint() the JFrame as well! This has worked.

Many thanks
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
Grub is offline Offline
60 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Dynemic Image on JPanel
Next Thread in Java Forum Timeline: Java GUI Run Error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC