0

我正在使用 JButton 的 Action 侦听器来绘制不同的形状。它工作正常,但如何始终在面板上保留以前绘制的形状?因为当另一个按钮按下之前的形状已经消失了。

jButton1.setText("Button1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

jButton2.setText("Button2");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    s = evt.getActionCommand();
    repaint();

}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    s = evt.getActionCommand();
    repaint();

}

.......而paintComponent方法是

 protected void paintComponent(Graphics g) {
        super.paintComponent(g);


        System.out.println("====>>> " + s);
        switch (s) {

            case "Button1":
                g.drawRoundRect(20,20,40,40,100,200);
                break;

            case "Button2":
                g.drawRect(0, 0, 200, 200);
                break;

            default:
                g.drawOval(40, 40, 100, 100);

这里 String 包含按下按钮的标题。

4

3 回答 3

6

您可以简单地绘制到缓冲图像并显示该图像。

演示代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

class TestPaint {

    private BufferedImage image;
    private JLabel drawing;

    private int x = 0;
    private int y = 0;

    protected void initUI() {
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton jButton1 = new JButton();
        JButton jButton2 = new JButton();
        jButton1.setText("Button1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Button2");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        image = new BufferedImage(500, 300, BufferedImage.TYPE_INT_RGB);
        image.getGraphics().setColor(Color.WHITE);
        image.getGraphics().fillRect(0, 0, image.getWidth(), image.getHeight());
        drawing = new JLabel(new ImageIcon(image));
        JPanel bottomPanel = new JPanel(new FlowLayout());
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
        buttonPanel.add(jButton1);
        buttonPanel.add(jButton2);
        bottomPanel.add(buttonPanel);
        jFrame.add(drawing);
        jFrame.add(bottomPanel, BorderLayout.SOUTH);
        jFrame.pack();
        jFrame.setVisible(true);
    }

    private Graphics getImageGraphics() {
        return image.getGraphics();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        Graphics g = getImageGraphics();
        g.setColor(Color.GREEN);
        g.drawRoundRect(x, y, 40, 40, 100, 200);
        drawing.repaint();
        x += 5;
        y += 5;
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        Graphics g = getImageGraphics();
        g.setColor(Color.BLUE);
        g.drawRect(x, y, 200, 200);
        drawing.repaint();
        x += 5;
        y += 5;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestPaint().initUI();
            }

        });
    }
}
于 2013-04-30T07:46:06.290 回答
5

这些中的任何一个都应该这样做:

  • 将所有绘图操作存储在列表中并在绘制时,迭代列表并将它们全部绘制。

  • 将形状绘制到 aBufferedImage并在标签中显示图像。EG 如本答案所示:

于 2013-04-30T07:36:46.317 回答
3

自定义绘画方法展示了两种常见的方法:

  1. 绘制列表中包含的对象
  2. 绘制一个包含所有对象的 BufferedImage。
于 2013-04-30T16:02:01.893 回答