使用getGraphics进行闪烁绘画

时间:2014-01-15 17:47:10

标签: java swing paint paintcomponent flicker

我正在做一个选择工具而且我已经提供了这些解决方案,在第一个使用JPanel getGraphics方法时,我在拖动鼠标的任何地方画一个椭圆,在第二个我重写paintComponent方法来绘制椭圆。

当我执行第一个时,拖动鼠标时闪烁,绘图很差,而第二个方法运行完美。为什么会发生这种情况?如果我在第一个解决方案中退出重绘它会绘制椭圆并且不会“删除”它们。

做这样的事情的最佳做法是什么?我在使用getGraphics绘制时遗漏了什么?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class TestGetGraphics extends JFrame {

    JPanel canvas;
    Painter painter;

    class Painter{
        void paint( Graphics2D g, Point p ){
            g.drawOval(p.x, p.y, 30, 30);
        }
    }

    public TestGetGraphics(){
        super();

        canvas = new JPanel();
        painter = new Painter();

        canvas.setPreferredSize( new Dimension(400, 400) );
        canvas.setBackground(Color.WHITE);

        canvas.addMouseMotionListener(new MouseAdapter() {
                public void mouseDragged(MouseEvent me){
                    painter.paint((Graphics2D) canvas.getGraphics(), me.getPoint());
                    canvas.repaint();
                }
        });

        add( canvas );

        setVisible(true);
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }


    public static void main(String [] args){
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestGetGraphics();
            }
        });
    }

}

这是paintComponent方法:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class TestPaintComponent extends JFrame  {
    JPanel canvas;
    Painter painter;

    class Painter{
        Point p;
        void paint( Graphics2D g ){
            if( p != null )
                g.drawOval(p.x, p.y, 30, 30);
        }

        void setPoint( Point p ){
            this.p = p;
        }
    }

    public TestPaintComponent(){
        super();

        canvas = new JPanel(){
            @Override
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                painter.paint((Graphics2D) g);

            }

        };
        painter = new Painter();

        canvas.setPreferredSize( new Dimension(400, 400) );
        canvas.setBackground(Color.WHITE);

        canvas.addMouseMotionListener(new MouseAdapter() {
                public void mouseDragged(MouseEvent me){
                    painter.setPoint(me.getPoint());
                    canvas.repaint();
                }
        });

        add( canvas );

        setVisible(true);
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }



    public static void main(String [] args){
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestPaintComponent();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:2)

不要在组件上调用getGraphics()。相反,扩展组件并覆盖paintComponent()。更多信息:http://docs.oracle.com/javase/tutorial/uiswing/painting/