vendredi 3 avril 2015

Java - Selecting a random shape from pre-designed shapes

I'm having a problem randomly selecting a shape from pre-designed shapes in Java. Previously, I was able to create a random shape but I'm getting no-where with this.


My code is below. I need "DIE07" to randomly be DIE01-DIE06. All help is very much appreciated.



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


and this...



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




Aucun commentaire:

Enregistrer un commentaire