gpt4 book ai didi

java - 在 JPanel 上显示所有绘制的形状

转载 作者:行者123 更新时间:2023-12-02 07:41:16 26 4
gpt4 key购买 nike

我正在使用 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 包含按下的按钮标题。

最佳答案

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

演示代码:

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

});
}
}

关于java - 在 JPanel 上显示所有绘制的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16294556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com