Swing中的重绘机制:Paint-Repaint

3

我在Swing Java中的Paint-Repaint机制遇到了问题。

我希望创建一个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以便我看不见它,当它重新绘制时。
非常感谢您的帮助。

目前,您可以假设:在“组件”上调用“getGraphics”始终是错误的。(虽然有些情况下这是可行的,但您几乎永远不会遇到它们) - Marco13
2个回答

1

您需要编写一个JPanel的扩展类,它覆盖了paint(Graphics g)或者更好的是paintComponent(Graphics g)。在重写的方法中,您将需要调用drawRects方法。


1

请参见自定义绘图方法,了解使用以下内容进行自定义绘图的示例:

  1. 要绘制的对象列表
  2. 要绘制对象的BufferedImage。

这些示例将绘制不同颜色的矩形。因此,您需要添加代码以绘制不同的形状。但是首先要理解基本的绘图概念。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接