- //Francisco Barrios AP programing mid term
- //11/14/2010
- //Slot matchine casino
- //Note for PB - Paint may be a little off but it's alright. Nothing big
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.image.CropImageFilter;
- import java.awt.image.FilteredImageSource;
- import java.awt.image.ImageFilter;
- import java.awt.image.ImageProducer;
- import java.net.MalformedURLException;
- import javax.swing.JPanel;
- public class SlotMachine extends Applet{
- int p=0;
- int k=0;
- Thread runner;
- Image currImg;
- int random1,random2,random3;
- int cardWidth=150,cardHeight=120;
- Image [] images= new Image[5];
- Image [] image_rule = new Image [1]; //For Rule Photos
- int thisPos;
- Button b1, b2, b3, bPlay, bReset ,betYaz;
- int playCount, winCount , doubleCount = 0;
- int para=1;
- int kazanc=100;
- Label betMon,betMon2,kazLab;
- Label lPlayed, lWon,doublesLab;
- String whichButton;
- TextField tfPlayed, tfWon,bet,kazan,doubles;
- TextArea textArea1 = new TextArea(20,30);
- TextArea textArea2 = new TextArea(5,30);
- public void init() {
- setBackground(Color.lightGray);
- JPanel jp = new JPanel();
- BorderLayout outer = new BorderLayout();
- bPlay = new Button("Play"); // create "Play" button
- bReset = new Button("Reset"); // create "Reset" button
- this.setLayout(outer);
- Panel bottomPanel = new Panel();
- JPanel LeftPanel = new JPanel();
- JPanel cp = new JPanel();
- add("West",LeftPanel);
- add("East",jp);
- add("South",bottomPanel);
- add("Center",cp);
- cp.add(textArea2);
- textArea2.setEditable(false);
- textArea2.setFont(new java.awt.Font("Courier New", 1, 18));
- cp.setLayout(new FlowLayout(FlowLayout.LEADING,180,300));
- bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER,3,100));
- Action MyAction= new Action();
- bPlay.addActionListener(MyAction); // give action bPlay
- bReset.addActionListener(MyAction);
- tfPlayed = new TextField(" "); // space to show text area
- tfPlayed.setText(Integer.toString(playCount));
- lPlayed = new Label("Games Played:");
- tfPlayed.setEditable(false); // set so users can't edit text
- doubles = new TextField(" ");
- doubles.setText(Integer.toString(doubleCount));
- doubles.setEditable(false);
- doublesLab = new Label("Double Wins:");
- tfWon = new TextField(" ");
- tfWon.setText(Integer.toString(winCount));
- lWon = new Label("Games won:");
- tfWon.setEditable(false);
- bet =new TextField(" ");
- kazan = new TextField(" ");
- kazan.setEditable(false);
- bet.setText("1");
- bet.setEditable(true);
- betMon = new Label("Bet:");
- kazLab= new Label("Account:");
- bottomPanel.add(bPlay);
- bottomPanel.add(bReset);
- bottomPanel.add(lPlayed);
- bottomPanel.add(tfPlayed);
- bottomPanel.add(lWon);
- bottomPanel.add(tfWon);
- bottomPanel.add(betMon);
- bottomPanel.add(bet);
- bottomPanel.add(kazLab);
- bottomPanel.add(kazan);
- bottomPanel.add(doublesLab);
- bottomPanel.add(doubles);
- validate();
- setVisible(true);
- kazan.setText(Integer.toString(kazanc));
- ////Rule List Image
- Image rules;
- int rulesPos;
- ImageFilter rulesFilter;
- ImageProducer rulesProducer;
- rules = getImage(getCodeBase(),"rules.gif");
- rulesPos=(0)+1;
- rulesFilter= new CropImageFilter(rulesPos,1,199,349);
- rulesProducer = new FilteredImageSource(rules.getSource(),rulesFilter);
- image_rule[0] = createImage(rulesProducer);
- }
- public void colorNum() throws MalformedURLException
- {
- String q=bet.getText();
- if(q.equals(""))para=1; else para=Integer.parseInt(q);
- int randomCard,cardPos;
- Image playingCards;
- ImageFilter cardFilter;
- ImageProducer cardProducer;
- playingCards=getImage(getCodeBase(),"photos.gif");
- for(int k=0;k<3;k++)
- {
- randomCard=(int) Math.round((Math.random()*4));
- cardPos=(randomCard*cardWidth)+1;
- cardFilter= new CropImageFilter(cardPos,1,cardWidth,cardHeight);
- cardProducer = new FilteredImageSource(playingCards.getSource(),cardFilter);
- images[k]=createImage(cardProducer);
- if(k==0){random1=randomCard;}
- if(k==1){random2=randomCard;}
- if(k==2){random3=randomCard;}
- }
- }
- public void paint(Graphics g) {
- int i,startPT=0;
- ///////////////////////////
- ////////PRINT RULES////////
- startPT=(700);
- g.drawImage(image_rule[0],startPT+77,100,this);
- startPT=0;
- /////////////////////////
- if(p==k){
- int loop;
- for(i=0;i<20;i++){
- try{
- Thread.sleep(50);
- }
- catch(InterruptedException e){
- System.out.println("Sleep Interrupted");
- }
- ///////////////////////////
- ////////PRINT RULES////////
- startPT=(700);
- g.drawImage(image_rule[0],startPT+77,100,this);
- startPT=0;
- /////////////////////////
- loop=(int) Math.round((Math.random()*4));
- g.drawImage(images[loop],startPT+77,100,this);
- loop=(int) Math.round((Math.random()*4));
- g.drawImage(images[loop],startPT+276,100,this);
- loop=(int) Math.round((Math.random()*4));
- g.drawImage(images[loop],startPT+476,100,this);
- }
- p++;
- k++;
- for(i=0;i<3;i++){
- startPT=(i*(cardWidth+50));
- g.drawImage(images[i],startPT+77,100,this);
- g.drawRoundRect(startPT+75,99,cardWidth+2,cardHeight+2,5,5);
- }
- if(random1==random2 && random2==random3)
- { if(playCount==0){textArea2.setText("\n"+"\n"+" You have $100!");}
- else{kazanc+=para*3;
- winCount++;
- kazan.setText(Integer.toString(kazanc));
- tfPlayed.setText(Integer.toString(playCount));
- tfWon.setText(Integer.toString(winCount));
- doubles.setText(Integer.toString(doubleCount));
- textArea2.setText("\n"+"\n"+" You Won by Triple!!!");
- play(getCodeBase(),"applause.au");
- }}
- else if(random1==random2 || random2==random3 || random1==random3){
- kazanc+=para*2;
- doubleCount++;
- kazan.setText(Integer.toString(kazanc));
- tfPlayed.setText(Integer.toString(playCount));
- tfWon.setText(Integer.toString(winCount));
- doubles.setText(Integer.toString(doubleCount));
- textArea2.setText("\n"+"\n"+" You Won by Double!!!");}
- else{
- kazanc=kazanc-para;
- kazan.setText(Integer.toString(kazanc));
- tfPlayed.setText(Integer.toString(playCount));
- textArea2.setText("\n" +"\n" +" You LOST!!!" + "\n" + " Try Again!!!");
- }
- }
- for(i=0;i<3;i++){
- startPT=(i*(cardWidth+50));
- g.drawImage(images[i],startPT+77,100,this);
- g.drawRoundRect(startPT+75,99,cardWidth+2,cardHeight+2,5,5);
- }
- p=p+100;
- }
- private class Action implements ActionListener{
- public void actionPerformed(ActionEvent e) {
- whichButton = e.getActionCommand();
- if(whichButton.equals("Reset")) { // resets everything back to white
- winCount=0; // resets counts to zero
- playCount=0;
- doubleCount=0;
- bet.setText("1");
- kazan.setText("");
- tfPlayed.setText(Integer.toString(playCount));
- tfWon.setText(Integer.toString(winCount));
- doubles.setText(Integer.toString(doubleCount));
- kazanc=100;
- kazan.setText(Integer.toString(kazanc));
- } // end if
- if(whichButton.equals("Play")) {
- play(getCodeBase(),"drop22.au");
- textArea2.setText("\n" +"\n" +" ");
- p=k; //to start paint one time
- Thread.currentThread();
- try {
- colorNum();
- } catch (MalformedURLException e2) {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- }
- String q=bet.getText();
- if(q.equals(""))para=1; else para=Integer.parseInt(q);
- playCount++;
- tfPlayed.setText(Integer.toString(playCount));
- repaint();
- } // end if
- } // end actionPerformed
- } // end Action
- }