gpt4 book ai didi

java - 在 JAVA GUI 中添加随机形状矩形、椭圆形和线条

转载 作者:行者123 更新时间:2023-12-02 12:02:35 25 4
gpt4 key购买 nike

嘿,我正在尝试设计一个 GUI 程序 ShapeFactory,它具有要生成的按钮:

a.具有随机颜色的随机矩形。

b.具有随机颜色的随机椭圆。

c.具有随机颜色的随机线。

每当用户单击按钮时,相应的形状就会添加到 JFrame 中已绘制的形状中。

我一直在尝试将 ActionListener 与绘制方法连接起来

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ShapeFactory extends JFrame implements ActionListener{

public static void main(String[] args) {
new ShapeFactory();
}

JButton ranRects = new JButton("Random Rectangles");
JButton ranElli = new JButton("Random Ellipses");
JButton ranLines = new JButton("Random Lines");
Graphics g;
Color c;
ArrayList shapeList = new ArrayList( );

public ShapeFactory(){
super("Shape Factory");
setLayout(new FlowLayout());
setSize(600,400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

add(ranRects);
add(ranElli);
add(ranLines);

JPanel nPanel = new JPanel (new GridLayout(1,3));
nPanel.add(ranRects);
nPanel.add(ranElli);
nPanel.add(ranLines);

add(nPanel);


ranRects.addActionListener(this);

ranElli.addActionListener(this);

ranLines.addActionListener(this);


setVisible(true);
}

public void paint (Graphics g) {

super.paint(g);

if (d == 1) {
paintRect(g);
shapeList.add(d);
}

else if (d == 2) {
paintOval(g);
shapeList.add(d);
}

else if (d == 3 ) {
paintLine(g);
shapeList.add(d);
}
}

public void paintRect(Graphics g) {

int rectX1 = (int)(Math.random() * getWidth() / 4.0);
int rectY1 = (int)(Math.random() * getHeight()/ 4.0);
int rectX2 = (int)(Math.random() * getWidth());
int rectY2 = (int)(Math.random() * getHeight());
c = new Color( (int) (Math.random() * 255) , (int) (Math.random() * 255) , (int) (Math.random() * 255));
g.setColor(c);
g.fillRect(rectX1,rectY2,rectX2,rectY2);
}
public void paintOval(Graphics g) {

int ciX1 = (int)(Math.random() * getWidth() / 4.0);
int ciY1 = (int)(Math.random() * getHeight()/ 4.0);
int ciX2 = (int)(Math.random() * getWidth());
int ciY2 = (int)(Math.random() * getHeight());
c = new Color( (int) (Math.random() * 255) , (int) (Math.random() * 255) , (int) (Math.random() * 255));
g.setColor(c);
g.fillOval(ciX1,ciY1,ciX2,ciY2);
}
public void paintLine(Graphics g) {

int lineX1 = (int)(Math.random() * getWidth() / 4.0);
int lineY1 = (int)(Math.random() * getHeight()/ 4.0);
int lineX2 = (int)(Math.random() * getWidth());
int lineY2 = (int)(Math.random() * getHeight());
c = new Color( (int) (Math.random() * 255) , (int) (Math.random() * 255) , (int) (Math.random() * 255));
g.setColor(c);
g.drawLine(lineX1,lineY1,lineX2,lineY2);
}

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();

if (source == ranRects) {
d = 1;
}
else if (source == ranElli) {
d = 2;
}
else if (source == ranLines) {
d = 3;

}
repaint();

}

}

最佳答案

因此,一种解决方案是使用 3 个成员变量来跟踪每种类型需要绘制多少个形状。单击按钮后,您只需增加相应的变量,然后在 paint 方法中多次调用 paintOval 或任何形状。在这种情况下,您的 paint 方法可能如下所示:

public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < this.numberOfRects; i++)
paintRect(g);
for (int i = 0; i < this.numberOfEllis; i++)
paintOval(g);
for (int i = 0; i < this.numberOfLines; i++)
this.paintLine(g);
}

actionPerformed 像这样:

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == ranRects) {
this.numberOfRects++;
} else if (source == ranElli) {
this.numberOfEllis++;
} else if (source == ranLines) {
this.numberOfLines++;
}
repaint();
}

当然,您需要将 3 个成员变量添加到类中。

<小时/>

如果您想存储形状位置和颜色,我建议创建一个新类将它们存储在某种List中。您可以创建一个内部类 Shape 来存储形状属性,例如位置、大小和颜色。

private class Shape{
public int x1;
public int y1;
public int x2;
public int y2;
public Color color;
}

然后您可以为每种类型的形状添加成员变量:

private List<Shape> rectList = new ArrayList<>();
private List<Shape> ovalList = new ArrayList<>();
private List<Shape> lineList = new ArrayList<>();

我会推荐一种添加形状的方法,如下所示:

public void addRect(){
//Create new Shape
Shape shape = new Shape();

//Set shape's properties
shape.x1 = (int) (Math.random() * getWidth() / 4.0);
shape.x2 = (int) (Math.random() * getHeight() / 4.0);
shape.y1 = (int) (Math.random() * getWidth());
shape.y2 = (int) (Math.random() * getHeight());
shape.color = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));

//Add the shape to the list
this.rectList.add(shape);
}

并调用actionListener中的方法

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == ranRects) {
addRect();
} else if (source == ranElli) {
addOval();
} else if (source == ranLines) {
addLine();
}
repaint();
}

要绘制它们,您只需迭代每种形状类型的列表即可:

public void paint(Graphics g) {
super.paint(g);
for (Shape s: rectList )
paintRect(g, s);
//...
//Add loops for all shapes
}

public void paintRect(Graphics g, Shape shape) {
g.setColor(shape.color);
g.fillRect(shape.x1, shape.y1, shape.x2, shape.y2);
}

最后你应该得到这样的结果:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;

public class ShapeFactory extends JFrame implements ActionListener {

public static void main(String[] args) {
new ShapeFactory();
}

JButton ranRects = new JButton("Random Rectangles");
JButton ranElli = new JButton("Random Ellipses");
JButton ranLines = new JButton("Random Lines");
private List<Shape> rectList = new ArrayList<>();
private List<Shape> ovalList = new ArrayList<>();
private List<Shape> lineList = new ArrayList<>();

private class Shape{
public int x1;
public int y1;
public int x2;
public int y2;
public Color color;
}

public ShapeFactory() {
super("Shape Factory");
setLayout(new FlowLayout());
setSize(600, 400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

add(ranRects);
add(ranElli);
add(ranLines);

JPanel nPanel = new JPanel(new GridLayout(1, 3));
nPanel.add(ranRects);
nPanel.add(ranElli);
nPanel.add(ranLines);

add(nPanel);


ranRects.addActionListener(this);

ranElli.addActionListener(this);

ranLines.addActionListener(this);


setVisible(true);
}

public void paint(Graphics g) {
super.paint(g);
for (Shape s: rectList )
paintRect(g, s);
//...
//Add loops for all shapes
}

public void paintRect(Graphics g, Shape shape) {
g.setColor(shape.color);
g.fillRect(shape.x1, shape.y1, shape.x2, shape.y2);
}
//...
//Add paint methods for oval and lines

public void addRect(){
//Create new Shape
Shape shape = new Shape();

//Set shape's properties
shape.x1 = (int) (Math.random() * getWidth() / 4.0);
shape.x2 = (int) (Math.random() * getHeight() / 4.0);
shape.y1 = (int) (Math.random() * getWidth());
shape.y2 = (int) (Math.random() * getHeight());
shape.color = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
//Add the shape to the list

this.rectList.add(shape);
}
//...
//Add methods to add ovals and lines


public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == ranRects) {
addRect();
} else if (source == ranElli) {
//addOval();
} else if (source == ranLines) {
//addLine();
}
repaint();
}
}

我只实现了矩形的 paintadd 方法,但添加椭圆形和线条并不难。希望这有帮助

关于java - 在 JAVA GUI 中添加随机形状矩形、椭圆形和线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47136308/

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