Advertisement
Guest User

Ball.java

a guest
May 25th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package tarekeselotto;
  7.  
  8. import java.awt.Color;
  9. import java.awt.FlowLayout;
  10. import java.awt.Graphics;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import javax.swing.JPanel;
  14. import javax.swing.Timer;
  15.  
  16. /**
  17.  *
  18.  * @author isonuZ
  19.  */
  20. public class Ball extends JPanel {
  21.   private boolean[] numbers = new boolean[42];
  22.   public String value;
  23.   public int radius = 15;
  24.   int delay = 100;
  25.   Timer timer = new Timer(delay, new TimerListenerBall());
  26.   int timerCount = 0;
  27.   boolean spinning = false;
  28.  
  29.   public Ball() {
  30.     value = "0";
  31.     this.timer.start();
  32.   }
  33.  
  34.   private void rollWheels() {
  35.     this.spinning = true;
  36.     this.timer.start();
  37.   }
  38.  
  39.   public int getValue(int[] resultArray, int bounds) {
  40.     rollWheels();
  41.     System.out.print("he");
  42.     //while(this.spinning) {}
  43.     System.out.println("llo");
  44.     numbers[Integer.parseInt(value)+1] = false;
  45.     return Integer.parseInt(value);
  46.   }
  47.  
  48.  
  49.  
  50.  
  51.   protected void paintComponent(Graphics g) {
  52.       super.paintComponent(g);
  53.       g.setColor(Color.red);
  54.       g.fillOval(5, 5, 2 * radius, 2 * radius);
  55.      
  56.       g.setColor(Color.white);
  57.       g.fillOval(8, 8,
  58.               2 * radius - 7,
  59.               2 * radius - 7);
  60.      
  61.       g.setColor(Color.black);
  62.       g.drawString(value, radius+1, radius+10);
  63.   }
  64.  
  65.   class TimerListenerBall implements ActionListener {
  66.     /** Handle ActionEvent */
  67.     public void actionPerformed(ActionEvent e) {
  68.       int number;
  69.      
  70.       do {
  71.         number = (int) (Math.random()*42);
  72.       } while(!numbers[number]);
  73.      
  74.       value = number + "";
  75.       timerCount++;
  76.      
  77.       if(timerCount > 15)
  78.         delay = 150;
  79.       if(timerCount > 20)
  80.         delay = 200;
  81.       if(timerCount > 50)
  82.         delay = 1000;
  83.       if(timerCount > 53)
  84.         spinning = false;
  85.      
  86.       if(spinning) {
  87.         timer.setDelay(delay);
  88.         repaint();
  89.       } else {
  90.         timer.stop();
  91.       }
  92.      
  93.       repaint();
  94.     }
  95.   }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement