How to make circles randomize in a JFrame when I r

2019-06-09 04:41发布

问题:

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

The objective is to make the circles randomize, how would I go about to randomize the circles so when I run the "Splat" program the circles will randomize in different places and sizes? I have a Rectangle class, a Circle class, a Splat class and then the SplatPanel class. I'm only assuming I would be using the randomizer in the the SplatPanel class

回答1:

The problem is you are using hard coded values for the Circles. Instead you can randomize the values using the Random class

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

You could also use a List<Circle> to add the circles to and just loop through them in the paintComponent method (which is pretty common).

Here's a full running example

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