简体   繁体   中英

Drawing in JPanel vs JComponent

I need some help understanding why the drawing works differently in JComponent vs JPanel.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Particle extends JComponent implements Runnable{
    private int x = 45;
    private int y = 45;
    private int cx;
    private int cy;
    private int size;
    private Color color;
    private JFrame frame;

    public Color getColor(){
        return color = new Color(100,0,190);
    }

    public Particle(){
        frame = new JFrame();
        frame.setSize(400, 400);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
        frame.setVisible(true);
    }

    public void update(){
        x+=1;
        y+=1;
    }

    public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(getColor());

        g2d.fillRect(x, y, 4, 4);
    }

    public void startThread(){
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        for(int i = 0; i <= 200; i++){
            try{
                update();
                repaint();
                Thread.sleep(4);    
            }catch(Exception e){
                System.out.print("Exception at thread.start()");
            }
        }
    }

    public static void main(String[] args) {
        Particle particle = new Particle();
        particle.startThread();
    }
}

In this above example, the "particle" moves from point A to point B just fine..

But when I subclass Particle from JComponent to JPanel..

the drawing forms a line.. ie the rectangle never disappears from where it starts..

Why is this the case?

A solution has been post by Toilal . I want to explain why :

In the API docs of paintComponent of JComponent

Further, if you do not invoker super's implementation you must honor the opaque property, that is if this component is opaque, you must completely fill in the background in a non-opaque color. If you do not honor the opaque property you will likely see visual artifacts.

In setOpaque of JComponent

The default value of this property is false for JComponent . However, the default value for this property on most standard JComponent subclasses (such as JButton and JTree ) is look-and-feel dependent.

Add this code:

System.out.println(isOpaque());
  • In JComponent case false is printed.
  • In JPanel case true is printed.

That's all.

Call super.paintComponent(g) in paintComponent implementation.

public void paintComponent(Graphics g) {
  super.paintComponent(g);

  Graphics2D g2d = (Graphics2D) g.create();
  g2d.setColor(getColor());
  g2d.fillRect(x, y, 4, 4);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM