repaint()

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2007
Posts: 60
Reputation: Grub is an unknown quantity at this point 
Solved Threads: 0
Grub Grub is offline Offline
Junior Poster in Training

repaint()

 
0
  #1
Feb 4th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: repaint()

 
0
  #2
Feb 4th, 2009
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 60
Reputation: Grub is an unknown quantity at this point 
Solved Threads: 0
Grub Grub is offline Offline
Junior Poster in Training

Re: repaint()

 
0
  #3
Feb 5th, 2009
So I have used a JPanel and a JFrame and paint() the JFrame as well! This has worked.

Many thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC