diff options
| author | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
| commit | c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch) | |
| tree | df9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/network/ProgressBar.java | |
| download | worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip | |
Initial commit
Diffstat (limited to 'NET/worlds/network/ProgressBar.java')
| -rw-r--r-- | NET/worlds/network/ProgressBar.java | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/NET/worlds/network/ProgressBar.java b/NET/worlds/network/ProgressBar.java new file mode 100644 index 0000000..97d7d6d --- /dev/null +++ b/NET/worlds/network/ProgressBar.java @@ -0,0 +1,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); + } +} |