为什么我们需要重绘 JPanel?

Why We Need To Repaint JPanel?

问题是当您像这样绘制 JPanel 时:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor (Color.CYAN);
    g.fillRect(10,10,200,50);
    g.setColor(Color.RED);
    g.fillOval(50,50,50,50);
}

因此,如果在创建 JPanel 时绘制一个青色矩形和一个红色椭圆形。 问题是如果你想移动椭圆,你必须重新绘制整个面板!

如何在不重绘整个面板的情况下制作一个二维对象并使其可移动和可变?

先看看Painting in AWT and Swing and Performing Custom Painting 了解绘画过程。

如果您不想"have"重绘整个面板,您可以使用JPanel#repaint(int, int, int, int)来指定您想要重绘的区域。

现在,请记住,如果您需要绘制用于包含椭圆形的区域,然后绘制它现在所在的区域,这将确保先前的位置也得到更新,否则您会出现重影...

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testball;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestBall {

    public static void main(String[] args) {
        new TestBall();
    }

    public TestBall() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private int x = 100 - 10;
        private int y = 100 - 10;
        private int delta = 4;

        public TestPane() {
            Timer timer = new Timer(40, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // Replace existing area...
                    repaint(x, y, 21, 21);

                    x += delta;
                    if (x + 20 >= getWidth()) {
                        x = getWidth() - 20;
                        delta *= -1;
                    } else if (x < 0) {
                        x = 0;
                        delta *= -1;
                    }
                    repaint(x, y, 21, 21);
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(Color.RED);
            g2d.drawOval(x, y, 20, 20);
            g2d.dispose();
        }

    }

}

如果您的对象处于新的顺序,则必须重新绘制。如果您最小化和最大化 window,默认情况下也会调用重绘。制作在移动时自动更新面板的 2D 对象的一种可能性是使用观察者模式: http://en.wikipedia.org/wiki/Observer_pattern