KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > microedition > lcdui > Gauge


1
2 /*
3  * MicroEmulator
4  * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contributor(s):
21  * 3GLab
22  */

23  
24 package javax.microedition.lcdui;
25
26 import com.barteo.emulator.device.DeviceFactory;
27
28
29 public class Gauge extends Item
30 {
31   static int HEIGHT = 15;
32   
33   int value;
34   int maxValue;
35   boolean interactive;
36
37
38   public Gauge(String label, boolean interactive, int maxValue, int initialValue)
39   {
40     super(label);
41     
42     this.interactive = interactive;
43     
44     setMaxValue(maxValue);
45     setValue(initialValue);
46   }
47
48
49   public void setValue(int value)
50   {
51     if (value < 0) {
52       value = 0;
53     }
54     if (value > maxValue) {
55       value = maxValue;
56     }
57     
58     this.value = value;
59     repaint();
60   }
61
62
63   public int getValue()
64   {
65     return value;
66   }
67
68
69   public void setMaxValue(int maxValue)
70   {
71     if (maxValue <= 0) {
72       throw new IllegalArgumentException();
73     }
74     
75     this.maxValue = maxValue;
76     setValue(getValue());
77   }
78
79
80   public int getMaxValue()
81   {
82     return maxValue;
83   }
84
85
86   public boolean isInteractive()
87   {
88     return interactive;
89   }
90
91
92   public void setLabel(String label)
93   {
94     super.setLabel(label);
95   }
96
97
98     int getHeight()
99     {
100         return super.getHeight() + HEIGHT;
101     }
102
103   
104     boolean isFocusable()
105     {
106         return interactive;
107     }
108
109   
110   void keyPressed(int keyCode)
111   {
112     if (Display.getGameAction(keyCode) == Canvas.LEFT && value > 0) {
113       value--;
114       repaint();
115     } else if (Display.getGameAction(keyCode) == Canvas.RIGHT && value < maxValue) {
116       value++;
117       repaint();
118     }
119   }
120
121   
122   int paint(Graphics g)
123   {
124     super.paintContent(g);
125     
126         g.translate(0, super.getHeight());
127     
128         if (hasFocus()) {
129       g.drawRect(2, 2,
130           DeviceFactory.getDevice().getDeviceDisplay().getWidth() - 5, HEIGHT - 5);
131     }
132     
133     int width = (DeviceFactory.getDevice().getDeviceDisplay().getWidth() - 8) * value / maxValue;
134     g.fillRect(4, 4, width, HEIGHT - 8);
135         g.translate(0, -super.getHeight());
136     
137     return getHeight();
138   }
139
140
141     int traverse(int gameKeyCode, int top, int bottom, boolean action)
142     {
143         if (gameKeyCode == Canvas.UP) {
144             if (top > 0) {
145                 return -top;
146             } else {
147                 return Item.OUTOFITEM;
148             }
149         }
150         if (gameKeyCode == Canvas.DOWN) {
151             if (getHeight() > bottom) {
152                 return getHeight() - bottom;
153             } else {
154                 return Item.OUTOFITEM;
155             }
156         }
157
158         return 0;
159     }
160   
161 }
162
Popular Tags