gpt4 book ai didi

java - 当我运行程序时,如何使 JFrame 中的圆圈随机化

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

public class SplatPanel extends JPanel
{
private Circle circle1, circle2, circle3, circle4, circle5;
private Rectangle rec1, rec2, rec3;

// Constructor: Creates five Circle objects.

public SplatPanel()
{
circle1 = new Circle (30, Color.red, 70, 35);
circle2 = new Circle (50, Color.green, 30, 20);
circle3 = new Circle (100, Color.cyan, 60, 85);
circle4 = new Circle (45, Color.yellow, 200, 100);
circle5 = new Circle (60, Color.blue, 350, 200);

// (positionX, positionY, color, width, length)

rec1 = new Rectangle (200, 40, Color.GRAY, 90, 70);
rec2 = new Rectangle (150, 250, Color.red, 10, 10);
rec3 = new Rectangle (40, 200, Color.pink, 50, 300);

setPreferredSize (new Dimension(500, 400));
setBackground (Color.black);
}


// Draws this panel by requesting that each circle draw itself.

public void paintComponent (Graphics page)
{
super.paintComponent(page);

circle1.draw(page);
circle2.draw(page);
circle3.draw(page);
circle4.draw(page);
circle5.draw(page);
rec1.draw(page);
rec2.draw(page);
rec3.draw(page);
}

目标是使圆圈随机化,我将如何使圆圈随机化,以便当我运行“Splat”程序时,圆圈将在不同的位置和大小随机化?我有一个 Rectangle 类、一个 Circle 类、一个 Splat 类,然后是 SplatPanel 类。我只是假设我会在 SplatPanel 类中使用随机发生器

最佳答案

问题是您正在使用 Circles 的硬编码值。相反,您可以使用 Random 随机化值类

Random random = new Random();
int randX = random.nextInt(500); // or whatever the width of your panel is
int randY = random.nextInt(400); // or whatever the height of your panel is
int randRadius = random.nextInt(100); // max radius
Circle circle = new Circle(randRadius, Color.BLUE, randX, randY);

您还可以使用List<Circle>将圆圈添加到 paintComponent 中并循环遍历它们方法(这很常见)。

这是一个完整的运行示例

import java.awt.Color;
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.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SplatPanel extends JPanel {

private static final int D_W = 500;
private static final int D_H = 500;

private List<Color> colors;
private List<Circle> circles;
Random random = new Random();

public SplatPanel() {
colors = createColorList();
circles = new ArrayList<>();

Timer timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int randX = random.nextInt(D_W); // or whatever the width of your panel is
int randY = random.nextInt(D_H); // or whatever the height of your panel is
int randRadius = random.nextInt(100); // max radius
Color color = colors.get(random.nextInt(colors.size()));
Circle circle = new Circle(randRadius, color, randX, randY);
circles.add(circle);
repaint();
}
});
timer.start();
}

private List<Color> createColorList() {
List<Color> list = new ArrayList<>();
list.add(Color.BLUE);
list.add(Color.CYAN);
list.add(Color.PINK);
list.add(Color.ORANGE);
list.add(Color.GREEN);
list.add(Color.MAGENTA);
list.add(Color.YELLOW);
list.add(Color.RED);
return list;
}

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

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

public class Circle {

int radius, x, y;
Color color;

public Circle(int radius, Color color, int x, int y) {
this.radius = radius;
this.color = color;
this.x = x;
this.y = y;
}

public void drawCircle(Graphics g) {
g.setColor(color);
g.fillOval(x, y, radius * 2, radius * 2);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new SplatPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

关于java - 当我运行程序时,如何使 JFrame 中的圆圈随机化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22651614/

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