Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 7.62 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //Francisco Barrios AP programing mid term
  2. //11/14/2010
  3. //Slot matchine casino
  4. //Note for PB - Paint may be a little off but it's alright. Nothing big
  5. import java.applet.Applet;
  6. import java.awt.*;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.image.CropImageFilter;
  10. import java.awt.image.FilteredImageSource;
  11. import java.awt.image.ImageFilter;
  12. import java.awt.image.ImageProducer;
  13. import java.net.MalformedURLException;
  14. import javax.swing.JPanel;
  15.  
  16.  
  17. public  class SlotMachine extends Applet{
  18.         int p=0;
  19.         int k=0;
  20.         Thread runner;
  21.         Image currImg;
  22.         int random1,random2,random3;
  23.         int cardWidth=150,cardHeight=120;
  24.         Image [] images= new Image[5];
  25.         Image [] image_rule = new Image [1]; //For Rule Photos
  26.         int thisPos;
  27.         Button b1, b2, b3, bPlay, bReset ,betYaz;
  28.         int playCount, winCount , doubleCount = 0;
  29.         int para=1;
  30.         int kazanc=100;
  31.         Label betMon,betMon2,kazLab;
  32.         Label lPlayed, lWon,doublesLab;
  33.         String whichButton;
  34.         TextField tfPlayed, tfWon,bet,kazan,doubles;
  35.         TextArea textArea1 = new TextArea(20,30);
  36.         TextArea textArea2 = new TextArea(5,30);
  37. public void init() {
  38.  
  39.         setBackground(Color.lightGray);
  40.         JPanel jp = new JPanel();
  41.         BorderLayout outer = new BorderLayout();
  42.         bPlay = new Button("Play"); // create "Play" button
  43.         bReset = new Button("Reset"); // create "Reset" button
  44.          this.setLayout(outer);
  45.                
  46.                 Panel bottomPanel = new Panel();
  47.                 JPanel LeftPanel = new JPanel();
  48.                 JPanel cp = new JPanel();
  49.                 add("West",LeftPanel);
  50.                 add("East",jp);
  51.                 add("South",bottomPanel);
  52.                 add("Center",cp);
  53.                 cp.add(textArea2);
  54.                 textArea2.setEditable(false);
  55.                 textArea2.setFont(new java.awt.Font("Courier New", 1, 18));
  56.                 cp.setLayout(new FlowLayout(FlowLayout.LEADING,180,300));
  57.                 bottomPanel.setLayout(new FlowLayout(FlowLayout.CENTER,3,100));
  58.        
  59.         Action MyAction= new  Action();
  60.         bPlay.addActionListener(MyAction); // give action bPlay
  61.         bReset.addActionListener(MyAction);
  62.        
  63.        
  64.         tfPlayed = new TextField("   "); // space to show text area
  65.         tfPlayed.setText(Integer.toString(playCount));
  66.         lPlayed = new Label("Games Played:");
  67.         tfPlayed.setEditable(false); // set so users can't edit text
  68.         doubles = new TextField("   ");
  69.         doubles.setText(Integer.toString(doubleCount));
  70.         doubles.setEditable(false);
  71.         doublesLab = new Label("Double Wins:");
  72.         tfWon = new TextField("   ");
  73.         tfWon.setText(Integer.toString(winCount));
  74.         lWon = new Label("Games won:");
  75.         tfWon.setEditable(false);
  76.         bet =new TextField("     ");
  77.         kazan = new TextField("   ");
  78.         kazan.setEditable(false);
  79.         bet.setText("1");
  80.         bet.setEditable(true);
  81.        
  82.  
  83.         betMon = new Label("Bet:");
  84.         kazLab= new Label("Account:");
  85.         bottomPanel.add(bPlay);
  86.         bottomPanel.add(bReset);
  87.         bottomPanel.add(lPlayed);
  88.         bottomPanel.add(tfPlayed);
  89.         bottomPanel.add(lWon);
  90.         bottomPanel.add(tfWon);
  91.         bottomPanel.add(betMon);
  92.         bottomPanel.add(bet);
  93.         bottomPanel.add(kazLab);
  94.         bottomPanel.add(kazan);
  95.         bottomPanel.add(doublesLab);
  96.         bottomPanel.add(doubles);
  97.         validate();
  98.         setVisible(true);
  99.         kazan.setText(Integer.toString(kazanc));
  100.    
  101.        
  102.  
  103.         ////Rule List Image
  104.         Image rules;
  105.         int rulesPos;
  106.         ImageFilter rulesFilter;
  107.         ImageProducer rulesProducer;
  108.     rules = getImage(getCodeBase(),"rules.gif");
  109.     rulesPos=(0)+1;
  110.         rulesFilter= new CropImageFilter(rulesPos,1,199,349);
  111.         rulesProducer = new FilteredImageSource(rules.getSource(),rulesFilter);
  112.     image_rule[0] = createImage(rulesProducer);
  113.    
  114. }
  115.  
  116. public void colorNum() throws MalformedURLException
  117. {  
  118.         String q=bet.getText();
  119.     if(q.equals(""))para=1; else para=Integer.parseInt(q);
  120.         int randomCard,cardPos;
  121.         Image playingCards;
  122.         ImageFilter cardFilter;
  123.         ImageProducer cardProducer;
  124.         playingCards=getImage(getCodeBase(),"photos.gif");
  125.         for(int k=0;k<3;k++)
  126.         {
  127.         randomCard=(int) Math.round((Math.random()*4));
  128.        
  129.         cardPos=(randomCard*cardWidth)+1;
  130.         cardFilter= new CropImageFilter(cardPos,1,cardWidth,cardHeight);
  131.         cardProducer = new FilteredImageSource(playingCards.getSource(),cardFilter);
  132.         images[k]=createImage(cardProducer);
  133.         if(k==0){random1=randomCard;}
  134.         if(k==1){random2=randomCard;}
  135.         if(k==2){random3=randomCard;}
  136.        
  137.         }      
  138.  
  139. }
  140.  
  141. public void paint(Graphics g) {
  142.         int i,startPT=0;
  143.        
  144.     ///////////////////////////
  145.         ////////PRINT RULES////////
  146.         startPT=(700);
  147.         g.drawImage(image_rule[0],startPT+77,100,this);
  148.         startPT=0;
  149.         /////////////////////////
  150.        
  151.         if(p==k){
  152.         int loop;
  153.         for(i=0;i<20;i++){
  154.                 try{
  155.               Thread.sleep(50);
  156.     }
  157.     catch(InterruptedException e){
  158.     System.out.println("Sleep Interrupted");
  159.     }
  160.     ///////////////////////////
  161.         ////////PRINT RULES////////
  162.         startPT=(700);
  163.         g.drawImage(image_rule[0],startPT+77,100,this);
  164.         startPT=0;
  165.         /////////////////////////
  166.      
  167.     loop=(int) Math.round((Math.random()*4));
  168.     g.drawImage(images[loop],startPT+77,100,this);
  169.     loop=(int) Math.round((Math.random()*4));
  170.     g.drawImage(images[loop],startPT+276,100,this);
  171.     loop=(int) Math.round((Math.random()*4));
  172.     g.drawImage(images[loop],startPT+476,100,this);
  173.  
  174.  
  175.         }
  176.         p++;
  177.     k++;
  178.        
  179.         for(i=0;i<3;i++){
  180.                 startPT=(i*(cardWidth+50));
  181.                 g.drawImage(images[i],startPT+77,100,this);
  182.                 g.drawRoundRect(startPT+75,99,cardWidth+2,cardHeight+2,5,5);
  183.         }
  184.                
  185.                
  186.                
  187.                 if(random1==random2 && random2==random3)
  188.                 {       if(playCount==0){textArea2.setText("\n"+"\n"+"             You have $100!");}
  189.                     else{kazanc+=para*3;
  190.                         winCount++;
  191.                         kazan.setText(Integer.toString(kazanc));
  192.                         tfPlayed.setText(Integer.toString(playCount));
  193.                         tfWon.setText(Integer.toString(winCount));
  194.                         doubles.setText(Integer.toString(doubleCount));
  195.                     textArea2.setText("\n"+"\n"+"           You Won by Triple!!!");
  196.                     play(getCodeBase(),"applause.au");
  197.                 }}
  198.                 else if(random1==random2 || random2==random3 || random1==random3){
  199.                         kazanc+=para*2;
  200.                         doubleCount++;
  201.                         kazan.setText(Integer.toString(kazanc));
  202.                         tfPlayed.setText(Integer.toString(playCount));
  203.                         tfWon.setText(Integer.toString(winCount));
  204.                         doubles.setText(Integer.toString(doubleCount));
  205.                        
  206.                         textArea2.setText("\n"+"\n"+"           You Won by Double!!!");}
  207.                 else{
  208.                         kazanc=kazanc-para;
  209.                         kazan.setText(Integer.toString(kazanc));
  210.                         tfPlayed.setText(Integer.toString(playCount));
  211.                         textArea2.setText("\n" +"\n" +"                   You LOST!!!" + "\n" + "                   Try Again!!!");
  212.        
  213.                 }
  214.         }
  215.  
  216.         for(i=0;i<3;i++){
  217.                 startPT=(i*(cardWidth+50));
  218.                 g.drawImage(images[i],startPT+77,100,this);
  219.                 g.drawRoundRect(startPT+75,99,cardWidth+2,cardHeight+2,5,5);
  220.         }
  221.         p=p+100;
  222.  
  223.  
  224. }
  225.  
  226. private class Action implements ActionListener{
  227.  
  228.         public void actionPerformed(ActionEvent e) {
  229.        
  230.  
  231.                 whichButton = e.getActionCommand();
  232.  
  233.                         if(whichButton.equals("Reset")) { // resets everything back to white
  234.                        
  235.  
  236.                                 winCount=0; // resets counts to zero
  237.                                 playCount=0;
  238.                                 doubleCount=0;
  239.                                 bet.setText("1");
  240.                                 kazan.setText("");
  241.                                 tfPlayed.setText(Integer.toString(playCount));
  242.                                 tfWon.setText(Integer.toString(winCount));
  243.                                 doubles.setText(Integer.toString(doubleCount));
  244.                                 kazanc=100;
  245.                                 kazan.setText(Integer.toString(kazanc));
  246.                         } // end if
  247.  
  248.                         if(whichButton.equals("Play")) {
  249.                                 play(getCodeBase(),"drop22.au");
  250.                                 textArea2.setText("\n" +"\n" +"            ");
  251.  
  252.                                 p=k; //to start paint one time
  253.  
  254.                                 Thread.currentThread();
  255.                            
  256.                                 try {
  257.                                         colorNum();
  258.                                 } catch (MalformedURLException e2) {
  259.                                         // TODO Auto-generated catch block
  260.                                         e2.printStackTrace();
  261.                                 }
  262.                                 String q=bet.getText();
  263.                                 if(q.equals(""))para=1; else para=Integer.parseInt(q);
  264.                                 playCount++;
  265.                                 tfPlayed.setText(Integer.toString(playCount));
  266.                                
  267.                        
  268.                                 repaint();
  269.                        
  270.                        
  271.                                
  272.                         } // end if
  273.                        
  274.                        
  275.                        
  276.         } // end actionPerformed
  277.  
  278. } // end Action
  279.  
  280.  
  281. }