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