1 | /******************************************************************************
|
---|
2 | *
|
---|
3 | * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
|
---|
4 | * www.mindbright.se, [email protected]
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program 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
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | *****************************************************************************
|
---|
17 | * $Author: mats $
|
---|
18 | * $Date: 2000/03/07 16:44:34 $
|
---|
19 | * $Name: rel1-2-1 $
|
---|
20 | *****************************************************************************/
|
---|
21 | package mindbright.gui;
|
---|
22 |
|
---|
23 | import java.awt.*;
|
---|
24 |
|
---|
25 | public class ProgressBar extends Canvas {
|
---|
26 | int max = 0;
|
---|
27 | int current = 0;
|
---|
28 | int width = 0;
|
---|
29 | int height = 0;
|
---|
30 | Color barColor;
|
---|
31 |
|
---|
32 | FontMetrics fm;
|
---|
33 |
|
---|
34 | Image img;
|
---|
35 | Graphics memG;
|
---|
36 |
|
---|
37 | public synchronized void setBarColor(Color c) {
|
---|
38 | barColor = c;
|
---|
39 | }
|
---|
40 | public synchronized void setValue(int v) {
|
---|
41 | setValue(v, false);
|
---|
42 | }
|
---|
43 | public synchronized void setValue(int v, boolean repaintNow) {
|
---|
44 | current = (v > max ? max : v);
|
---|
45 | if(repaintNow) {
|
---|
46 | this.update(getGraphics());
|
---|
47 | } else {
|
---|
48 | repaint();
|
---|
49 | }
|
---|
50 | }
|
---|
51 | public synchronized void setMax(int max, boolean reset) {
|
---|
52 | this.max = max;
|
---|
53 | if(reset)
|
---|
54 | current = 0;
|
---|
55 | setValue(current, true);
|
---|
56 | }
|
---|
57 | public Dimension getPreferredSize() {
|
---|
58 | return new Dimension(width, height);
|
---|
59 | }
|
---|
60 | public ProgressBar(int max, int width, int height) {
|
---|
61 | super();
|
---|
62 | this.max = max;
|
---|
63 | this.width = width;
|
---|
64 | this.height = height;
|
---|
65 | barColor = Color.black;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public void update(Graphics g) {
|
---|
69 | paint(g);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public synchronized void paint(Graphics g) {
|
---|
73 | int d = (max != 0 ? max : 1);
|
---|
74 | double perc = ((double)current * 100.0) / (double)d;
|
---|
75 | String p = ((int)perc) + "%";
|
---|
76 | int w = (current * (width - 2)) / d;
|
---|
77 |
|
---|
78 | if(fm == null) {
|
---|
79 | fm = g.getFontMetrics(g.getFont());
|
---|
80 | }
|
---|
81 |
|
---|
82 | if(img == null) {
|
---|
83 | setBackground(Color.white);
|
---|
84 | img = createImage(width, height);
|
---|
85 | memG = img.getGraphics();
|
---|
86 | }
|
---|
87 |
|
---|
88 | memG.setPaintMode();
|
---|
89 | memG.setColor(Color.white);
|
---|
90 | memG.fillRect(0, 0, width, height);
|
---|
91 | memG.setColor(Color.black);
|
---|
92 |
|
---|
93 | memG.drawRect(0, 0, width - 1, height - 1);
|
---|
94 | memG.drawString(p, (width / 2) - (fm.stringWidth(p) / 2) + 1, (height / 2) + fm.getMaxAscent() + fm.getLeading() - (fm.getHeight() / 2));
|
---|
95 | memG.setColor(barColor);
|
---|
96 | memG.setXORMode(Color.white);
|
---|
97 | memG.fillRect(1, 1, w, height - 2);
|
---|
98 |
|
---|
99 | g.drawImage(img, 0, 0, this);
|
---|
100 | }
|
---|
101 | }
|
---|