如何将图像文件附加到将在 JPanel 中显示的随机滚动的数字



我需要创建一个程序,它会随机掷出 5 个骰子,然后在 JPanel 中显示这些骰子的面,就像 Yahtzee 游戏一样。我有每个模面的图像,并且在尝试将随机滚动的数字附加到图像时遇到问题。因此,例如,如果随机数为 1,那么它将为五个不同的面显示模面 1。我在下面有的是我的Die类,面板类和我的GameDrive。很抱歉复制了所有代码,不确定是否需要驱动程序来解决此问题,但不知道需要在哪里编写代码才能工作。

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

创建面板时,需要向其传递对要显示的骰子的引用。

(我想这就是你要问的)

您可以在构造函数中传入一些 Die 值,然后在"paint"方法中引用它们,例如......

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

}

(我没有尝试明显地运行它,所以它可能无法编译!

看起来您不像是在您发布的任何代码中创建此面板,但是假设您想在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
    }
}

最新更新