summaryrefslogtreecommitdiff
path: root/NET/worlds/console/ImageCanvas.java
blob: 96104382750181198c562aabe97b28d081380b2d (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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();
   }
}