Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / client / BatteryIcon.java @ 638

History | View | Annotate | Download (2.65 KB)

1

    
2
import javax.swing.*;
3
import java.awt.*;
4

    
5
/**
6
*         BatteryIcon class
7
*         Graphical representation of battery level
8
*/
9
public class BatteryIcon implements Icon {
10
        private int width;
11
        private int height;
12
        private int level;
13

    
14
        /**
15
        * Constructs a new BatteryIcon with all default parameters.
16
        * Default width and height are 50.
17
        * Default level is 100.
18
        */
19
        public BatteryIcon(){
20
                this(100, 50, 50);
21
        }
22

    
23
        /**
24
        * Constructs a new BatteryIcon with default width and height, and with the specified level.
25
        * Default width and height are 50.
26
        */
27
        public BatteryIcon(int startLevel){
28
                this(startLevel, 50, 50);
29
        }
30

    
31
        /**
32
        * Constructs a new BatteryIcon with the specified level, width, and height.
33
        */
34
        public BatteryIcon(int startLevel, int w, int h){
35
                level = startLevel;
36
                width = w;
37
                height = h;
38
        }
39

    
40
        public void paintIcon(Component c, Graphics g, int x, int y) {
41
                Graphics2D g2d = (Graphics2D) g.create();
42
                //clear the background
43
                g2d.setColor(Color.WHITE);
44
                g2d.fillRect(x + 1, y + 1, width - 2, height - 2);
45
                //outline
46
                g2d.setColor(Color.BLACK);
47
                g2d.drawRect((int)(x + width*.3), y + 2, (int)(width*.4), height - 4);
48
                //battery life rectangle
49

    
50
                if (level > 50)
51
                        g2d.setColor(Color.GREEN);
52
                else if (level > 25)
53
                        g2d.setColor(Color.YELLOW);
54
                else
55
                        g2d.setColor(Color.RED);
56

    
57
                int greenX = (int)(x + 1 + width*.3);
58
                int greenY = (int)((y+3) + Math.abs(level-100.0)*(height-6)/(100));
59
                int greenWidth = (int)(width*.4 - 2)+1;
60
                int greenHeight = 1+(int)(level-0.0)*(height-6)/(100);
61
                g2d.fillRect(greenX, greenY, greenWidth, greenHeight);
62
                //text
63
                g2d.setColor(Color.BLACK);
64
                g2d.drawString(level + "%", greenX + greenWidth/2 - 10, greenY + greenHeight/2 + 5);
65

    
66
                g2d.dispose();
67
        }
68

    
69
        /**
70
        * Sets the battery level for this BatteryIcon. The level should be given in raw form, i.e. 0-255 directly
71
        * from the robot. The value will be converted to a representative percentage automatically.
72
        *
73
        * @param newLevel the new battery reading from the robot that this BatteryIcon will display.
74
        */
75
        public void setLevel(int newLevel) {
76
                level = convert(newLevel);
77
        }
78

    
79
        public int getIconWidth() {
80
                return width;
81
        }
82

    
83
        public int getIconHeight() {
84
                return height;
85
        }
86

    
87
        /**
88
        * Converts a robot battery reading into representable form.
89
        * Readings from the robot are returned as raw values, 0-255. This method converts the reading into a value
90
        * from 0 to 100 so that the practical remaining charge is represented.
91
        *
92
        * @param x The battery level as returned by the robot.
93
        * @return The representable battery percentage.
94
        */
95
        public int convert (int x) {
96
            final int DEAD = 110;
97
            final int FULL = 170;
98
            if (x < DEAD)
99
                return 1;
100
                return (int) ((x-DEAD) * 100 / (FULL-DEAD));
101
        }
102
}