Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to make a roulette game but I don't know what to do here as I have made the code but it is

I am trying to make a roulette game but I don't know what to do here as I have made the code but it is not working and not organized. For example, when the user clicks on spin button a random number gets generated and put onto the textarea where if it even it will go on the even column, odd than odd column, zero than zero columm
the best represention of the area would be this:
Odd Even Zero
9
2
23
0
this is my code now:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Rol implements ActionListener {
private JButton btnSpin;
private JButton btnClear;
private JButton btnExit;
private JTextArea resultArea;
private JLabel[] lblOutcomes;
public Rol(){
btnSpin = new JButton("Spin");
btnSpin.setBounds(290,600,100,50);
btnSpin.addActionListener(this);
btnClear = new JButton("Clear");
btnClear.setBounds(400,600,100,50);
btnClear.addActionListener(this);
btnExit = new JButton("Exit");
btnExit.setBounds(510,600,100,50);
btnExit.addActionListener(this);
resultArea = new JTextArea();
resultArea.setBounds(290,50,320,530);
lblOutcomes = new JLabel[10];
String[] outcomes ={"Odd", "Even", "Zero", "1st 12","2nd 12","3rd 12","1-18","19-36", "Red", "Black"};
for (int i =0; i < outcomes.length; i++){
lblOutcomes[i]= new JLabel();
lblOutcomes[i].setBounds(650,50+(i *55),80,50);
lblOutcomes[i].setBorder(BorderFactory.createLineBorder(Color.white));
lblOutcomes[i].setBorder(BorderFactory.createRaisedBevelBorder());
lblOutcomes[i].setBorder(BorderFactory.createTitledBorder(outcomes[i]));
}
}
public void addComponet(JPanel p){
p.add(btnClear);
p.add(btnSpin);
p.add(btnExit);
p.add(resultArea);
for (int i =0; i < lblOutcomes.length; i++){
p.add(lblOutcomes[i]);
}
}
public void actionPerformed(ActionEvent e){
int ran = random();
int event = events(ran);
if(e.getSource()==btnSpin){
if(event ==0){
resultArea.append(String.format("%-10s",""+ran+"
"));
}else if(event ==1){
resultArea.append(String.format("%-40s",""+ran+"
"));
}else if(event ==-1){
resultArea.append(String.format("%-60s",""+ran+"
"));
}
}
}
public int random(){
int RNG;
RNG =(int)(Math.random()*38);
return RNG;
}
public int events(int ran){
int[] even ={2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36};
int[] odd ={1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37};
if(ran == even.length)
return 0;
else if(ran == odd.length)
return 1;
else
return -1;
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions