Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
DealerTimer - TimerCanvas.java

TimerCanvas.java

Caricato da:
Scarica il programma completo

  1. package dealertimer.graphics;
  2.  
  3. import dealertimer.Action;
  4. import dealertimer.Button;
  5. import dealertimer.DealerTimer;
  6. import dealertimer.Point;
  7. import java.io.IOException;
  8. import javax.microedition.lcdui.Canvas;
  9. import javax.microedition.lcdui.Display;
  10. import javax.microedition.lcdui.Font;
  11. import javax.microedition.lcdui.Graphics;
  12. import javax.microedition.media.Manager;
  13. import javax.microedition.media.MediaException;
  14. import javax.microedition.media.Player;
  15.  
  16. public class TimerCanvas extends Canvas {
  17.  
  18.     private class Control implements Runnable{
  19.  
  20.         private Thread start;
  21.  
  22.         public void start(){
  23.             this.start=new Thread(this);
  24.             this.start.start();
  25.         }
  26.  
  27.         public void run() {
  28.             while(true){
  29.                 try {
  30.                     if (timer.isEndeed()) {
  31.                         try {
  32.                             playTimer();
  33.                         } catch (MediaException ex) {
  34.                             ex.printStackTrace();
  35.                         }
  36.                     }
  37.                     Thread.sleep(500);
  38.                 } catch (InterruptedException ex) {
  39.                     ex.printStackTrace();
  40.                 }
  41.                 repaint();
  42.             }
  43.         }
  44.  
  45.     }
  46.  
  47.     private DealerTimer timer;
  48.     private Display display;
  49.     private Button addMinute;
  50.     private Button startStop;
  51.     private Button pause;
  52.     private Button reset;
  53.     private Player end;
  54.     private Control controller;
  55.  
  56.     public TimerCanvas(Display pDisplay) throws IOException, MediaException {
  57.         this.timer = new DealerTimer();
  58.         this.display = pDisplay;
  59.         int buttonSize=this.getWidth()/4;
  60.         this.addMinute=new Button(new Action() {
  61.  
  62.             public void executeCode() {
  63.                 timer.addMinute();
  64.                 repaint();
  65.             }
  66.         }, buttonSize, buttonSize, "1.MIN", new Point(0, 4*(this.getHeight()/5)));
  67.         this.startStop=new Button(new Action() {
  68.  
  69.             public void executeCode() {
  70.                 timer.start();
  71.                 String label="2.STOP";
  72.                 if(timer.getStop()){
  73.                     label="2.START";
  74.                 }
  75.                 startStop.setLabel(label);
  76.                 repaint();
  77.             }
  78.         }, buttonSize, buttonSize, "2.START", new Point(buttonSize, 4*(this.getHeight()/5)));
  79.         this.pause=new Button(new Action() {
  80.  
  81.             public void executeCode() {
  82.                 timer.pause();
  83.                 String label="3.RESUME";
  84.                 if(!timer.getPause()){
  85.                     label="3.PAUSE";
  86.                 }
  87.                 pause.setLabel(label);
  88.                 repaint();
  89.             }
  90.         }, buttonSize, buttonSize, "3.PAUSE", new Point(2*buttonSize, 4*(this.getHeight()/5)));
  91.         this.reset=new Button(new Action() {
  92.  
  93.             public void executeCode() {
  94.                 timer.reset();
  95.                 repaint();
  96.             }
  97.         }, buttonSize, buttonSize, "4.RESET", new Point(3*buttonSize, 4*(this.getHeight()/5)));
  98.         this.end=Manager.createPlayer(getClass().getResourceAsStream("/dealertimer/sounds/timer.wav"), "audio/X-wav");
  99.         this.controller=new Control();
  100.         this.controller.start();
  101.         this.setFullScreenMode(true);
  102.     }
  103.  
  104.     protected void paint(Graphics g) {
  105.         g.setColor(255, 255, 255);
  106.         g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD+Font.STYLE_UNDERLINED, Font.SIZE_LARGE));
  107.         g.fillRect(0, 0, this.getWidth(), this.getHeight());
  108.         g.setColor(0, 0, 0);
  109.         g.drawString("Dealer Timer", (this.getWidth()-g.getFont().stringWidth("Dealer Timer"))/2 , this.getHeight() / 5-g.getFont().getHeight()-3, 0);
  110.         g.drawRect(this.getWidth() / 4, this.getHeight() / 5, 2 * (this.getWidth() / 4), this.getHeight() / 5);
  111.         g.setColor(0, 200, 90);
  112.         g.fillRect((this.getWidth() / 4)+1, (this.getHeight() / 5)+1, (2 * (this.getWidth() / 4))-1, (this.getHeight() / 5)-1);
  113.         g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
  114.         String time="";
  115.         if(this.timer.getMinutes()>=10){
  116.             time=this.timer.getMinutes()+"";
  117.         }else{
  118.             time="0"+this.timer.getMinutes();
  119.         }
  120.         time+=" : ";
  121.         if(this.timer.getSeconds()>=10){
  122.             time+=this.timer.getSeconds();
  123.         }else{
  124.             time+="0"+this.timer.getSeconds();
  125.         }
  126.         g.setColor(0, 0, 0);
  127.         g.drawString(time, (this.getWidth()-g.getFont().stringWidth(time))/2, (this.getHeight() / 5)+((this.getHeight()/5)-g.getFont().getHeight())/2, 0);
  128.         String level="Buio: "+this.timer.getLevel();
  129.         g.drawString(level, (this.getWidth()-g.getFont().stringWidth(level))/2, 2*(this.getHeight() / 5)+((this.getHeight()/5)-g.getFont().getHeight())/2, 0);
  130.         g.drawImage(this.addMinute.getGraphics(), this.addMinute.getLocation().x, this.addMinute.getLocation().y, 0);
  131.         g.drawImage(this.pause.getGraphics(), this.pause.getLocation().x, this.pause.getLocation().y, 0);
  132.         g.drawImage(this.startStop.getGraphics(), this.startStop.getLocation().x, this.startStop.getLocation().y, 0);
  133.         g.drawImage(this.reset.getGraphics(), this.reset.getLocation().x, this.reset.getLocation().y, 0);
  134.     }
  135.  
  136.     protected void keyPressed(int keyCode) {
  137.         if(keyCode==Canvas.KEY_NUM1){
  138.             this.addMinute.pressButton();
  139.         }else if(keyCode==Canvas.KEY_NUM2){
  140.             this.startStop.pressButton();
  141.         }else if(keyCode==Canvas.KEY_NUM3){
  142.             this.pause.pressButton();
  143.         }else if(keyCode==Canvas.KEY_NUM4){
  144.             this.reset.pressButton();
  145.         }
  146.     }
  147.  
  148.     protected void pointerPressed(int x, int y) {
  149.         Point in=new Point(x, y);
  150.         if(this.addMinute.isIn(in)){
  151.             this.addMinute.pressButton();
  152.         }else if(this.startStop.isIn(in)){
  153.             this.startStop.pressButton();
  154.         }else if(this.pause.isIn(in)){
  155.             this.pause.pressButton();
  156.         }else if(this.reset.isIn(in)){
  157.             this.reset.pressButton();
  158.         }
  159.     }
  160.  
  161.     public void playTimer() throws MediaException{
  162.         this.display.vibrate(1000);
  163.         this.end.start();
  164.     }
  165.  
  166.  
  167. }