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/console/ProgressBar.java | |
| download | worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip | |
Initial commit
Diffstat (limited to 'NET/worlds/console/ProgressBar.java')
| -rw-r--r-- | NET/worlds/console/ProgressBar.java | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/NET/worlds/console/ProgressBar.java b/NET/worlds/console/ProgressBar.java new file mode 100644 index 0000000..c4159d7 --- /dev/null +++ b/NET/worlds/console/ProgressBar.java @@ -0,0 +1,142 @@ +package NET.worlds.console; + +import NET.worlds.network.CacheEntry; +import java.awt.Button; +import java.awt.Canvas; +import java.awt.Color; +import java.awt.Event; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Frame; +import java.awt.Graphics; +import java.awt.GridLayout; +import java.awt.Image; +import java.awt.Label; +import java.awt.Panel; +import java.awt.TextField; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class ProgressBar extends Frame implements ActionListener { + private static final long serialVersionUID = -5057984821909220829L; + private int _total; + private int _current = 0; + private boolean _offline = false; + private Canvas _barCanvas; + private Label _header; + private TextField _message; + private Button _offlineButton; + private static final int BAR_WIDTH = 250; + private static final int BAR_HEIGHT = 20; + private static final int BORDER = 10; + + public ProgressBar(String header, int total) { + this._header = new Label(header); + this._total = total; + if (this._total < 1) { + this._total = 1; + } + + this.build(); + this.setTitle("Loading Progress"); + } + + public void advance() { + this._current++; + if (this._current > this._total) { + this._current = this._total; + } + + this.paintBar(); + } + + public int current() { + return this._current; + } + + public int total() { + return this._total; + } + + public int percentComplete() { + return this._current * 100 / this._total; + } + + public boolean offline() { + return this._offline; + } + + public void setMessage(String m) { + this._message.setText(m); + this.update(this.getGraphics()); + } + + protected int pixelsComplete() { + return this._current * 250 / this._total; + } + + protected void build() { + this.setLayout(new GridLayout(3, 1, 10, 0)); + this._barCanvas = new Canvas(); + this._barCanvas.setSize(270, 40); + this.add(this._barCanvas); + this._message = new TextField("Working..."); + this._message.setEditable(false); + this.add(this._message); + Panel buttonPanel = new Panel(); + this._offlineButton = new Button("Go Offline"); + this._offlineButton.addActionListener(this); + buttonPanel.add(this._offlineButton); + this.add(buttonPanel); + this.pack(); + } + + @Override + public void paint(Graphics g) { + this.paintBar(); + } + + @Override + public void actionPerformed(ActionEvent ae) { + if (ae.getSource() == this._offlineButton) { + this._offline = true; + CacheEntry.setOffline(); + this._offlineButton.setEnabled(false); + } + } + + @Override + public boolean handleEvent(Event e) { + if (e.id == 201) { + this.setVisible(false); + this.dispose(); + Main.end(); + throw new Error("User cancelled startup."); + } else { + return super.handleEvent(e); + } + } + + protected void paintBar() { + Graphics g = this._barCanvas.getGraphics(); + Image off = this.createImage(270, 40); + Graphics offG = off.getGraphics(); + offG.setColor(this.getBackground()); + offG.fillRect(0, 0, 270, 40); + offG.setColor(Color.gray); + offG.fillRect(10, 10, this.pixelsComplete(), 20); + offG.setColor(Color.darkGray); + offG.draw3DRect(11, 11, this.pixelsComplete() - 1, 18, true); + offG.setColor(Color.black); + offG.drawRect(10, 10, 250, 20); + String perString = Integer.toString(this.percentComplete()) + "% Complete"; + g.setFont(new Font("Times Roman", 0, 12)); + FontMetrics fm = g.getFontMetrics(); + int halfHeight = fm.getHeight() / 2; + int halfWidth = fm.stringWidth(perString) / 2; + offG.drawString(perString, 135 - halfWidth, 20 + halfHeight); + g.drawImage(off, 0, 0, null); + offG.dispose(); + off.flush(); + } +} |