* Each click will
display 3 selected pictures based on 3 random numbers being generated
* after each click If
2 random numbers are the same, you display the same picture
* corresponding to
the number. If all 3 random numbers are the same, you display
* same picture in a
row and declare a winner Make sure you set
* the right size of
the GUI frame, so that 3 pictures can be displayed in a single row.
*/
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class SlotMachine {
public static
void main(String[] args){
final
JLabel labelOne = new JLabel();
final
JLabel labelTwo = new JLabel();
final
JLabel labelThree = new JLabel();
ImageIcon
apple = new ImageIcon("c:/SlotMachine/apple.png");
ImageIcon
bar7 = new ImageIcon("c:/SlotMachine/bar7.png");
ImageIcon
bell = new ImageIcon("c:/SlotMachine/bell.png");
ImageIcon
crown = new ImageIcon("c:/SlotMachine/crown.png");
ImageIcon
melon = new ImageIcon("c:/SlotMachine/melon.png");
final
ArrayList<ImageIcon> slotlist = new ArrayList<ImageIcon>();
slotlist.add(apple);
slotlist.add(bar7);
slotlist.add(bell);
slotlist.add(crown);
slotlist.add(melon);
Random
ran = new Random();
ran.nextInt(5);
JFrame
frame = new JFrame();
frame.setSize(600,400);
frame.setTitle("Georgia
Lottery");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
final
JLabel message = new JLabel();
JPanel
panel = new JPanel();
panel.setBackground(Color.gray);
frame.add(panel,
BorderLayout.NORTH);
JPanel
labelPanel = new JPanel();
labelPanel.add(labelOne);
labelPanel.add(labelTwo);
labelPanel.add(labelThree);
frame.add(labelPanel,
BorderLayout.SOUTH);
frame.add(message,
BorderLayout.CENTER);
JButton
button = new JButton(
"Win
1 Million instantly!");
panel.add(button);
button.addActionListener(new
ActionListener() {
@Override
public
void actionPerformed(ActionEvent arg0) {
Random
rand = new Random();
int
n1 = rand.nextInt(slotlist.size());
int
n2 = rand.nextInt(slotlist.size());
int
n3 = rand.nextInt(slotlist.size());
labelOne.setIcon(slotlist.get(n1));
labelTwo.setIcon(slotlist.get(n2));
labelThree.setIcon(slotlist.get(n3));
if
(n1 == n2 && n1 == n3)
{
message.setText("Congratulations!
You are a Winner!");
}
else
if (n1 == n2 || n1 == n3 || n2 == n3)
{
message.setText("Try
again.");
}
else
message.setText("Sorry,
you lose!");
}
});
}
}
No comments:
Post a Comment