For my project i have 3 light sensor's labeled 1,2 and 3. Eventually I would to display a screen with a Play button, after the button is pressed i want it to randomly display A picture of the Number(1-3) every three seconds indicating which one to shoot with the laser pointer gun. When one of the sensor's is shot I want to add 5 points to the score. I want to display the score on the same panel as the random Number Images. Soo far I have tried making a program to display the Random images, but I'm sure there is a better way of accomplishing this other than what i have used. Could someone please show me?
Here are the images I want to display:
1.jpg2.jpg3.jpg
import java.util.*; import javax.swing.event.*; import javax.swing.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.util.Random; import javax.swing.Timer; public class TimerExample extends JFrame { Random rand = new Random(); static int currRand; private ImageIcon image1; private ImageIcon image2; private ImageIcon image3; private JPanel panel; private JPanel panel2; private JPanel panel3; private JLabel label2; private JLabel label3; private JLabel label; public TimerExample() { setSize(1000,1000); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("RandomPictureTimer"); ImageIcon image1 = new ImageIcon("1.png"); ImageIcon image2 = new ImageIcon("2.png"); ImageIcon image3 = new ImageIcon("3.png"); label= new JLabel(image1); label2 = new JLabel(image2); label3= new JLabel(image3); panel = new JPanel(); panel2 = new JPanel(); panel3= new JPanel(); Timer timer = new Timer(3000,new TimerListener()); timer.start(); while(true) if(currRand==2) { System.out.println("3"); panel3.add(label3); add(panel3); setVisible(true); } else if (currRand ==1) { System.out.println("2"); panel2.add(label2); add(panel2); setVisible(true); } else if (currRand==0) { System.out.println("1"); panel.add(label); add(panel); setVisible(true); } } private class TimerListener implements ActionListener { public void actionPerformed(ActionEvent e) { currRand = rand.nextInt(3); } } public static void main(String args[]) { new TimerExample(); } }