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

时间:2015-04-04 06:32:55

标签: 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);
    }
    }

1 个答案:

答案 0 :(得分:1)

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

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

Random#nextInt(int n)返回int(包括)与指定值0之间的随机n值(不包括)。因此,DIE01 + rand.nextInt(DIE06)将在intDIE01之间返回DIE06值(包括两者)。