Here is an example of a program written in Java, the code is below. Please do not use the code with out permission. If you want to use the code, send me an e-mail at the above address. Include details of what you plan to use the code, or part of the code for.
//Author: B.Myles
//Lasted edited: 17/Nov/98
//
//==============================================================================
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
//==============================================================================
public class bandits extends Applet
implements ActionListener ,Runnable
{
int MAIN_PANEL_WIDTH = 500 ,MAIN_PANEL_HEIGHT = 300;
int NORTH_PANEL_HEIGHT = 40 ,SOUTH_PANEL_HEIGHT = 70;
int REEL_HEIGHT=200 ,REEL_WIDTH = 100 ,REEL_Y_POS = 40;
MediaTracker MyTracker;
Graphics g,g_North,g_East_reel,g_Center_reel,g_West_reel,g_South;
Graphics g_Button_start,g_Button_hold1,g_Button_hold2,g_Button_hold3;
Thread AppleThread;//,BounceThread;
int xpos_of_string;
int ypos_of_string;
String FontList[];
Font Font_one,small_font;
Image img;
int AppletWidth = 1000,AppletHeight = 500;
int reel_one_number = 0,reel_two_number = 5,reel_three_number = 6;
int reel_one_next_number = 5,reel_two_next_number = 2,reel_three_next_number = 6;
int reel_one_next1_number = 3,reel_two_next1_number = 6,reel_three_next1_number = 1;
int int_gap_between_reels = 20;
int int_picture_width = 100;
int reel_one_xpos = 0, reel_one_ypos = 50;
int reel_two_xpos = 0, reel_two_ypos = 50;
int reel_three_xpos = 0, reel_three_ypos = 50;
int reel_one_next_ypos = 150;
int reel_two_next_ypos = 150;
int reel_three_next_ypos = 150;
int reel_one_next1_ypos = 250;
int reel_two_next1_ypos = 250;
int reel_three_next1_ypos = 250;
Panel panel_west = new Panel();
Panel panel_center = new Panel();
Panel panel_east = new Panel();
Panel panel_north = new Panel();
Panel panel_south = new Panel();
Panel Main_panel = new Panel();
Panel Message_panel = new Panel();
Panel Radios_panel = new Panel();
Image reels[][] = new Image[3][8];
Button StartButton,Hold_one,Hold_two,Hold_three;
ActionEvent event;
int held_one,held_two,held_three;
int stop_one =1,stop_two =1,stop_three =1;
Label Message_Label= new Label("spin reels!");
int Winnings=0,Total_Won=0;
int Bet = 10;
int Total_Spent = 0;
int Total_Pounds_Spent = 0;
int Total_Pence_Spent = 0;
Label Label_one = new Label("Winnings: " + String.valueOf(Total_Won) + " pounds");
Label Label_two = new Label("Spent: " + String.valueOf(Total_Spent) + " pounds");
Label Label_three = new Label("Bet: " + String.valueOf(Bet ) + " pence");
boolean Already_Checked= false;
CheckboxGroup cbg = new CheckboxGroup();
Checkbox ten_check = new Checkbox("10 pence", cbg, true);
Checkbox twenty_check =new Checkbox("20 pence", cbg, false);
Checkbox fifty_check = new Checkbox("50 pence", cbg, false);
Label Name_Label= new Label("Enter your name ");
TextField Name_Text = new TextField();
//int bounce = 0;
//==============================================================================
// constructor
public bandits()
{
// TODO: Add constructor code here
AppleThread = null;
//BounceThread = null;
MyTracker = new MediaTracker(this);
//Set x and y position of the string of random numbers
xpos_of_string = 0;
ypos_of_string = 20;
//Get the font list
FontList= Toolkit.getDefaultToolkit().getFontList();
//Set font_one up as plain size 24 of
Font_one = new Font(FontList[0],Font.PLAIN,24);
//Set the font of the current graphics object to font_one
setFont(Font_one);
//set up another font which is small
small_font= new Font(FontList[1],Font.BOLD,14);
}
//==============================================================================
//destructor
public void destroy()
{
//thought you might need to delete dynamicaly alloccated memory
//delete panel_west;
//delete panel_center;
//delete panel_east;
//delete panel_north;
//delete panel_south;
//delete Main_panel;
//for(int x=0;x<3;x++)
//for(int y=0;y<8;y++)
//delete reels[x][y];
}
//==============================================================================
public void run()
{
if(Thread.currentThread() == AppleThread)
{
while(true)
{
try
{
AppleThread.sleep(0);
}
catch (Exception ignored)
{
return;
}
repaint();
}
}
/*if(Thread.currentThread() == BounceThread)
{
while(true)
{
try
{
BounceThread.sleep(8);
}
catch (Exception ignored)
{
return;
}
bounce_reel();
}
}*/
}
//==============================================================================
// start function
public void start()
{
if (AppleThread== null)
{
AppleThread= new Thread(this);
AppleThread.start();
}
/*if (BounceThread== null)
{
BounceThread= new Thread(this);
BounceThread.start();
}*/
}
//==============================================================================
// stop function
public void stop()
{
if (AppleThread!= null)
{
AppleThread.stop();
AppleThread= null;
}
/*if (BounceThread!= null)
{
BounceThread.stop();
BounceThread= null;
}*/
}
//==============================================================================
//initalization function
public void init()
{
//adds the pictures to the arrays
initialise_reels();
//Main_panel uses a borderLayout with vertical and horizontal sapce between componetns of 20.
Main_panel.setLayout(new BorderLayout());
Message_panel.setLayout(new BorderLayout());
Main_panel.add("West",panel_west);
Main_panel.add("Center",panel_center);
Main_panel.add("East",panel_east);
Main_panel.add("North",panel_north);
Main_panel.add("South",panel_south);
Radios_panel.setLayout(new GridLayout(3, 1));
Radios_panel.add(ten_check);
Radios_panel.add(twenty_check);
Radios_panel.add(fifty_check);
//add event handalers for when the user clicks a radio button
ten_check.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent event)
{
Bet = 10;
Label_three.setText("Bet: " + String.valueOf(Bet) + " pence");
}
} );
twenty_check.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent event)
{
Bet = 20;
Label_three.setText("Bet: " + String.valueOf(Bet) + " pence");
}
} );
fifty_check.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent event)
{
Bet = 50;
Label_three.setText("Bet: " + String.valueOf(Bet) + " pence");
}
} );
Message_panel.add("North",Label_one);
Message_panel.add("Center",Label_two);
Message_panel.add("South",Label_three);
Message_panel.add("East",Radios_panel);
add(Name_Label);
add(Name_Text);
add(Main_panel);
add(Message_panel);
Hold_one = new Button("Hold 1");
Hold_two = new Button("Hold 2");
Hold_three = new Button("Hold 3");
StartButton = new Button("Start");
panel_south.add(StartButton);
panel_south.add(Hold_one);
panel_south.add(Hold_two);
panel_south.add(Hold_three);
panel_north.add(Message_Label);
StartButton.addActionListener(this);
Hold_one.addActionListener(this);
Hold_two.addActionListener(this);
Hold_three.addActionListener(this);
//get the graphics objects so that pictures can be added
g_North = panel_north.getGraphics();
g_South = panel_south.getGraphics();
g_West_reel = panel_west.getGraphics();
g_Center_reel = panel_center.getGraphics();
g_East_reel = panel_east.getGraphics();
g_Button_start = StartButton.getGraphics();
g_Button_hold1 = Hold_one.getGraphics();
g_Button_hold2 = Hold_two.getGraphics();
g_Button_hold3 = Hold_three.getGraphics();
//set the buttons to grey
Hold_one.setBackground(Color.gray);
Hold_two.setBackground(Color.gray);
Hold_three.setBackground(Color.gray);
StartButton.setBackground(Color.gray);
//set the two objects to have a small font
Name_Label.setFont(small_font);
Name_Text.setFont(small_font);
}
// event handler for the buttons
public void actionPerformed(ActionEvent event)
{
//get the name on the button pressed
String command = event.getActionCommand();
//================================================
if (command.equals("Start"))
{
//set the buuton to yellow and add a space to the end of the button text
StartButton.setLabel("Start ");
StartButton.setBackground(Color.yellow);
//If one of the reels are held set the reel to stop
//The stop variable is the one check before spinning
if(held_one == 0)
stop_one =0;
if(held_two == 0)
stop_two =0;
if(held_three == 0)
stop_three =0;
// set all buttons to unenabled
Hold_one.setEnabled(false);
Hold_two.setEnabled(false);
Hold_three.setEnabled(false);
StartButton.setEnabled(false);
ten_check.setEnabled(false);
twenty_check.setEnabled(false);
fifty_check.setEnabled(false);
//add to the total spent
Total_Spent = Total_Spent + Bet;
Total_Pounds_Spent = (int)Total_Spent/100;
Total_Pence_Spent = (int)Total_Spent%100;
//update the strings
Label_two.setText("Spent: " + String.valueOf(Total_Pounds_Spent ) + " pounds "
+String.valueOf(Total_Pence_Spent ) + " pence");
Label_three.setText("Bet: " + String.valueOf(Bet) + " pence");
Already_Checked = false;
repaint();
}
if (command.equals("Start "))
{
StartButton.setLabel("Start");
StartButton.setBackground(Color.gray);
stop_one =1;
stop_two =1;
stop_three =1;
repaint();
}
//================================================
//if the user clicked hold 1
if (command.equals("Hold 1"))
{
Hold_one.setLabel("Held 1");
Hold_one.setBackground(Color.yellow);
repaint();
stop_one = 1;
held_one = 1;
}
if (command.equals("Held 1"))
{
Hold_one.setLabel("Hold 1");
Hold_one.setBackground(Color.gray);
repaint();
held_one = 0;
}
//if the user clicked hold 2
//================================================
if (command.equals("Hold 2"))
{
Hold_two.setLabel("Held 2");
Hold_two.setBackground(Color.yellow);
repaint();
stop_two = 1;
held_two = 1;
}
if (command.equals("Held 2"))
{
Hold_two.setLabel("Hold 2");
Hold_two.setBackground(Color.gray);
repaint();
held_two = 0;
}
//================================================
//if the user clicked hold 3
if (command.equals("Hold 3"))
{
Hold_three.setLabel("Held 3");
Hold_three.setBackground(Color.yellow);
repaint();
stop_three = 1;
held_three = 1;
}
if (command.equals("Held 3"))
{
Hold_three.setLabel("Hold 3");
Hold_three.setBackground(Color.gray);
repaint();
held_three = 0;
}
}
//==============================================================================
// paint function gets called continuously
public void paint(Graphics g)
{
//set the heigth and widths of all the oblects
Setup_Height_widths_positions();
setBackground(Color.blue);
//Draw a background image
g.drawImage(img,0,0,this);
//each call to this might result in the next reel stopping
//it is random!
randomize_reels_stopping();
//if reel ones top pic is off the top of the viewable reel then update the pics
if(reel_one_ypos < -100)
{
update_reel_one();
}
if(reel_two_ypos < -100)
{
update_reel_two();
}
if(reel_three_ypos < -100)
{
update_reel_three();
}
//reel one is meant to be stopped and the pic is centerd in the reel, do nothing
if(stop_one == 1 && reel_one_ypos == -50);
else
//move the reel up a little bit
{
alter_xy_of_pics_in_reel_one();
draw_reel_one();
}
//reel two is meant to be stopped and the pic is centerd in the reel, do nothing
if(stop_two == 1 && reel_two_ypos == -50);
else
//move the reel up a little bit
{
alter_xy_of_pics_in_reel_two();
draw_reel_two();
}
//reel three is meant to be stopped and the pic is centerd in the reel, do nothing
if(stop_three == 1 && reel_three_ypos == -50);
else
//move the reel up a little bit
{
alter_xy_of_pics_in_reel_three();
draw_reel_three();
}
//if the applet is resized when all the reels have stopped spining this function
//will stop the reels going blank.
if_the_reels_have_all_stopped_draw_them();
}
//==============================================================================
// setup the heights and widths and the positions of all the panels
public void Setup_Height_widths_positions()
{
//setSize( width, height)
//setLocation ( x , y )
AppletWidth =this.getSize().width;
AppletHeight =this.getSize().height;
Main_panel.setSize(MAIN_PANEL_WIDTH, MAIN_PANEL_HEIGHT);
Main_panel.setLocation(AppletWidth/2 - MAIN_PANEL_WIDTH/2,200);
Message_panel.setSize(MAIN_PANEL_WIDTH, 140);
Message_panel.setLocation(AppletWidth/2 - MAIN_PANEL_WIDTH/2,30);
Name_Label.setSize(150, 25);
Name_Label.setLocation(AppletWidth/2 - 150,5);
Name_Text.setSize(150, 25);
Name_Text.setLocation(AppletWidth/2,5);
Label_one.setLocation(20,10);
Label_two.setLocation(20,50);
Label_three.setLocation(20,90);
Label_one.setSize(300, 45);
Label_two.setSize(300, 45);
Label_three.setSize(300,45);
Radios_panel.setLocation(330,10);
panel_north.setSize(MAIN_PANEL_WIDTH,NORTH_PANEL_HEIGHT);
panel_north.setLocation(0,0);
Message_Label.setSize(500,NORTH_PANEL_HEIGHT);
panel_south.setSize(MAIN_PANEL_WIDTH,SOUTH_PANEL_HEIGHT);
panel_south.setLocation(0,240);
panel_west.setSize(REEL_WIDTH,REEL_HEIGHT);
panel_west.setLocation(10,REEL_Y_POS);
panel_center.setSize(REEL_WIDTH,REEL_HEIGHT);
panel_center.setLocation(130,REEL_Y_POS);
panel_east.setSize(300,REEL_HEIGHT);
panel_east.setLocation(250,REEL_Y_POS);
Hold_one.setLocation(20,10);
Hold_two.setLocation(140,10);
Hold_three.setLocation(260,10);
StartButton.setLocation(360,10);
StartButton.setSize(100,35);
}
//========================================================================
//Stops the reels one after another, allow for reels wich are held
public void randomize_reels_stopping()
{
//if the first reel hasn't stopped give it a new random value
//the value might be 1, which will then cause the reel to stop
if(stop_one != 1)
stop_one = ((int)(Math.random()*1000));
//If reel one has stopped and reel two hasn't then give it a new value
if(stop_one == 1 && stop_two != 1)
stop_two = ((int)(Math.random()*1000));
//if reel one has stopped and reel twohas stopped and reel three hasn't give it a new value
if(stop_one ==1 && stop_two == 1 && stop_three != 1 )
{
stop_three = ((int)(Math.random()*1000));
}
//If they have all stopped spinning change the startButton back to start
if(stop_one ==1 && stop_two == 1 && stop_three == 1 && StartButton.getLabel().equals("Start "))
{
StartButton.setEnabled(true);
StartButton.setLabel("Start");
StartButton.setBackground(Color.gray);
}
}
//==============================================================================
// change the number and poistions of the pics on reel one
//and randomize new number for the next pic
public void update_reel_one()
{
reel_one_ypos = -10;
reel_one_number = reel_one_next_number;
reel_one_next_number = reel_one_next1_number;
Generate_Next1_Random_Numbers_reel_one();
}
//==============================================================================
// change the number and poistions of the pics on reel two
//and randomize new number for the next pic
public void update_reel_two()
{
reel_two_ypos = -10;
reel_two_number = reel_two_next_number;
reel_two_next_number = reel_two_next1_number;
Generate_Next1_Random_Numbers_reel_two();
}
//==============================================================================
// change the number and poistions of the pic on reel three
//and randomize new number for the next pic
public void update_reel_three()
{
reel_three_ypos = -10;
reel_three_number = reel_three_next_number;
reel_three_next_number = reel_three_next1_number;
Generate_Next1_Random_Numbers_reel_three();
}
//==============================================================================
// This function subtracts 20 of the y corodiate of the displayed pics in reel one
// ie. moves them all up the screen a little
public void alter_xy_of_pics_in_reel_one()
{
reel_one_ypos = reel_one_ypos - 10;
reel_one_next_ypos = reel_one_ypos + 100;
reel_one_next1_ypos = reel_one_next_ypos + 100;
}
//==============================================================================
// This function subtracts 20 of the y corodiate of the displayed pics in reel two
// ie. moves them all up the screen a little
public void alter_xy_of_pics_in_reel_two()
{
reel_two_ypos = reel_two_ypos - 10;
reel_two_next_ypos = reel_two_ypos + 100;
reel_two_next1_ypos = reel_two_next_ypos + 100;
}
//==============================================================================
// This function subtracts 20 of the y corodiate of the displayed pics in reel three
// ie. moves them all up the screen a little
public void alter_xy_of_pics_in_reel_three()
{
reel_three_ypos = reel_three_ypos - 10;
reel_three_next_ypos = reel_three_ypos + 100;
reel_three_next1_ypos = reel_three_next_ypos + 100;
}
//==============================================================================
//Draws the first reel. This consists of three pics displayed one above the other.
//The x position is the same for them all.
//The y positions differ by one hundered (this is the height of the pics)
public void draw_reel_one()
{
//Draws the first reel
Draw_reel(g_West_reel, 0, reel_one_number , reel_one_xpos, reel_one_ypos);
//Draws the first reel
Draw_reel(g_West_reel,0, reel_one_next_number , reel_one_xpos, reel_one_next_ypos);
//Draws the first reel
Draw_reel(g_West_reel,0, reel_one_next1_number , reel_one_xpos, reel_one_next1_ypos);
}
//==============================================================================
//Draws the second reel. This consists of three pics displayed one above the other.
//The x position is the same for them all.
//The y positions differ by one hundered (this is the height of the pics)
public void draw_reel_two()
{
//Draws the second reel
Draw_reel(g_Center_reel,1, reel_two_number , reel_two_xpos, reel_two_ypos);
//Draws the second reel
Draw_reel(g_Center_reel,1, reel_two_next_number, reel_two_xpos, reel_two_next_ypos);
//Draws the second reel
Draw_reel(g_Center_reel,1, reel_two_next1_number, reel_two_xpos, reel_two_next1_ypos);
}
//==============================================================================
//Draws the first reel. This consists of three pics displayed one above the other.
//The x position is the same for them all.
//The y positions differ by one hundered (this is the height of the pics)
public void draw_reel_three()
{
//Draws the third reel
Draw_reel(g_East_reel, 2, reel_three_number , reel_three_xpos, reel_three_ypos);
//Draws the third reel
Draw_reel(g_East_reel,2, reel_three_next_number, reel_three_xpos, reel_three_next_ypos);
//Draws the third reel
Draw_reel(g_East_reel,2, reel_three_next1_number, reel_three_xpos, reel_three_next1_ypos);
}
public void Generate_Next1_Random_Numbers_reel_one()
{
reel_one_next1_number =((int)(Math.random()*8));
}
public void Generate_Next1_Random_Numbers_reel_two()
{
reel_two_next1_number =((int)(Math.random()*8));
}
public void Generate_Next1_Random_Numbers_reel_three()
{
reel_three_next1_number =((int)(Math.random()*8));
}
//==============================================================================
// Update function stops the screen from flashing
public void update(Graphics g)
{
paint(g);
}
//==============================================================================
// get the images and puts them in the reels array
public void initialise_reels()
{
//add in the pics to the array
//pics should be 100 by 100
for(int reel_number=0;reel_number < 3;reel_number++)
{
reels[reel_number][0] = getImage(getDocumentBase(),"images/apple.gif");
reels[reel_number][1] = getImage(getDocumentBase(),"images/barone.gif");
reels[reel_number][2] = getImage(getDocumentBase(),"images/cherr.gif");
reels[reel_number][3] = getImage(getDocumentBase(),"images/seven.gif");
reels[reel_number][4] = getImage(getDocumentBase(),"images/bartwo.gif");
reels[reel_number][5] = getImage(getDocumentBase(),"images/strawb.gif");
reels[reel_number][6] = getImage(getDocumentBase(),"images/sevengol.gif");
reels[reel_number][7] = getImage(getDocumentBase(),"images/barthree.gif");
//add images to the tracker
for(int x=0;x<8;x++)
MyTracker.addImage(reels[reel_number][x],0);
}
//get back ground image
img = getImage(getDocumentBase(),"images/blue.gif");
//add the background image to the tracker
MyTracker.addImage(img,0);
//wait until the images have loaded
try
{
MyTracker.waitForID(0);
}
catch(Exception et)
{
System.out.println("loading images");
}
}
//==============================================================================
// sets the postion of the reels
public void set_reel_positons()
{
//sets reel two to be in the middle of the screen
// -50 beacuse the reel is 100 wide.
reel_two_xpos = AppletWidth/2 -50;
reel_one_xpos = reel_two_xpos - int_gap_between_reels - int_picture_width;
reel_three_xpos = reel_two_xpos + int_gap_between_reels + int_picture_width + 30;
}
//==============================================================================
// draws reel one the screen
public void Draw_reel(Graphics the_g,int reel_number,int image_number,int reel_xpos,int reel_ypos)
{
the_g.drawImage(reels[reel_number][image_number],reel_xpos,reel_ypos,this);
}
//=============================================================
//if the applet is resized when all the reels have stopped spining this function
//will stop the reels going blank. Also determines if there is a winning line and
// displays a approprate message.
public void if_the_reels_have_all_stopped_draw_them()
{
//if the reels have stopped
if(stop_one ==1 && stop_two == 1 && stop_three == 1 &&
reel_one_ypos == -50 && reel_two_ypos == -50 && reel_three_ypos == -50)
{ //draw the reels
draw_reel_one();
draw_reel_two();
draw_reel_three();
//if there is no winning line when the reels have stopped make the message say
//"spin reel!"
if(WinLine() == 0)//not a winning line
{
Message_Label.setText("spin reel!");
//The reels have now stopped and the player didn't win so enable them all again
Hold_one.setEnabled(true);
Hold_two.setEnabled(true);
Hold_three.setEnabled(true);
ten_check.setEnabled(true);
twenty_check.setEnabled(true);
fifty_check.setEnabled(true);
}
else//there is a winning line
{ //this if statment ensures that the following code is only called once
//per winning line.
if(Already_Checked == false)
{
//if there is a winning line change the message
Winnings = WinLine();
//add the winnings for this go to the total
Total_Won = Total_Won + Winnings;
//set message saying how much you win
Message_Label.setText(Name_Text.getText() + " you win " + String.valueOf(Winnings) + " pounds");
//set the text for total winnings
Label_one.setText("Winnings: " + String.valueOf(Total_Won) + " pounds");
//the player won so leave the hold buttons disabled
//set all the hold buttons to not holding
held_one = 0;
Hold_one.setLabel("Hold 1");
Hold_one.setBackground(Color.gray);
held_two = 0;
Hold_two.setLabel("Hold 2");
Hold_two.setBackground(Color.gray);
held_three = 0;
Hold_three.setLabel("Hold 3");
Hold_three.setBackground(Color.gray);
Already_Checked = true;
}
}
}
}
//==============================================================================
//returns zero if all of the sybols don't match
//else it returns the amount of winnings.
public int WinLine()
{
if(reel_one_next_number == reel_two_next_number && reel_one_next_number == reel_three_next_number )
return(reel_one_number + 1);
else
return(0);
}
//==============================================================================
// When the reel is held it continues scrolling until the top pic is at a height of -50
// The reel is then set to bounce and then stop.
// This function does the bouncing
public void bounce_reel()
{
//couldn't get this function to work
}
//==============================================================================
// Generate random numbers function is now redundant as the numbers do not need to be displayed
public void Generate_Random_Numbers(Graphics g)
{
String string_Rand_Numbers;
int int_Width_of_string,int_Height_of_string ;
reel_one_number = ((int)(Math.random()*8));
reel_two_number = ((int)(Math.random()*8));
reel_three_number = ((int)(Math.random()*8));
string_Rand_Numbers=reel_one_number +":"+reel_two_number +":"+reel_three_number ;
int_Width_of_string = g.getFontMetrics().stringWidth(string_Rand_Numbers);
int_Height_of_string = g.getFontMetrics().getHeight();
xpos_of_string = AppletWidth/2 - int_Width_of_string/2;
ypos_of_string = int_Height_of_string + 5;
g.drawString(string_Rand_Numbers,xpos_of_string ,20);
}
}
//Lasted edited: 17/Nov/98
//
//==============================================================================
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
//==============================================================================
public class bandits extends Applet
implements ActionListener ,Runnable
{
int MAIN_PANEL_WIDTH = 500 ,MAIN_PANEL_HEIGHT = 300;
int NORTH_PANEL_HEIGHT = 40 ,SOUTH_PANEL_HEIGHT = 70;
int REEL_HEIGHT=200 ,REEL_WIDTH = 100 ,REEL_Y_POS = 40;
MediaTracker MyTracker;
Graphics g,g_North,g_East_reel,g_Center_reel,g_West_reel,g_South;
Graphics g_Button_start,g_Button_hold1,g_Button_hold2,g_Button_hold3;
Thread AppleThread;//,BounceThread;
int xpos_of_string;
int ypos_of_string;
String FontList[];
Font Font_one,small_font;
Image img;
int AppletWidth = 1000,AppletHeight = 500;
int reel_one_number = 0,reel_two_number = 5,reel_three_number = 6;
int reel_one_next_number = 5,reel_two_next_number = 2,reel_three_next_number = 6;
int reel_one_next1_number = 3,reel_two_next1_number = 6,reel_three_next1_number = 1;
int int_gap_between_reels = 20;
int int_picture_width = 100;
int reel_one_xpos = 0, reel_one_ypos = 50;
int reel_two_xpos = 0, reel_two_ypos = 50;
int reel_three_xpos = 0, reel_three_ypos = 50;
int reel_one_next_ypos = 150;
int reel_two_next_ypos = 150;
int reel_three_next_ypos = 150;
int reel_one_next1_ypos = 250;
int reel_two_next1_ypos = 250;
int reel_three_next1_ypos = 250;
Panel panel_west = new Panel();
Panel panel_center = new Panel();
Panel panel_east = new Panel();
Panel panel_north = new Panel();
Panel panel_south = new Panel();
Panel Main_panel = new Panel();
Panel Message_panel = new Panel();
Panel Radios_panel = new Panel();
Image reels[][] = new Image[3][8];
Button StartButton,Hold_one,Hold_two,Hold_three;
ActionEvent event;
int held_one,held_two,held_three;
int stop_one =1,stop_two =1,stop_three =1;
Label Message_Label= new Label("spin reels!");
int Winnings=0,Total_Won=0;
int Bet = 10;
int Total_Spent = 0;
int Total_Pounds_Spent = 0;
int Total_Pence_Spent = 0;
Label Label_one = new Label("Winnings: " + String.valueOf(Total_Won) + " pounds");
Label Label_two = new Label("Spent: " + String.valueOf(Total_Spent) + " pounds");
Label Label_three = new Label("Bet: " + String.valueOf(Bet ) + " pence");
boolean Already_Checked= false;
CheckboxGroup cbg = new CheckboxGroup();
Checkbox ten_check = new Checkbox("10 pence", cbg, true);
Checkbox twenty_check =new Checkbox("20 pence", cbg, false);
Checkbox fifty_check = new Checkbox("50 pence", cbg, false);
Label Name_Label= new Label("Enter your name ");
TextField Name_Text = new TextField();
//int bounce = 0;
//==============================================================================
// constructor
public bandits()
{
// TODO: Add constructor code here
AppleThread = null;
//BounceThread = null;
MyTracker = new MediaTracker(this);
//Set x and y position of the string of random numbers
xpos_of_string = 0;
ypos_of_string = 20;
//Get the font list
FontList= Toolkit.getDefaultToolkit().getFontList();
//Set font_one up as plain size 24 of
Font_one = new Font(FontList[0],Font.PLAIN,24);
//Set the font of the current graphics object to font_one
setFont(Font_one);
//set up another font which is small
small_font= new Font(FontList[1],Font.BOLD,14);
}
//==============================================================================
//destructor
public void destroy()
{
//thought you might need to delete dynamicaly alloccated memory
//delete panel_west;
//delete panel_center;
//delete panel_east;
//delete panel_north;
//delete panel_south;
//delete Main_panel;
//for(int x=0;x<3;x++)
//for(int y=0;y<8;y++)
//delete reels[x][y];
}
//==============================================================================
public void run()
{
if(Thread.currentThread() == AppleThread)
{
while(true)
{
try
{
AppleThread.sleep(0);
}
catch (Exception ignored)
{
return;
}
repaint();
}
}
/*if(Thread.currentThread() == BounceThread)
{
while(true)
{
try
{
BounceThread.sleep(8);
}
catch (Exception ignored)
{
return;
}
bounce_reel();
}
}*/
}
//==============================================================================
// start function
public void start()
{
if (AppleThread== null)
{
AppleThread= new Thread(this);
AppleThread.start();
}
/*if (BounceThread== null)
{
BounceThread= new Thread(this);
BounceThread.start();
}*/
}
//==============================================================================
// stop function
public void stop()
{
if (AppleThread!= null)
{
AppleThread.stop();
AppleThread= null;
}
/*if (BounceThread!= null)
{
BounceThread.stop();
BounceThread= null;
}*/
}
//==============================================================================
//initalization function
public void init()
{
//adds the pictures to the arrays
initialise_reels();
//Main_panel uses a borderLayout with vertical and horizontal sapce between componetns of 20.
Main_panel.setLayout(new BorderLayout());
Message_panel.setLayout(new BorderLayout());
Main_panel.add("West",panel_west);
Main_panel.add("Center",panel_center);
Main_panel.add("East",panel_east);
Main_panel.add("North",panel_north);
Main_panel.add("South",panel_south);
Radios_panel.setLayout(new GridLayout(3, 1));
Radios_panel.add(ten_check);
Radios_panel.add(twenty_check);
Radios_panel.add(fifty_check);
//add event handalers for when the user clicks a radio button
ten_check.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent event)
{
Bet = 10;
Label_three.setText("Bet: " + String.valueOf(Bet) + " pence");
}
} );
twenty_check.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent event)
{
Bet = 20;
Label_three.setText("Bet: " + String.valueOf(Bet) + " pence");
}
} );
fifty_check.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent event)
{
Bet = 50;
Label_three.setText("Bet: " + String.valueOf(Bet) + " pence");
}
} );
Message_panel.add("North",Label_one);
Message_panel.add("Center",Label_two);
Message_panel.add("South",Label_three);
Message_panel.add("East",Radios_panel);
add(Name_Label);
add(Name_Text);
add(Main_panel);
add(Message_panel);
Hold_one = new Button("Hold 1");
Hold_two = new Button("Hold 2");
Hold_three = new Button("Hold 3");
StartButton = new Button("Start");
panel_south.add(StartButton);
panel_south.add(Hold_one);
panel_south.add(Hold_two);
panel_south.add(Hold_three);
panel_north.add(Message_Label);
StartButton.addActionListener(this);
Hold_one.addActionListener(this);
Hold_two.addActionListener(this);
Hold_three.addActionListener(this);
//get the graphics objects so that pictures can be added
g_North = panel_north.getGraphics();
g_South = panel_south.getGraphics();
g_West_reel = panel_west.getGraphics();
g_Center_reel = panel_center.getGraphics();
g_East_reel = panel_east.getGraphics();
g_Button_start = StartButton.getGraphics();
g_Button_hold1 = Hold_one.getGraphics();
g_Button_hold2 = Hold_two.getGraphics();
g_Button_hold3 = Hold_three.getGraphics();
//set the buttons to grey
Hold_one.setBackground(Color.gray);
Hold_two.setBackground(Color.gray);
Hold_three.setBackground(Color.gray);
StartButton.setBackground(Color.gray);
//set the two objects to have a small font
Name_Label.setFont(small_font);
Name_Text.setFont(small_font);
}
// event handler for the buttons
public void actionPerformed(ActionEvent event)
{
//get the name on the button pressed
String command = event.getActionCommand();
//================================================
if (command.equals("Start"))
{
//set the buuton to yellow and add a space to the end of the button text
StartButton.setLabel("Start ");
StartButton.setBackground(Color.yellow);
//If one of the reels are held set the reel to stop
//The stop variable is the one check before spinning
if(held_one == 0)
stop_one =0;
if(held_two == 0)
stop_two =0;
if(held_three == 0)
stop_three =0;
// set all buttons to unenabled
Hold_one.setEnabled(false);
Hold_two.setEnabled(false);
Hold_three.setEnabled(false);
StartButton.setEnabled(false);
ten_check.setEnabled(false);
twenty_check.setEnabled(false);
fifty_check.setEnabled(false);
//add to the total spent
Total_Spent = Total_Spent + Bet;
Total_Pounds_Spent = (int)Total_Spent/100;
Total_Pence_Spent = (int)Total_Spent%100;
//update the strings
Label_two.setText("Spent: " + String.valueOf(Total_Pounds_Spent ) + " pounds "
+String.valueOf(Total_Pence_Spent ) + " pence");
Label_three.setText("Bet: " + String.valueOf(Bet) + " pence");
Already_Checked = false;
repaint();
}
if (command.equals("Start "))
{
StartButton.setLabel("Start");
StartButton.setBackground(Color.gray);
stop_one =1;
stop_two =1;
stop_three =1;
repaint();
}
//================================================
//if the user clicked hold 1
if (command.equals("Hold 1"))
{
Hold_one.setLabel("Held 1");
Hold_one.setBackground(Color.yellow);
repaint();
stop_one = 1;
held_one = 1;
}
if (command.equals("Held 1"))
{
Hold_one.setLabel("Hold 1");
Hold_one.setBackground(Color.gray);
repaint();
held_one = 0;
}
//if the user clicked hold 2
//================================================
if (command.equals("Hold 2"))
{
Hold_two.setLabel("Held 2");
Hold_two.setBackground(Color.yellow);
repaint();
stop_two = 1;
held_two = 1;
}
if (command.equals("Held 2"))
{
Hold_two.setLabel("Hold 2");
Hold_two.setBackground(Color.gray);
repaint();
held_two = 0;
}
//================================================
//if the user clicked hold 3
if (command.equals("Hold 3"))
{
Hold_three.setLabel("Held 3");
Hold_three.setBackground(Color.yellow);
repaint();
stop_three = 1;
held_three = 1;
}
if (command.equals("Held 3"))
{
Hold_three.setLabel("Hold 3");
Hold_three.setBackground(Color.gray);
repaint();
held_three = 0;
}
}
//==============================================================================
// paint function gets called continuously
public void paint(Graphics g)
{
//set the heigth and widths of all the oblects
Setup_Height_widths_positions();
setBackground(Color.blue);
//Draw a background image
g.drawImage(img,0,0,this);
//each call to this might result in the next reel stopping
//it is random!
randomize_reels_stopping();
//if reel ones top pic is off the top of the viewable reel then update the pics
if(reel_one_ypos < -100)
{
update_reel_one();
}
if(reel_two_ypos < -100)
{
update_reel_two();
}
if(reel_three_ypos < -100)
{
update_reel_three();
}
//reel one is meant to be stopped and the pic is centerd in the reel, do nothing
if(stop_one == 1 && reel_one_ypos == -50);
else
//move the reel up a little bit
{
alter_xy_of_pics_in_reel_one();
draw_reel_one();
}
//reel two is meant to be stopped and the pic is centerd in the reel, do nothing
if(stop_two == 1 && reel_two_ypos == -50);
else
//move the reel up a little bit
{
alter_xy_of_pics_in_reel_two();
draw_reel_two();
}
//reel three is meant to be stopped and the pic is centerd in the reel, do nothing
if(stop_three == 1 && reel_three_ypos == -50);
else
//move the reel up a little bit
{
alter_xy_of_pics_in_reel_three();
draw_reel_three();
}
//if the applet is resized when all the reels have stopped spining this function
//will stop the reels going blank.
if_the_reels_have_all_stopped_draw_them();
}
//==============================================================================
// setup the heights and widths and the positions of all the panels
public void Setup_Height_widths_positions()
{
//setSize( width, height)
//setLocation ( x , y )
AppletWidth =this.getSize().width;
AppletHeight =this.getSize().height;
Main_panel.setSize(MAIN_PANEL_WIDTH, MAIN_PANEL_HEIGHT);
Main_panel.setLocation(AppletWidth/2 - MAIN_PANEL_WIDTH/2,200);
Message_panel.setSize(MAIN_PANEL_WIDTH, 140);
Message_panel.setLocation(AppletWidth/2 - MAIN_PANEL_WIDTH/2,30);
Name_Label.setSize(150, 25);
Name_Label.setLocation(AppletWidth/2 - 150,5);
Name_Text.setSize(150, 25);
Name_Text.setLocation(AppletWidth/2,5);
Label_one.setLocation(20,10);
Label_two.setLocation(20,50);
Label_three.setLocation(20,90);
Label_one.setSize(300, 45);
Label_two.setSize(300, 45);
Label_three.setSize(300,45);
Radios_panel.setLocation(330,10);
panel_north.setSize(MAIN_PANEL_WIDTH,NORTH_PANEL_HEIGHT);
panel_north.setLocation(0,0);
Message_Label.setSize(500,NORTH_PANEL_HEIGHT);
panel_south.setSize(MAIN_PANEL_WIDTH,SOUTH_PANEL_HEIGHT);
panel_south.setLocation(0,240);
panel_west.setSize(REEL_WIDTH,REEL_HEIGHT);
panel_west.setLocation(10,REEL_Y_POS);
panel_center.setSize(REEL_WIDTH,REEL_HEIGHT);
panel_center.setLocation(130,REEL_Y_POS);
panel_east.setSize(300,REEL_HEIGHT);
panel_east.setLocation(250,REEL_Y_POS);
Hold_one.setLocation(20,10);
Hold_two.setLocation(140,10);
Hold_three.setLocation(260,10);
StartButton.setLocation(360,10);
StartButton.setSize(100,35);
}
//========================================================================
//Stops the reels one after another, allow for reels wich are held
public void randomize_reels_stopping()
{
//if the first reel hasn't stopped give it a new random value
//the value might be 1, which will then cause the reel to stop
if(stop_one != 1)
stop_one = ((int)(Math.random()*1000));
//If reel one has stopped and reel two hasn't then give it a new value
if(stop_one == 1 && stop_two != 1)
stop_two = ((int)(Math.random()*1000));
//if reel one has stopped and reel twohas stopped and reel three hasn't give it a new value
if(stop_one ==1 && stop_two == 1 && stop_three != 1 )
{
stop_three = ((int)(Math.random()*1000));
}
//If they have all stopped spinning change the startButton back to start
if(stop_one ==1 && stop_two == 1 && stop_three == 1 && StartButton.getLabel().equals("Start "))
{
StartButton.setEnabled(true);
StartButton.setLabel("Start");
StartButton.setBackground(Color.gray);
}
}
//==============================================================================
// change the number and poistions of the pics on reel one
//and randomize new number for the next pic
public void update_reel_one()
{
reel_one_ypos = -10;
reel_one_number = reel_one_next_number;
reel_one_next_number = reel_one_next1_number;
Generate_Next1_Random_Numbers_reel_one();
}
//==============================================================================
// change the number and poistions of the pics on reel two
//and randomize new number for the next pic
public void update_reel_two()
{
reel_two_ypos = -10;
reel_two_number = reel_two_next_number;
reel_two_next_number = reel_two_next1_number;
Generate_Next1_Random_Numbers_reel_two();
}
//==============================================================================
// change the number and poistions of the pic on reel three
//and randomize new number for the next pic
public void update_reel_three()
{
reel_three_ypos = -10;
reel_three_number = reel_three_next_number;
reel_three_next_number = reel_three_next1_number;
Generate_Next1_Random_Numbers_reel_three();
}
//==============================================================================
// This function subtracts 20 of the y corodiate of the displayed pics in reel one
// ie. moves them all up the screen a little
public void alter_xy_of_pics_in_reel_one()
{
reel_one_ypos = reel_one_ypos - 10;
reel_one_next_ypos = reel_one_ypos + 100;
reel_one_next1_ypos = reel_one_next_ypos + 100;
}
//==============================================================================
// This function subtracts 20 of the y corodiate of the displayed pics in reel two
// ie. moves them all up the screen a little
public void alter_xy_of_pics_in_reel_two()
{
reel_two_ypos = reel_two_ypos - 10;
reel_two_next_ypos = reel_two_ypos + 100;
reel_two_next1_ypos = reel_two_next_ypos + 100;
}
//==============================================================================
// This function subtracts 20 of the y corodiate of the displayed pics in reel three
// ie. moves them all up the screen a little
public void alter_xy_of_pics_in_reel_three()
{
reel_three_ypos = reel_three_ypos - 10;
reel_three_next_ypos = reel_three_ypos + 100;
reel_three_next1_ypos = reel_three_next_ypos + 100;
}
//==============================================================================
//Draws the first reel. This consists of three pics displayed one above the other.
//The x position is the same for them all.
//The y positions differ by one hundered (this is the height of the pics)
public void draw_reel_one()
{
//Draws the first reel
Draw_reel(g_West_reel, 0, reel_one_number , reel_one_xpos, reel_one_ypos);
//Draws the first reel
Draw_reel(g_West_reel,0, reel_one_next_number , reel_one_xpos, reel_one_next_ypos);
//Draws the first reel
Draw_reel(g_West_reel,0, reel_one_next1_number , reel_one_xpos, reel_one_next1_ypos);
}
//==============================================================================
//Draws the second reel. This consists of three pics displayed one above the other.
//The x position is the same for them all.
//The y positions differ by one hundered (this is the height of the pics)
public void draw_reel_two()
{
//Draws the second reel
Draw_reel(g_Center_reel,1, reel_two_number , reel_two_xpos, reel_two_ypos);
//Draws the second reel
Draw_reel(g_Center_reel,1, reel_two_next_number, reel_two_xpos, reel_two_next_ypos);
//Draws the second reel
Draw_reel(g_Center_reel,1, reel_two_next1_number, reel_two_xpos, reel_two_next1_ypos);
}
//==============================================================================
//Draws the first reel. This consists of three pics displayed one above the other.
//The x position is the same for them all.
//The y positions differ by one hundered (this is the height of the pics)
public void draw_reel_three()
{
//Draws the third reel
Draw_reel(g_East_reel, 2, reel_three_number , reel_three_xpos, reel_three_ypos);
//Draws the third reel
Draw_reel(g_East_reel,2, reel_three_next_number, reel_three_xpos, reel_three_next_ypos);
//Draws the third reel
Draw_reel(g_East_reel,2, reel_three_next1_number, reel_three_xpos, reel_three_next1_ypos);
}
public void Generate_Next1_Random_Numbers_reel_one()
{
reel_one_next1_number =((int)(Math.random()*8));
}
public void Generate_Next1_Random_Numbers_reel_two()
{
reel_two_next1_number =((int)(Math.random()*8));
}
public void Generate_Next1_Random_Numbers_reel_three()
{
reel_three_next1_number =((int)(Math.random()*8));
}
//==============================================================================
// Update function stops the screen from flashing
public void update(Graphics g)
{
paint(g);
}
//==============================================================================
// get the images and puts them in the reels array
public void initialise_reels()
{
//add in the pics to the array
//pics should be 100 by 100
for(int reel_number=0;reel_number < 3;reel_number++)
{
reels[reel_number][0] = getImage(getDocumentBase(),"images/apple.gif");
reels[reel_number][1] = getImage(getDocumentBase(),"images/barone.gif");
reels[reel_number][2] = getImage(getDocumentBase(),"images/cherr.gif");
reels[reel_number][3] = getImage(getDocumentBase(),"images/seven.gif");
reels[reel_number][4] = getImage(getDocumentBase(),"images/bartwo.gif");
reels[reel_number][5] = getImage(getDocumentBase(),"images/strawb.gif");
reels[reel_number][6] = getImage(getDocumentBase(),"images/sevengol.gif");
reels[reel_number][7] = getImage(getDocumentBase(),"images/barthree.gif");
//add images to the tracker
for(int x=0;x<8;x++)
MyTracker.addImage(reels[reel_number][x],0);
}
//get back ground image
img = getImage(getDocumentBase(),"images/blue.gif");
//add the background image to the tracker
MyTracker.addImage(img,0);
//wait until the images have loaded
try
{
MyTracker.waitForID(0);
}
catch(Exception et)
{
System.out.println("loading images");
}
}
//==============================================================================
// sets the postion of the reels
public void set_reel_positons()
{
//sets reel two to be in the middle of the screen
// -50 beacuse the reel is 100 wide.
reel_two_xpos = AppletWidth/2 -50;
reel_one_xpos = reel_two_xpos - int_gap_between_reels - int_picture_width;
reel_three_xpos = reel_two_xpos + int_gap_between_reels + int_picture_width + 30;
}
//==============================================================================
// draws reel one the screen
public void Draw_reel(Graphics the_g,int reel_number,int image_number,int reel_xpos,int reel_ypos)
{
the_g.drawImage(reels[reel_number][image_number],reel_xpos,reel_ypos,this);
}
//=============================================================
//if the applet is resized when all the reels have stopped spining this function
//will stop the reels going blank. Also determines if there is a winning line and
// displays a approprate message.
public void if_the_reels_have_all_stopped_draw_them()
{
//if the reels have stopped
if(stop_one ==1 && stop_two == 1 && stop_three == 1 &&
reel_one_ypos == -50 && reel_two_ypos == -50 && reel_three_ypos == -50)
{ //draw the reels
draw_reel_one();
draw_reel_two();
draw_reel_three();
//if there is no winning line when the reels have stopped make the message say
//"spin reel!"
if(WinLine() == 0)//not a winning line
{
Message_Label.setText("spin reel!");
//The reels have now stopped and the player didn't win so enable them all again
Hold_one.setEnabled(true);
Hold_two.setEnabled(true);
Hold_three.setEnabled(true);
ten_check.setEnabled(true);
twenty_check.setEnabled(true);
fifty_check.setEnabled(true);
}
else//there is a winning line
{ //this if statment ensures that the following code is only called once
//per winning line.
if(Already_Checked == false)
{
//if there is a winning line change the message
Winnings = WinLine();
//add the winnings for this go to the total
Total_Won = Total_Won + Winnings;
//set message saying how much you win
Message_Label.setText(Name_Text.getText() + " you win " + String.valueOf(Winnings) + " pounds");
//set the text for total winnings
Label_one.setText("Winnings: " + String.valueOf(Total_Won) + " pounds");
//the player won so leave the hold buttons disabled
//set all the hold buttons to not holding
held_one = 0;
Hold_one.setLabel("Hold 1");
Hold_one.setBackground(Color.gray);
held_two = 0;
Hold_two.setLabel("Hold 2");
Hold_two.setBackground(Color.gray);
held_three = 0;
Hold_three.setLabel("Hold 3");
Hold_three.setBackground(Color.gray);
Already_Checked = true;
}
}
}
}
//==============================================================================
//returns zero if all of the sybols don't match
//else it returns the amount of winnings.
public int WinLine()
{
if(reel_one_next_number == reel_two_next_number && reel_one_next_number == reel_three_next_number )
return(reel_one_number + 1);
else
return(0);
}
//==============================================================================
// When the reel is held it continues scrolling until the top pic is at a height of -50
// The reel is then set to bounce and then stop.
// This function does the bouncing
public void bounce_reel()
{
//couldn't get this function to work
}
//==============================================================================
// Generate random numbers function is now redundant as the numbers do not need to be displayed
public void Generate_Random_Numbers(Graphics g)
{
String string_Rand_Numbers;
int int_Width_of_string,int_Height_of_string ;
reel_one_number = ((int)(Math.random()*8));
reel_two_number = ((int)(Math.random()*8));
reel_three_number = ((int)(Math.random()*8));
string_Rand_Numbers=reel_one_number +":"+reel_two_number +":"+reel_three_number ;
int_Width_of_string = g.getFontMetrics().stringWidth(string_Rand_Numbers);
int_Height_of_string = g.getFontMetrics().getHeight();
xpos_of_string = AppletWidth/2 - int_Width_of_string/2;
ypos_of_string = int_Height_of_string + 5;
g.drawString(string_Rand_Numbers,xpos_of_string ,20);
}
}