A
download ImageTest.java
Language: Java
Copyright: (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
LOC: 385
Project Info
JavaML
Server: SourceForge
Type: cvs
...ml\javaml\java‑ml\examples\
   .cvsignore
   Abacus.java
   AlphaBullet.java
   Animator.java
   AnInterface.java
   AnonymousClass.java
   AnotherApplet.java
   Assignments.java
   AudioItem.java
   Banner.java
   BigInit.java
   Blah.java
   BounceItem.java
   Bubbles.java
   cannon.java
   Chart.java
   ComplexApplet.java
   ComplexApplet2.java
   Continued.java
   crossword.java
   DiningPhilosophers.java
   escherpaint.java
   fillTriangle.java
   FirstApplet.java
   generic_paint.java
   Hangman.java
   ImageLoop.java
   ImageLoopItem.java
   ImageTape.java
   ImageTest.java
   JackhammerDuke.java
   LED.java
   LEDMessage.java
   Letters.java
   OuterClass.java
   Pythagoras.java
   RotateFilter.java
   Script.java
   SimpAnim.html
   SimpAnim.java
   SimpleAnimator.java
   stars.java
   SystemInfo.java
   TumbleItem.java
   voltage.java
   WordMatch.java

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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
 * @(#)ImageTest.java	1.8 96/02/27
 *
 * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
 * without fee is hereby granted. 
 * Please refer to the file http://java.sun.com/copy_trademarks.html
 * for further important copyright and trademark information and to
 * http://java.sun.com/licensing.html for further important licensing
 * information for the Java (tm) Technology.
 * 
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 * 
 * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
 * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
 * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
 * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
 * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
 * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
 * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
 * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
 * HIGH RISK ACTIVITIES.
 */
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.applet.Applet;

public class ImageTest extends Applet {
    public void init() {
	setLayout(new BorderLayout());
	add("Center", new ImagePanel(this));
	add("North", new ImageHelp());
	setVisible(true);
    }
    public String getAppletInfo() {
        return "A simple image manipulation tool.";
    }
}

class ImageHelp extends Panel {
    public ImageHelp() {
	setLayout(new GridLayout(5, 1));
	add(new Label("Move the images with the mouse, with < > ^ v, or with [L]eft/[R]ight/[U]p/[D]own",
		      Label.CENTER));
	add(new Label("Resize the images with +/-",
		      Label.CENTER));
	add(new Label("[T]oggle a color filter with the T key",
		      Label.CENTER));
	add(new Label("Change the alpha with (Shift)+/-",
		      Label.CENTER));
	add(new Label("Rotate the image with (Shift)<> or (Shift)[L]eft/[R]ight",
		      Label.CENTER));
    }
}

class ImagePanel extends Panel {
    Applet applet;

    public ImagePanel(Applet app) {
	applet = app;
	setLayout(new BorderLayout());
	Panel grid = new Panel();
	grid.setLayout(new GridLayout(0, 2));
	add("Center", grid);
	grid.add(new ImageCanvas(applet, makeDitherImage(), 0.5));
	Image joe = applet.getImage(applet.getDocumentBase(),
				    "images/joe.surf.yellow.small.gif");
	grid.add(new ImageCanvas(applet, joe, 1.0));
	setBounds(0, 0, 20, 20);
    }

    Image makeDitherImage() {
	int w = 100;
	int h = 100;
	int pix[] = new int[w * h];
	int index = 0;
	for (int y = 0; y < h; y++) {
	    int red = (y * 255) / (h - 1);
	    for (int x = 0; x < w; x++) {
		int blue = (x * 255) / (w - 1);
		pix[index++] = (255 << 24) | (red << 16) | blue;
	    }
	}
	return applet.createImage(new MemoryImageSource(w, h, pix, 0, w));
    }
}

class ImageCanvas 
extends Canvas 
implements ImageObserver, KeyListener, MouseListener, 
  MouseMotionListener, FocusListener {
    double 	hmult = 0;
    int		xadd = 0;
    int		yadd = 0;
    int		xprev = 0;
    int		yprev = 0;
    int		imgw = -1;
    int		imgh = -1;
    int		xoff = 0;
    int		yoff = 0;
    int		scalew = -1;
    int		scaleh = -1;
    boolean	focus = false;
    boolean	usefilter = false;
    static final int numalphas = 9;
    int		alpha = numalphas - 1;
    static final int numrotations = 8;
    int		rotation = 0;
    ImageFilter colorfilter;
    ImageFilter alphafilters[] = new ImageFilter[numalphas];
    RotateFilter rotfilters[] = new RotateFilter[numrotations];
    Image	origimage;
    Image	curimage;
    Applet	applet;

    public ImageCanvas(Applet app, Image img, double mult) {
        applet = app;
	origimage = img;
	hmult = mult;
	pickImage();
	setBounds(0, 0, 100, 100);
	addMouseListener(this);
	addMouseMotionListener(this);
	addKeyListener(this);
	addFocusListener(this);
    }
    //1.1 event handling
  public void focusGained(FocusEvent e) {
    focus = true;
    repaint();
  }
    
  public void focusLost(FocusEvent e) {
    focus = false;
    repaint();
  }

  public void keyPressed(KeyEvent e) {
  }
    
  public void keyTyped(KeyEvent e) {
    char key = e.getKeyChar();
    switch(key) 
      {
      case 't':
      case 'T':
	usefilter = !usefilter;
	pickImage();
	repaint();
	e.consume();
	break;
      case '^':
      case '6':
      case 'u':
      case 'U':
	yadd -= 5;
	repaint();
	e.consume();
	break;
      case 'v':
      case 'V':
      case 'd':
      case 'D':
	yadd += 5;
	repaint();
	e.consume();
	break;
      case '>':
      case 'R':
	rotation--;
	if (rotation < 0) {
	  rotation = numrotations - 1;
	}
	pickImage();
	scalew = scaleh = -1;
	repaint();
	e.consume();
	break;
      case '.':
      case 'r':
	xadd += 5;
	repaint();
	e.consume();
	break;
      case '<':
      case 'L':      
        rotation++;
	if (rotation >= numrotations) {
	  rotation = 0;
	}
	pickImage();
	scalew = scaleh = -1;
	repaint();
	e.consume();
	break;
      case ',':
      case 'l':
	xadd -= 5;
	repaint();
	e.consume();
	break;
      case '+':
	if (++alpha > numalphas - 1) {
	  alpha = numalphas - 1;
	}
	pickImage();
	repaint();
	e.consume();
	break;	      
      case '=':
	hmult *= 1.2;
	scalew = scaleh = -1;
	repaint();
	e.consume();
	break;	      
      case '-':
	hmult /= 1.2;
	scalew = scaleh = -1;
	repaint();
	e.consume();
	break;
      case '_':
	if (--alpha < 0) {
	  alpha = 0;
	}
	pickImage();
	repaint();
	e.consume();
	break;
      }
  }
    
  public void keyReleased(KeyEvent e) {

  }
    
  public void mouseClicked(MouseEvent e) {
  }
    
  public void mousePressed(MouseEvent e) {
    xprev = e.getX();
    yprev = e.getY();
    e.consume();
  }
    
  public void mouseReleased(MouseEvent e) {
    e.consume();
  }
    
  public void mouseEntered(MouseEvent e) {
    requestFocus();
    e.consume();
  }
    
  public void mouseExited(MouseEvent e) {
  }
    
  public void mouseDragged(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    
    xadd += x - xprev;
    yadd += y - yprev;
    xprev = x;
    yprev = y;
    repaint();
    e.consume();
  }
    
  public void mouseMoved(MouseEvent e) {
  }
 
    public void paint(Graphics g) {
	Rectangle r = getBounds();
	int hlines = r.height / 10;
	int vlines = r.width / 10;

	if (focus) {
	    g.setColor(Color.red);
	} else {
	    g.setColor(Color.darkGray);
	}
	g.drawRect(0, 0, r.width-1, r.height-1);
	g.drawLine(0, 0, r.width, r.height);
	g.drawLine(r.width, 0, 0, r.height);
	g.drawLine(0, r.height / 2, r.width, r.height / 2);
	g.drawLine(r.width / 2, 0, r.width / 2, r.height);
	if (imgw < 0) {
	    imgw = curimage.getWidth(this);
	    imgh = curimage.getHeight(this);
	    if (imgw < 0 || imgh < 0) {
		return;
	    }
	}
	if (scalew < 0) {
	    if (rotation == 0) {
		scalew = imgw;
		scaleh = imgh;
	    } else {
		Rectangle rect = new Rectangle(0, 0, imgw, imgh);
		rotfilters[rotation].transformBBox(rect);
		xoff = rect.x;
		yoff = rect.y;
		scalew = rect.width;
		scaleh = rect.height;
	    }
	    scalew = (int) (scalew * hmult);
	    scaleh = (int) (scaleh * hmult);
	    xoff = (imgw - scalew) / 2;
	    yoff = (imgh - scaleh) / 2;
	}
	if (imgw != scalew || imgh != scaleh) {
	    g.drawImage(curimage, xadd + xoff, yadd + yoff,
			scalew, scaleh, this);
	} else {
	    g.drawImage(curimage, xadd + xoff, yadd + yoff, this);
	}
    }

    static final long updateRate = 100;

    public synchronized boolean imageUpdate(Image img, int infoflags,
					    int x, int y, int w, int h) {
	if (img != curimage) {
	    return false;
	}
	boolean ret = true;
	boolean dopaint = false;
	long updatetime = 0;
	if ((infoflags & WIDTH) != 0) {
	    imgw = w;
	    dopaint = true;
	}
	if ((infoflags & HEIGHT) != 0) {
	    imgh = h;
	    dopaint = true;
	}
	if ((infoflags & (FRAMEBITS | ALLBITS)) != 0) {
	    dopaint = true;
	    ret = false;
	} else if ((infoflags & SOMEBITS) != 0) {
	    dopaint = true;
	    updatetime = updateRate;
	}
	if ((infoflags & ERROR) != 0) {
	    ret = false;
	}
	if (dopaint) {
	    repaint(updatetime);
	}
	return ret;
    }

    public synchronized Image pickImage() {
	ImageProducer src = origimage.getSource();
	if (alpha != numalphas - 1) {
	    ImageFilter imgf = alphafilters[alpha];
	    if (imgf == null) {
		int alphaval = (alpha * 255) / (numalphas - 1);
		imgf = new AlphaFilter(alphaval);
		alphafilters[alpha] = imgf;
	    }
	    src = new FilteredImageSource(src, imgf);
	}
	if (rotation != 0) {
	    RotateFilter imgf = rotfilters[rotation];
	    if (imgf == null) {
		double angle = (2 * Math.PI * rotation) / numrotations;
		imgf = new RotateFilter(angle);
		rotfilters[rotation] = imgf;
	    }
	    src = new FilteredImageSource(src, imgf);
	}
	if (usefilter) {
	    if (colorfilter == null) {
		colorfilter = new RedBlueSwapFilter();
	    }
	    src = new FilteredImageSource(src, colorfilter);
	}
	Image choice;
	if (src == origimage.getSource()) {
	    choice = origimage;
	} else {
	    choice = applet.createImage(src);
	}
	if (curimage != choice) {
	    if (curimage != null && curimage != origimage) {
		curimage.flush();
	    }
	    curimage = choice;
	}
	return choice;
    }
}

class RedBlueSwapFilter extends RGBImageFilter {
    public RedBlueSwapFilter() {
	canFilterIndexColorModel = true;
    }

    public void setColorModel(ColorModel model) {
	if (model instanceof DirectColorModel) {
	    DirectColorModel dcm = (DirectColorModel) model;
	    int rm = dcm.getRedMask();
	    int gm = dcm.getGreenMask();
	    int bm = dcm.getBlueMask();
	    int am = dcm.getAlphaMask();
	    int bits = dcm.getPixelSize();
	    dcm = new DirectColorModel(bits, bm, gm, rm, am);
	    substituteColorModel(model, dcm);
	    consumer.setColorModel(dcm);
	} else {
	    super.setColorModel(model);
	}
    }

    public int filterRGB(int x, int y, int rgb) {
	return ((rgb & 0xff00ff00)
		| ((rgb & 0xff0000) >> 16)
		| ((rgb & 0xff) << 16));
    }
}

class AlphaFilter extends RGBImageFilter {
    ColorModel origmodel;
    ColorModel newmodel;

    int alphaval;

    public AlphaFilter(int alpha) {
	alphaval = alpha;
	canFilterIndexColorModel = true;
    }

    public int filterRGB(int x, int y, int rgb) {
	int alpha = (rgb >> 24) & 0xff;
	alpha = alpha * alphaval / 255;
	return ((rgb & 0x00ffffff) | (alpha << 24));
    }
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us