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

标签 java random 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。如果没有,您可以直接将其分配给您的类型

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

Random#nextInt(int n) 返回 0(含)和指定值 n< 之间的随机 int(独家)。因此,DIE01 + rand.nextInt(DIE06) 将返回 DIE01DIE06 之间的 int 值(包括两者) )。

关于Java - 从预先设计的形状中选择随机形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29443495/

相关文章:

java - 玩!框架 CRUD 模块 : adding default values and change date format?

java - 不同文件中 Spring bean 的 Grails 配置

java - 如何对自定义 Wicket 组件进行单元测试

ios - (iOS) 如何使用 shapeLayer 为圆角矩形制作动画?

r - 是否可以在组合线图和点图上更改颜色和形状,同时保留每个图表一个图例?

Java 矩阵乘法产生相同的矩阵

go - 在 go 中生成一个随机 boolean 值

javascript - 在 JavaScript 中获取范围随机数的最佳方法?

javascript - jQuery Math.random : avoid repeating same item

python - 形状分析以区分矩形与其他形状