运行程序时如何在JFrame中使圆随机化 [英] How to make circles randomize in a JFrame when I run the program

查看:103
本文介绍了运行程序时如何在JFrame中使圆随机化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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类中使用随机化器

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

推荐答案

问题是您正在为Circles使用硬编码值.相反,您可以使用Random

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

您还可以使用List<Circle>将圆添加到圆中,并通过paintComponent方法循环(很常见).

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

这是一个完整的例子

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

这篇关于运行程序时如何在JFrame中使圆随机化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆