Java - 从预先设计的形状中选择一个随机形状

Java - Selecting a random shape from pre-designed shapes

我在从 Java 中预先设计的形状中随机选择形状时遇到问题。以前,我能够创建一个随机形状,但我无处可去。

我的代码如下。我需要 "DIE07" 随机成为 DIE01-DIE06。非常感谢所有帮助。

import java.awt.*;
import javax.swing.JPanel;
import java.util.Random;


public class FigurePanel extends JPanel {
     public static final int DIE01 = 1;
     public static final int DIE02 = 2;
     public static final int DIE03 = 3;
     public static final int DIE04 = 4;
     public static final int DIE05 = 5;
     public static final int DIE06 = 6;
     public static final int DIE07 = 7;
     Random rand = new Random();

     private int type = 1;
     private boolean filled = false;

     public FigurePanel() {
     }

     public FigurePanel(int type) {
     this.type = type;
     }

     @Override
     protected void paintComponent(Graphics g) {
     super.paintComponent(g);

     int width = getWidth();
     int height = getHeight();

     switch (type) {
     case DIE01:
     g.setColor(Color.BLACK);
     g.drawRoundRect((int)(0.1 * width), (int)(0.25 * height),
        (int)(0.8 * width), (int)(0.5 * height), 20, 20);
     g.fillOval((int)(0.44 * width), (int)(0.46 * height),
            (int)(0.1 * width), (int)(0.075 * height));
     break;
   case DIE02:
      g.setColor(Color.RED);
      g.drawRoundRect((int)(0.1 * width), (int)(0.25 * height),
          (int)(0.8 * width), (int)(0.5 * height), 20, 20);
      g.fillOval((int)(0.27 * width), (int)(0.34 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      g.fillOval((int)(0.63 * width), (int)(0.58 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      break;
  case DIE03:
      g.setColor(Color.GREEN);
      g.drawRoundRect((int)(0.1 * width), (int)(0.25 * height),
          (int)(0.8 * width), (int)(0.5 * height), 20, 20);
      g.fillOval((int)(0.27 * width), (int)(0.34 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      g.fillOval((int)(0.45 * width), (int)(0.46 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      g.fillOval((int)(0.63 * width), (int)(0.58 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      break;
  case DIE04:
      g.setColor(Color.GREEN);
      g.drawRoundRect((int)(0.1 * width), (int)(0.25 * height),
          (int)(0.8 * width), (int)(0.5 * height), 20, 20);
      g.fillOval((int)(0.27 * width), (int)(0.34 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      g.fillOval((int)(0.27 * width), (int)(0.58 * height),
              (int)(0.1 * width), (int)(0.075 * height)); 
      g.fillOval((int)(0.63 * width), (int)(0.58 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      g.fillOval((int)(0.63 * width), (int)(0.34 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      break;
  case DIE05:
      g.setColor(Color.RED);
      g.drawRoundRect((int)(0.1 * width), (int)(0.25 * height),
          (int)(0.8 * width), (int)(0.5 * height), 20, 20);
      g.fillOval((int)(0.27 * width), (int)(0.34 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      g.fillOval((int)(0.27 * width), (int)(0.58 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      g.fillOval((int)(0.45 * width), (int)(0.46 * height),
              (int)(0.1 * width), (int)(0.075 * height));   
      g.fillOval((int)(0.63 * width), (int)(0.58 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      g.fillOval((int)(0.63 * width), (int)(0.34 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      break;
  case DIE06:
      g.setColor(Color.BLACK);
      g.drawRoundRect((int)(0.1 * width), (int)(0.25 * height),
          (int)(0.8 * width), (int)(0.5 * height), 20, 20);
      g.fillOval((int)(0.27 * width), (int)(0.34 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      g.fillOval((int)(0.27 * width), (int)(0.58 * height),
              (int)(0.1 * width), (int)(0.075 * height)); 
      g.fillOval((int)(0.27 * width), (int)(0.46 * height),
              (int)(0.1 * width), (int)(0.075 * height)); 
      g.fillOval((int)(0.63 * width), (int)(0.46 * height),
              (int)(0.1 * width), (int)(0.075 * height)); 
      g.fillOval((int)(0.63 * width), (int)(0.58 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      g.fillOval((int)(0.63 * width), (int)(0.34 * height),
              (int)(0.1 * width), (int)(0.075 * height));
      break;
  case DIE07:

      break;
}

   }
    public void setType(int type) {
    this.type = type;
    repaint();
   }

    public int getType() {
    return type;
   }

    public void setFilled(boolean filled) {
    this.filled = filled;
    repaint();
    }

    public boolean isFilled() {
    return filled;
    }

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

还有这个……

    import java.awt.*;
    import javax.swing.*;

    public class TestFigurePanel extends JFrame {
    public TestFigurePanel() {
    setLayout(new GridLayout(1, 7, 1, 1));
    add(new FigurePanel(FigurePanel.DIE01));
    add(new FigurePanel(FigurePanel.DIE02));
    add(new FigurePanel(FigurePanel.DIE03));
    add(new FigurePanel(FigurePanel.DIE04));
    add(new FigurePanel(FigurePanel.DIE05));
    add(new FigurePanel(FigurePanel.DIE06));
    add(new FigurePanel(FigurePanel.DIE07));
    }

    public static void main(String[] args) {
    TestFigurePanel frame = new TestFigurePanel();
    frame.setSize(1100, 300);
    frame.setTitle("TestFigurePanel");
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }

如果传递的参数是 DIE07,您可以使用 Random 成员实例随机实例化 type。如果没有,您直接将其分配给您的 type

public FigurePanel(int type) {
  if (type == DIE07) {
    this.type = DIE01 + rand.nextInt(DIE06);
  } else {
    this.type = type;
  }
}

Random#nextInt(int n) return 是介于 0(含)和指定值 n(不含)之间的随机 int 值。因此,DIE01 + rand.nextInt(DIE06) 将 return 一个 int 值介于 DIE01DIE06(包括两者)之间。