我需要创建一个程序,随机滚动5个骰子,然后在JPanel中显示这些骰子的面孔,就像Yahtzee游戏一样。我有每个模具面的图像,我很难将我随机卷起的数字附加到图像上。因此,例如,如果随机数为1,则它将显示五个不同面的模面1。我下面的内容是我的Die类,Panel类和我的GameDrive。很抱歉复制了所有代码,不确定是否需要驱动程序来解决此问题,但dint知道需要编写代码的位置。
public class Die implements Comparator {
private int face;
public Die() {
super();
face = (int)(Math.random() * 6) + 1;
}
public Die(int f){
super();
face = f;
}
public int getFace() {
return face;
}
public void setFace(int face) {
this.face = face;
}
@Override
public String toString() {
return "Die [face=" + face + "]";
}
@Override
public boolean equals(Object obj) {
return (this.getFace() == ((Die)obj).getFace());
}
@Override
public int compare(Object a, Object b) {
Die d1 = (Die)a;
Die d2 = (Die)b;
if (d1.getFace() == d2.getFace())
return 0;
if (d1.getFace() > d2.getFace())
return 1;
return -1;
}
}
小组类
public class Panel extends JPanel {
private BufferedImage [] img = new BufferedImage [6];
private int w = 0, h = 0, xloc = 0, yloc = 0;
public Panel() {
super();
try {
img[0] = ImageIO.read(new File("Die1.jpg"));
img[1] = ImageIO.read(new File("Die2.jpg"));
img[2] = ImageIO.read(new File("Die3.jpg"));
img[3] = ImageIO.read(new File("Die4.jpg"));
img[4] = ImageIO.read(new File("Die5.jpg"));
img[5] = ImageIO.read(new File("Die6.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
this.setPreferredSize(new Dimension(1300, 600));
w = img[0].getWidth();
h = img[0].getHeight();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
xloc = 0;
yloc = 0;
for (BufferedImage i : img)
{
g.drawImage(i, xloc, yloc, w, h, null);
xloc += w;
}
yloc += h;
xloc = 0;
for (int i = 0; i < img.length; i++)
{
g.drawImage(img[i], xloc, yloc, w, h, null);
xloc += w;
}
}
}
驱动器
public class GameDriver {
public static void main(String[] args) {
Frame f = new Frame();
ArrayList<Die> roll = new ArrayList<Die>(5);
for (int i =0; i < 5; i++)
roll.add(new Die());
roll.sort(roll.get(0));
for (Die e : roll)
System.out.println(e);
}
}
答案 0 :(得分:0)
创建Panel时,需要向其传递对要显示的骰子的引用。
(我认为这就是你要问的)
你可以在构造函数中传递一些Die值,然后在你的引用中绘制它们&#34; paint&#34;方法,例如......
public class Panel extends JPanel {
private BufferedImage [] img = new BufferedImage [6];
private int w = 0, h = 0, xloc = 0, yloc = 0;
private Die[] state;
public Panel(Die[] state) {
super();
this.state = state;
try {
img[0] = ...
...
img[5] = ImageIO.read(new File("Die6.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
this.setPreferredSize(new Dimension(1300, 600));
w = img[0].getWidth();
h = img[0].getHeight();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// ...I got rid of the bit that just draws all of the faces...
xloc = 0;
yloc = 0;
for (int i = 0; i < state.length; i++)
{
int imageIndex = state[i].getFace() - 1;
g.drawImage(img[imageIndex], xloc, yloc, w, h, null);
xloc += w;
}
}
}
(我没有尝试过这样做,所以它可能无法编译!)
看起来你在你发布的任何代码中都没有创建这个Panel,但假设你想在main方法中显示它,你需要做这样的事情......
public class GameDriver {
public static void main(String[] args) {
Frame f = new Frame();
ArrayList<Die> roll = new ArrayList<Die>(5);
for (int i =0; i < 5; i++)
roll.add(new Die());
// This is a bit strange...
roll.sort(roll.get(0));
for (Die e : roll)
System.out.println(e);
// Create the panel
Panel panel = new Panel(roll.toArray(new Die[0]));
// TODO put it in a frame and show it
}
}