lundi 5 janvier 2015

Slot Machine Randomise

I am creating a slot machine project(for fun).I am displaying the fruits in a JTextField. I have managed to display random fruits in the JTextFields, but the random fruits are the same, for example, the result would be -> Orange, Orange, Orange. Or it would be, Kiwi, Kiwi, Kiwi. ETC. Here is my code.



import java.awt.BorderLayout;...

import java.util.Random;


public class Game extends JFrame {

String [] fruits = { "Cherry", "Lemon", "Orange", "Grape", "Kiwi" };

String fruit = fruits[(int) (Math.random() * fruits.length)];

private JPanel contentPane;
private JTextField Slot_1;
private JTextField Slot_2;
private JTextField Slot_3;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game frame = new Game();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Game() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

Slot_1 = new JTextField();
Slot_1.setText("----------------");
Slot_1.setEditable(false);
Slot_1.setBounds(41, 53, 95, 28);
contentPane.add(Slot_1);
Slot_1.setColumns(10);

Slot_2 = new JTextField();
Slot_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
Slot_2.setText("-----------------");
Slot_2.setEditable(false);
Slot_2.setColumns(10);
Slot_2.setBounds(183, 53, 95, 28);
contentPane.add(Slot_2);

Slot_3 = new JTextField();
Slot_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});
Slot_3.setText("---------------");
Slot_3.setEditable(false);
Slot_3.setColumns(10);
Slot_3.setBounds(317, 53, 95, 28);
contentPane.add(Slot_3);

JButton btn_Pull = new JButton("Pull");
btn_Pull.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

Slot_1.setText(fruit);
Slot_2.setText(fruit);
Slot_3.setText(fruit);

}
});
btn_Pull.setBounds(145, 108, 166, 29);
contentPane.add(btn_Pull);

JButton btnMenu = new JButton("Menu");
btnMenu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

}
});

btnMenu.setBounds(383, 243, 61, 29);
contentPane.add(btnMenu);




}
}


Thank you





Aucun commentaire:

Enregistrer un commentaire