【问题标题】:Paint- Repaint-mechanism in SwingSwing 中的重绘机制
【发布时间】:2014-01-24 15:25:21
【问题描述】:

我在 Swing java 中的 Paint-Repaint-Mechanism 有问题。:

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

应用程序应如下所示

我的代码如下所示:

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);
        }
    }
}

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

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

我如何重绘 Jpanel,但当它自己重绘时我看不到它。

非常感谢您的帮助。

【问题讨论】:

  • 现在,您可以假设:在Component 上调用getGraphics 总是错误的。 (在某些情况下这是可行的,但您几乎不会遇到)

标签: java swing jpanel paint repaint


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    请参阅Custom Painting Approaches,了解如何使用以下方法进行自定义绘画:

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

    这些示例将绘制不同颜色的矩形。所以你需要添加代码来绘制不同的形状。但想法是先了解基本的绘画概念。

    【讨论】:

      猜你喜欢
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多