summaryrefslogtreecommitdiff
path: root/NET/worlds/network/ProgressBar.java
blob: 97d7d6d846cb9ea6d72f4c2e6d9eda36f1e0af12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package NET.worlds.network;

import NET.worlds.console.Console;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;

public class ProgressBar extends Canvas {
   private static final long serialVersionUID = -4391762871660491011L;
   private static final int borderThickness = 1;
   private static final int heightPadding = 2;
   private int barWidth;
   private int barHeight;
   private Font font;
   private int fillWidth = -1;
   private int percent;

   public ProgressBar(int width) {
      this.barWidth = width;
      this.font = new Font(Console.message("DialogFont"), 1, 14);
      FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(this.font);
      this.barHeight = fm.getAscent() + 4 + 2;
   }

   public void setProgress(double amt) {
      int visibleWidth = this.getSize().width;
      if (visibleWidth == 0) {
         visibleWidth = this.barWidth;
      }

      int width = (int)(amt * (visibleWidth - 2));
      if (width != this.fillWidth) {
         this.fillWidth = width;
         this.percent = (int)Math.round(100.0 * amt);
         this.repaint();
      }
   }

   @Override
   public Dimension preferredSize() {
      return new Dimension(this.barWidth, this.barHeight);
   }

   @Override
   public Dimension minimumSize() {
      return this.preferredSize();
   }

   @Override
   public boolean handleEvent(Event event) {
      return event.id == 201 ? true : super.handleEvent(event);
   }

   @Override
   public void paint(Graphics g) {
      Dimension size = this.getSize();
      int width = size.width;
      int height = size.height;
      g.setColor(Color.lightGray);
      g.draw3DRect(0, 0, width - 1, height - 1, true);
      if (this.fillWidth > 0) {
         g.setColor(Color.blue);
         g.fillRect(1, 1, this.fillWidth, height - 2);
      }

      String text = this.percent + "%";
      g.setFont(this.font);
      FontMetrics fm = g.getFontMetrics();
      int textWidth = fm.stringWidth(text);
      g.setColor(Color.black);
      g.drawString(text, (width - textWidth) / 2, (height + fm.getAscent()) / 2 - 1);
   }
}