2 import java.awt.event.*;
4 import java.awt.image.*;
5 import java.awt.geom.*;
7 import javax.swing.border.*;
11 public class ImageViewer extends JApplet
13 private class zoomLevel {
14 int x1, y1, x2, y2, zf;
17 zoomLevel(zoomLevel zl)
27 private BufferedImage bi;
28 private Graphics2D big;
31 private int selected = 0, imgId;
33 private PgmImage pgm = new PgmImage();
34 private String cmdline = new String();
35 private static String hostname;
36 private static boolean isApplet = true;
37 private boolean fullRefresh = false;
38 private Point offset = new Point(0,0);
39 private zoomLevel zl = new zoomLevel();
40 private Rectangle rect = new Rectangle();
41 private Stack zoomStack = new Stack();
42 private static String j2kfilename;
44 public int getX() { return offset.x; }
45 public int getY() { return offset.y; }
46 public int getWidth() { return iw; }
47 public int getHeight() { return ih; }
55 Dimension asz = this.getSize();
61 "/bin/sh get.sh " + j2kfilename + " " + iw
62 + " " + ih + " " + rect.x + " " + rect.y + " "
63 + rect.width + " " + rect.height;
64 Exec.execPrint(cmdline);
66 rect.x = rect.y = rect.width = rect.height = 0;
68 img = pgm.open("out.pgm");
70 iw = img.getWidth(this);
71 ih = img.getHeight(this);
72 bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
73 big = bi.createGraphics();
89 if (isApplet && (((hostname = this.getParameter("hostname")) == null)
90 || hostname.equals("")))
91 hostname = "localhost";
92 if (!isApplet || ((str = this.getParameter("cmdPort")) == null)) {
95 port = new Integer(str).intValue();
98 this.setSize(512, 512);
99 Dimension asz = this.getSize();
104 "/bin/sh get.sh " + j2kfilename + " " + asz.width
105 + " " + asz.height + " " + zl.x1 + " " + zl.y1 + " "
106 + zl.x2 + " " + zl.y2;
107 Exec.execPrint(cmdline);
108 img = pgm.open("out.pgm");
110 iw = img.getWidth(this);
111 ih = img.getHeight(this);
113 setBackground(Color.black);
114 bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
115 big = bi.createGraphics();
116 myMML = new MML(this);
117 addMouseListener(myMML);
118 addMouseMotionListener(myMML);
121 public void setSelected(int state)
123 if (state != selected) {
129 public boolean isInsideRect(int x, int y)
131 return rect.contains(x - offset.x, y - offset.y);
134 public void setRGeom(int x1, int y1, int x2, int y2)
136 rect.x = Math.min(x1,x2) - offset.x;
137 rect.y = Math.min(y1,y2) - offset.y;
138 rect.width = Math.abs(x2-x1);
139 rect.height = Math.abs(y2-y1);
142 public void paint(Graphics g)
144 Graphics2D g2 = (Graphics2D) g;
145 Dimension asz = this.getSize();
148 g2.clearRect(0, 0, asz.width, asz.height);
151 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
152 RenderingHints.VALUE_ANTIALIAS_ON);
153 g2.setRenderingHint(RenderingHints.KEY_RENDERING,
154 RenderingHints.VALUE_RENDER_QUALITY);
155 big.setColor(Color.black);
156 offset.x = (int) (asz.width - iw) / 2;
157 offset.y = (int) (asz.height - ih) / 2;
158 big.drawImage(img, 0, 0, this);
159 big.setPaint(Color.red);
160 if ((rect.width > 0) && (rect.height > 0))
163 shadeExt(big, 0, 0, 0, 64);
164 else if (selected == 2) {
165 shadeExt(big, 0, 0, 0, 255);
168 g2.drawImage(bi, offset.x, offset.y, this);
171 private void shadeRect(Graphics2D g2, int r, int g, int b, int a)
173 g2.setPaint(new Color(r, g, b, a));
174 g2.fillRect(rect.x + 1, rect.y + 1, rect.width - 1, rect.height - 1);
177 private void shadeExt(Graphics2D g2, int r, int g, int b, int a)
179 g2.setPaint(new Color(r, g, b, a));
180 g2.fillRect(0, 0, iw, rect.y); /* _N_ */
181 g2.fillRect(rect.x + rect.width + 1, rect.y,
182 iw - rect.x - rect.width - 1, rect.height + 1); /* E */
183 g2.fillRect(0, rect.y, rect.x, rect.height + 1); /* W */
184 g2.fillRect(0, rect.y + rect.height + 1,
185 iw, ih - rect.y - rect.height - 1); /* _S_ */
188 protected URL getURL(String filename)
190 URL codeBase = this.getCodeBase();
194 url = new URL(codeBase, filename);
195 } catch (java.net.MalformedURLException e) {
196 System.out.println("Couldn't create image: badly specified URL");
203 public static void main(String s[])
208 j2kfilename = "girl";
209 System.out.println(j2kfilename);
211 JFrame f = new JFrame("ImageViewer");
212 f.addWindowListener(new WindowAdapter() {
213 public void windowClosing(WindowEvent e) {System.exit(0);}
215 JApplet applet = new ImageViewer();
216 f.getContentPane().add("Center", applet);
219 f.setSize(new Dimension(550,550));