Swing中的Paint-重绘机制

时间:2014-01-24 15:25:22

标签: java swing jpanel paint repaint

我在Swing java中遇到了Paint-Repaint-Mechanism的问题:

我想创建GraphicEditor,它可以用鼠标创建一个矩形和形状。

应用程序应如下所示

http://picload.org/view/lrdwpad/ghh.png.html

我的代码看起来像这样:

public class MiniGrafikEditor extends JFrame implements ActionListener {
    private Vector rectList;

    private Rectangle currentRect;
    private Color color = Color.green;

    private static int v = 0 ;
    JPanel bp;

    public static void main(String[] args) {
        MiniGrafikEditor wnd = new MiniGrafikEditor();
    }

    public MiniGrafikEditor() {
        super("Rechtecke zeichnen");

        rectList = new Vector();
        currentRect = new Rectangle(0, 0, 0, 0);

        setLayout(new BorderLayout());

        addWindowListener(new MyWindowListener());
        addMouseListener(new MyMouseListener());
        addMouseMotionListener(new MyMouseMotionListener());

        bp = new JPanel();
        bp.setBackground(Color.gray);
        add("North", bp);

        JRadioButton b = null;
        bp.add(b = new JRadioButton("Rechteck"));
        b.addActionListener(this);
        bp.add(b = new JRadioButton("kreis"));
        b.addActionListener(this);
        bp.add(b = new JRadioButton("Standard"));
        b.addActionListener(this);

        setLocation(200, 200);
        setSize(400, 300);
        setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        String label = ((JRadioButton) e.getSource()).getLabel();
        bp.repaint();

        if (label.equals("Rechteck")) {

            v=1;
            bp.repaint();
        }
        if (label.equals("Blue")) {
            color = Color.blue;
            bp.repaint();
        }
        Graphics g = getGraphics();
        drawRects(g);
    }

    public void drawRects(Graphics g) {
        Rectangle r;
        Enumeration e;

        g.clearRect(0, 0, getSize().width, getSize().height);
        g.setColor(color);

        for (e = rectList.elements(); e.hasMoreElements();) {
            r = (Rectangle) e.nextElement();
            g.drawRect(r.x, r.y, r.width, r.height);
        }

        if (currentRect != null && (currentRect.x > 0 || currentRect.y > 0)) {
            g.drawRect(currentRect.x, currentRect.y, currentRect.width,
                    currentRect.height);

        }
        bp.repaint();
    }

    class MyMouseListener extends MouseAdapter {
        public void mousePressed(MouseEvent event) {
            bp.repaint();
            if(v==1){
            currentRect = new Rectangle(event.getX(), event.getY(), 0, 0);
            }

        }

        public void mouseReleased(MouseEvent event) {

            if(v==1){

            if (currentRect.width > 0 || currentRect.height > 0) {
                rectList.addElement(currentRect);
                currentRect = null;
            }
            Graphics g = getGraphics();
            drawRects(g);
            }
            bp.repaint();
        }
    }

    class MyMouseMotionListener extends MouseMotionAdapter {

        public void mouseDragged(MouseEvent event) {

            if(v==1){
            int x = event.getX();
            int y = event.getY();
            if (x > currentRect.x && y > currentRect.y) {
                currentRect.width = x - currentRect.x;
                currentRect.height = y - currentRect.y;
            }

            Graphics g = getGraphics();
            drawRects(g);

            }

        }
    }

    class MyWindowListener extends WindowAdapter {  
        public void windowClosing(WindowEvent event) {
            setVisible(false);
            dispose();
            System.exit(0);
        }
    }
}

当我运行应用程序时,它看起来像这样:

http://picload.org/view/lrdwpac/unbenannt.png.html

当我尝试绘制一个矩形时,我可以看到Jpanel重新绘制它的时间。

我怎么能重新绘制Jpanel,当它重新粉刷时我无法看到它。

对你的帮助很重要。

2 个答案:

答案 0 :(得分:1)

您必须编写JPanel的扩展名,其覆盖paint(Graphics g)或更好paintComponent(Graphics g)。在该方法的重写版本下,您必须调用drawRects方法。

答案 1 :(得分:1)

有关如何使用以下方式进行自定义绘画的示例,请参阅Custom Painting Approaches

  1. 要绘制的对象列表
  2. 用于绘制对象的BufferedImage。
  3. 示例将绘制不同颜色的矩形。因此,您需要添加代码来绘制不同的形状。但我们的想法是先了解基本的绘画概念。