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/ImageCanvas.java | |
| download | worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip | |
Initial commit
Diffstat (limited to 'NET/worlds/console/ImageCanvas.java')
| -rw-r--r-- | NET/worlds/console/ImageCanvas.java | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/NET/worlds/console/ImageCanvas.java b/NET/worlds/console/ImageCanvas.java new file mode 100644 index 0000000..9610438 --- /dev/null +++ b/NET/worlds/console/ImageCanvas.java @@ -0,0 +1,193 @@ +package NET.worlds.console; + +import NET.worlds.network.URL; +import NET.worlds.scape.BGLoaded; +import NET.worlds.scape.BackgroundLoader; +import NET.worlds.scape.Room; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Graphics; +import java.awt.Image; +import java.awt.MediaTracker; +import java.awt.Toolkit; + +public class ImageCanvas extends Component implements BGLoaded { + private static final long serialVersionUID = 1687513733496926512L; + protected URL imageURL_; + protected Image image_; + protected boolean loaded_; + protected Dimension dim_; + protected static final URL defaultImageURL = URL.make("home:..\\default.gif"); + + public ImageCanvas(URL inURL) { + this.setNewImage(inURL); + } + + protected ImageCanvas() { + } + + public ImageCanvas(String nm) { + this.loadImage(nm); + } + + private Image loadImage(String nm) { + this.imageURL_ = URL.make("home:" + nm); + this.image_ = this.loadLocalImage(this.imageURL_.unalias()); + if (this.image_ == null) { + this.imageURL_ = URL.make("home:..\\" + nm); + this.image_ = this.loadLocalImage(this.imageURL_.unalias()); + } + + if (this.image_ == null) { + this.setNewImage(URL.make(nm)); + } + + return this.image_; + } + + public void setNewImage(URL newImageURL, Graphics g) { + this.setNewImage(newImageURL); + } + + public void setNewImage(URL newImageURL) { + this.imageURL_ = newImageURL; + this.flushImage(); + this.loaded_ = false; + this.loadRemoteImage(); + } + + protected void flushImage() { + if (this.image_ != null) { + this.image_.flush(); + this.image_ = null; + } + } + + @Override + public void update(Graphics g) { + if (g != null) { + Dimension d = this.getSize(); + if (this.image_ == null || d.width > this.dim_.width || d.height > this.dim_.height) { + super.update(g); + } + + this.paint(g); + } + } + + @Override + public void paint(Graphics g) { + if (this.image_ != null && g != null) { + g.drawImage(this.image_, 0, 0, this); + } + } + + public void paint(Graphics g, int x, int y) { + if (this.image_ != null && g != null) { + g.drawImage(this.image_, x, y, this); + } + } + + public Dimension imageSize() { + if (this.image_ == null) { + this.dim_ = new Dimension(0, 0); + } else { + int width = this.image_.getWidth(this); + int height = this.image_.getHeight(this); + if (width != -1 && height != -1) { + this.dim_ = new Dimension(width, height); + } + } + + return this.dim_; + } + + @Override + public Object asyncBackgroundLoad(String localName, URL remoteURL) { + this.image_ = this.loadLocalImage(localName); + if (this.getGraphics() != null) { + this.update(this.getGraphics()); + } + + return localName; + } + + @Override + public boolean syncBackgroundLoad(Object obj, URL remoteURL) { + String localName = (String)obj; + this.image_ = this.loadLocalImage(localName); + if (this.getGraphics() != null) { + this.update(this.getGraphics()); + } + + return false; + } + + @Override + public Room getBackgroundLoadRoom() { + return null; + } + + private void loadRemoteImage() { + this.image_ = this.loadLocalImage(defaultImageURL.unalias()); + if (this.imageURL_ != defaultImageURL) { + BackgroundLoader.get(this, this.imageURL_); + } + } + + private Image loadLocalImage(String fName) { + Image image = Toolkit.getDefaultToolkit().getImage(fName); + MediaTracker tracker = new MediaTracker(this); + tracker.addImage(image, 0); + + try { + tracker.waitForAll(); + } catch (InterruptedException var5) { + } + + if (!tracker.isErrorAny()) { + this.loaded_ = true; + return image; + } else { + return null; + } + } + + public static Image loadImage(URL url, Component comp) { + Image image = Toolkit.getDefaultToolkit().getImage(url.unalias()); + MediaTracker tracker = new MediaTracker(comp); + tracker.addImage(image, 0); + + try { + tracker.waitForAll(); + } catch (InterruptedException var5) { + } + + return !tracker.isErrorAny() ? image : null; + } + + public static Image getSystemImage(String name, Component comp) { + for (int pass = 0; pass < 2; pass++) { + Image image = loadImage(URL.make("home:" + name), comp); + if (image != null) { + return image; + } + + if (pass == 0) { + name = "..\\" + name; + } + } + + return null; + } + + @Override + public Dimension preferredSize() { + return this.imageSize(); + } + + @Override + public Dimension minimumSize() { + return this.imageSize(); + } +} |