Java 为什么我们需要重新粉刷JPanel?

Java 为什么我们需要重新粉刷JPanel?,java,swing,graphics,2d,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时,它会绘制一个青色矩形和一个红色椭圆形。 问题是,如果

问题是,当您像这样绘制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对象并使其可移动和可更改,而无需重新绘制整个面板?

首先查看并了解绘制过程

如果不想重新绘制整个面板,您不必“必须”重新绘制,您可以使用它来指定要重新绘制的区域

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

/*
 * 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();
        }

    }

}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装测试球;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.Timer;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类测试球{
公共静态void main(字符串[]args){
新的测试球();
}
公共测试球(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
私人整数x=100-10;
私人整数y=100-10;
私有整数增量=4;
公共测试窗格(){
计时器计时器=新计时器(40,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
//替换现有区域。。。
重新喷漆(x、y、21、21);
x+=δ;
如果(x+20>=getWidth()){
x=getWidth()-20;
delta*=-1;
}else如果(x<0){
x=0;
delta*=-1;
}
重新喷漆(x、y、21、21);
}
});
timer.start();
}
@凌驾
公共维度getPreferredSize(){
返回新维度(200200);
}
@凌驾
受保护组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g.create();
g2d.setColor(Color.RED);
g2d.drawOval(x,y,20,20);
g2d.dispose();
}
}
}
首先看一看并理解绘画过程

如果不想重新绘制整个面板,您不必“必须”重新绘制,您可以使用它来指定要重新绘制的区域

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

/*
 * 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();
        }

    }

}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装测试球;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.Timer;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类测试球{
公共静态void main(字符串[]args){
新的测试球();
}
公共测试球(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
私人整数x=100-10;
私人整数y=100-10;
私有整数增量=4;
公共测试窗格(){
计时器计时器=新计时器(40,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
//替换现有区域。。。
重新喷漆(x、y、21、21);
x+=δ;
如果(x+20>=getWidth()){
x=getWidth()-20;
delta*=-1;
}else如果(x<0){
x=0;