gpt4 book ai didi

java - 单击按钮将形状添加到 JPanel

转载 作者:搜寻专家 更新时间:2023-11-01 01:51:50 24 4
gpt4 key购买 nike

我有一个带按钮的类圆和一个带 jPanel 的类,我想做的是单击该按钮时,将在面板上绘制一个圆圈,每次我单击该按钮并更改 x 和 y“一些如何不在这里实现”我一遍又一遍地在 JPanel 上画圈。如何做到这一点,或者有没有办法不管我的代码如何做我描述的事情,但我希望类圈子扩展 Shape。

public class Window{

private JFrame frame;
private JPanel panel = new JPanel();
Circle c = new Circle(frame, panel);
// some other buttons
.
.
// some code to set the panel grid bag constaraints and background then
frame.getContentPane().add(panel, gbc_panel);
}

然后是 Circle 类

public class Circle extends Shape implements ActionListener{

private JPanel Panel;
private GridBagConstraints gbc_btnCircle;
private JButton btnCircle;

public void setPanel(JPanel panel) {
Panel = panel;
}

public Circle(JFrame frame, JPanel panel){
btnCircle = new JButton("Circle");
// some code to set grid bag constraint then
frame.getContentPane().add(btnCircle, gbc_btnCircle);
setPanel(panel);
btnCircle.addActionListener(this);

}

public void paint(Graphics g) {
super.paintComponents(g);
g.setColor(Color.red);
g.fillOval(100, 100, 100, 100);
Panel.add(this);
}

public void actionPerformed(ActionEvent arg0) {
repaint();
}
}

最佳答案

你有点误会了。在您的绘图面板中,您应该有一个 List<Circle> .而在 paintComponent绘图面板的方法,你应该遍历列表来绘制每个圆

class Circle {
int x, int y, int width, int height;
public Circle (int x, int y, int width, int height) {
... set em
}
public void draw(Graphics g) {
g.fillOval(x, y, width, height);
}
}
class DrawingPanel extends JPanel {
List<Circle> circles = new ArrayList<>();

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Circle circle : circles) {
circle.draw(g);
}
}

// Dont forget to override public Dimension getPreferredSize()
}

要将更多圈子添加到列表中,只需使用 addCircle DrawingPanel 中的方法类

public void addCircle(Circle circle) {
circles.add(circle);
repaint();
}

就按钮而言,您应该在 Window 中创建它类(class)。在ActionListener , 只需创建一个 new Circle并添加 DrawingPanel通过调用 addCircle方法


题外话,Circle不需要扩展 Shape . Shape API 已经有一个 Ellipse2D 类,您可以从中创建圈子

class DrawingPanel extends JPanel {
List<Ellipse2D> circles = new ArrayList<>();

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g.create();
for (Ellipse2D circle : circles) {
g2.fill(circle);
}
g2.dispose();
}
// Dont forget to override public Dimension getPreferredSize()
}

更新:完整示例

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class CirclesDemo {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new CirclesDemo();
}
});
}

public CirclesDemo() {
JFrame frame = new JFrame();
frame.add(panel);
frame.add(createButton(), BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private final DrawingPanel panel = new DrawingPanel();

private JButton createButton() {
JButton button = new JButton("Add Circle");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int[] circleValues = generateRandomValues(300, 300, 50, 150);
int x = circleValues[0];
int y = circleValues[1];
int width = circleValues[2];
int height = width;
Circle circle = new Circle(x, y, width, height);
panel.addCircle(circle);
}
});
return button;
}

private int[] generateRandomValues(int maxX, int maxY,
int minSize, int maxSize) {
Random random = new Random();
int[] values = new int[3];
values[0] = random.nextInt(maxX);
values[1] = random.nextInt(maxY);
values[2] = Math.min(random.nextInt(maxSize) + minSize, maxSize);
return values;
}

class Circle {

int x, y, width, height;

public Circle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

public void draw(Graphics g) {
g.drawOval(x, y, width, height);
}
}

class DrawingPanel extends JPanel {

List<Circle> circles = new ArrayList<>();

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Circle circle : circles) {
circle.draw(g);
}
}

public void addCircle(Circle circle) {
circles.add(circle);
repaint();
}

@Override
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
}
}

关于java - 单击按钮将形状添加到 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26678649/

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