如何获得随机照片输出



我这里有一个程序可以掷两个骰子。它使用线程(我做得对吗?就像我使用了两个线程一样可以吗?当我点击滚动时,最终它会滚动。它工作得很好,但每次我点击停止时,我总是得到相同的输出。如何获得随机输出?请帮忙谢谢

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Rolling extends JFrame {
private boolean pause = false;
private int pic = 1;
private JLabel lblGame, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, out1, out2;
private JButton roll, stop;
private RollButtonHandler rbh;
private StopButtonHandler sbh;
public void showGui() {
    roll = new JButton("Roll");
    roll.setFont(new Font("Arial", Font.BOLD, 20));
    roll.setLocation(170, 350);
    roll.setSize(150, 80);
    roll.setVisible(true);
    rbh = new RollButtonHandler();
    roll.addActionListener(rbh);
    stop = new JButton("Stop");
    stop.setFont(new Font("Arial", Font.BOLD, 20));
    stop.setLocation(375, 350);
    stop.setSize(150, 80);
    stop.setVisible(true);
    sbh = new StopButtonHandler();
    stop.addActionListener(sbh);
    lblGame = new JLabel("Rolling Dice...");
    lblGame.setFont(new Font("Arial", Font.BOLD, 40));
    lblGame.setLocation(300, 20);
    lblGame.setSize(300, 80);
    lblGame.setVisible(true);
    ImageIcon dice1 = new ImageIcon(getClass().getResource("dice1.jpg"));
    d1 = new JLabel(dice1);
    d1.setSize(500, 200);
    d1.setLocation(200, 80);
    d1.setVisible(true);
    ImageIcon dice2 = new ImageIcon(getClass().getResource("dice2.jpg"));
    d2 = new JLabel(dice2);
    d2.setSize(500, 200);
    d2.setLocation(200, 80);
    d2.setVisible(true);
    ImageIcon dice3 = new ImageIcon(getClass().getResource("dice3.jpg"));
    d3 = new JLabel(dice3);
    d3.setSize(500, 200);
    d3.setLocation(200, 80);
    d3.setVisible(true);
    ImageIcon dice4 = new ImageIcon(getClass().getResource("dice4.jpg"));
    d4 = new JLabel(dice4);
    d4.setSize(500, 200);
    d4.setLocation(200, 80);
    d4.setVisible(true);
    ImageIcon dice5 = new ImageIcon(getClass().getResource("dice5.jpg"));
    d5 = new JLabel(dice5);
    d5.setSize(500, 200);
    d5.setLocation(200, 80);
    d5.setVisible(true);
    ImageIcon dice6 = new ImageIcon(getClass().getResource("dice6.jpg"));
    d6 = new JLabel(dice6);
    d6.setSize(500, 200);
    d6.setLocation(200, 80);
    d6.setVisible(true);
    ImageIcon dice7 = new ImageIcon(getClass().getResource("dice1.jpg"));
    d7 = new JLabel(dice7);
    d7.setSize(500,200);
    d7.setLocation(0,80);
    d7.setVisible(true);
    ImageIcon dice8 = new ImageIcon(getClass().getResource("dice2.jpg"));
    d8 = new JLabel(dice8);
    d8.setSize(500,200);
    d8.setLocation(0,80);
    d8.setVisible(true);
    ImageIcon dice9 = new ImageIcon(getClass().getResource("dice3.jpg"));
    d9 = new JLabel(dice9);
    d9.setSize(500,200);
    d9.setLocation(0,80);
    d9.setVisible(true);
    ImageIcon dice10 = new ImageIcon(getClass().getResource("dice4.jpg"));
    d10 = new JLabel(dice10);
    d10.setSize(500,200);
    d10.setLocation(0,80);
    d10.setVisible(true);
    ImageIcon dice11 = new ImageIcon(getClass().getResource("dice5.jpg"));
    d11 = new JLabel(dice11);
    d11.setSize(500,200);
    d11.setLocation(0,80);
    d11.setVisible(true);
    ImageIcon dice12 = new ImageIcon(getClass().getResource("dice6.jpg"));
    d12 = new JLabel(dice12);
    d12.setSize(500,200);
    d12.setLocation(0,80);
    d12.setVisible(true);
    Random ran = new Random();
    int rollDice1 = ran.nextInt(6) + 1;
    int rollDice2 = ran.nextInt(6) + 1;
    ImageIcon output1 = new ImageIcon(getClass().getResource("dice" + rollDice1 + ".jpg"));
    out1 = new JLabel(output1);
    out1.setSize(500, 200);
    out1.setLocation(0, 80);
    out1.setVisible(false);
    ImageIcon output2 = new ImageIcon(getClass().getResource("dice" + rollDice2 + ".jpg"));
    out2 = new JLabel(output2);
    out2.setSize(500, 200);
    out2.setLocation(200, 80);
    out2.setVisible(false);
    Container pane = getContentPane();
    pane.setLayout(null);
    pane.add(lblGame);
    pane.add(d1);
    pane.add(d2);
    pane.add(d3);
    pane.add(d4);
    pane.add(d5);
    pane.add(d6);
    pane.add(d7);
    pane.add(d8);
    pane.add(d9);
    pane.add(d10);
    pane.add(d11);
    pane.add(d12);
    pane.add(roll);
    pane.add(stop);
    pane.add(out1);
    pane.add(out2);
    RollThread1 thread1 = new RollThread1();
    RollThread2 thread2 = new RollThread2();
    setTitle("Two Roll Dice");
    setSize(600, 500);
    setResizable(false);
    setLocation(400, 200);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public class RollThread1 extends Thread {
    public RollThread1() {
        start();
    }
    public void run() {
        try {
            while(pause) {
                if(pic == 1) {
                    d1.setVisible(false);
                    d2.setVisible(true);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(true);
                    pic++;
                    sleep(250);
                } else if(pic == 2) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(true);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(true);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 3) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(true);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(true);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 4) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(true);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(true);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 5) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(true);
                    d7.setVisible(false);
                    d8.setVisible(true);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 6) {
                    d1.setVisible(true);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(true);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic = 1;
                    sleep(250);
                }
            }
        } catch(InterruptedException e) {
        }
    }
}
public class RollThread2 extends Thread {
    public RollThread2() {
        start();
    }
    public void run() {
        try {
            while(pause) {
                if(pic == 7) {
                    d1.setVisible(false);
                    d2.setVisible(true);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(true);
                    pic++;
                    sleep(250);
                } else if(pic == 8) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(true);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(true);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 9) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(true);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(true);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 10) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(true);
                    d6.setVisible(false);
                    d7.setVisible(false);
                    d8.setVisible(false);
                    d9.setVisible(true);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 11) {
                    d1.setVisible(false);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(true);
                    d7.setVisible(false);
                    d8.setVisible(true);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic++;
                    sleep(250);
                } else if(pic == 12) {
                    d1.setVisible(true);
                    d2.setVisible(false);
                    d3.setVisible(false);
                    d4.setVisible(false);
                    d5.setVisible(false);
                    d6.setVisible(false);
                    d7.setVisible(true);
                    d8.setVisible(false);
                    d9.setVisible(false);
                    d10.setVisible(false);
                    d11.setVisible(false);
                    d12.setVisible(false);
                    pic = 1;
                    sleep(250);
                }
            }
        } catch(InterruptedException e) {
        }
    }
}
public class RollButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        RollThread1 thread1 = new RollThread1();
        RollThread2 thread2 = new RollThread2();
        pause = true;
        stop.setEnabled(true);
        roll.setEnabled(false);
        Random ran = new Random();
        int rollDice1 = ran.nextInt(6) + 1;
        int rollDice2 = ran.nextInt(6) + 1;
    }
}
public class StopButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        RollThread1 thread1 = new RollThread1();
        RollThread2 thread2 = new RollThread2();
        pause = false;
        stop.setEnabled(false);
        roll.setEnabled(true);
        d1.setVisible(false);
        d2.setVisible(false);
        d3.setVisible(false);
        d4.setVisible(false);
        d5.setVisible(false);
        d6.setVisible(false);
        d7.setVisible(false);
        d8.setVisible(false);
        d9.setVisible(false);
        d10.setVisible(false);
        d11.setVisible(false);
        d12.setVisible(false);
        out1.setVisible(true);
        out2.setVisible(true);
        Random ran = new Random();
        int rollDice1 = ran.nextInt(6) + 1;
        int rollDice2 = ran.nextInt(6) + 1;
        ImageIcon output1 = new ImageIcon(getClass().getResource("dice" + rollDice1 +".jpg"));
        out1 = new JLabel(output1);
        ImageIcon output2 = new ImageIcon(getClass().getResource("dice" + rollDice2 +".jpg"));
        out2 = new JLabel(output2);
    }
}

}

public static void main(String[] args) {
    Rolling exe = new Rolling();
    exe.showGui();
}

在停止代码中有这个

Random ran = new Random(System.currentTimeMillis());
int rollDice1 = ran.nextInt(6) + 1;
ran.setSeed(System.currentTimeMillis());
int rollDice2 = ran.nextInt(6) + 1;

首先,这是很多代码。我要做的是创建图像图标,将它们添加到集合中并随机选择一个,然后显示它。

ArrayList<JLabel> diceList = new ArrayList<JLabel>();
public Constructor() {
    ImageIcon output1 = new ImageIcon(getClass().getResource("dice" + rollDice1 + ".jpg"));
    out1 = new JLabel(output1);
    out1.setSize(500, 200);
    out1.setLocation(0, 80);
    out1.setVisible(false);
    diceList.add(out1);
    //Add the other dices aswell
}
public JLabel getRandomDice() {
    Random rand = new Random();
    return diceList.get(rand.nextInt(diceList.getLength()))
}

注意:我已经有一段时间没有写Java了,所以它可能无法编译,但你可以得到大致的想法。无需为此使用线程。

即使需要使用线程,这也是非常糟糕的代码。谷歌研究

  • "静态"字段。
  • '爪哇集合'

完成此操作后,您将立即看到自己的代码中需要的一些改进。

相关内容

  • 没有找到相关文章

最新更新