【问题标题】:How to make circles randomize in a JFrame when I run the program运行程序时如何在 JFrame 中使圆圈随机化
【发布时间】:2014-03-26 04:40:32
【问题描述】:
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 类中使用随机发生器

【问题讨论】:

    标签: java swing random jpanel geometry


    【解决方案1】:

    问题是您正在为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);
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      • 2015-07-04
      • 2013-12-25
      • 2013-10-24
      • 2019-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多