0

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

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by Grub
0

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:

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
0

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

Many thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.