为什么我们需要重新绘制JPanel?

时间:2015-01-09 08:05:27

标签: java swing graphics 2d

问题是你画这样的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时,它会绘制一个青色矩形和一个红色椭圆。 问题是,如果你想移动椭圆形,例如,你必须重新绘制整个面板!

如何制作2D物体并使其可移动和可更改而无需重新绘制整个面板?

2 个答案:

答案 0 :(得分:2)

首先看看Painting in AWT and SwingPerforming Custom Painting,了解绘画过程。

你没有"有"如果您不想重新绘制整个面板,可以使用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();
        }

    }

}

答案 1 :(得分:1)

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