source: trunk/gsdl/src/java/org/nzdl/gsdl/GsdlCollageApplet/CollageImage.java@ 6816

Last change on this file since 6816 was 6816, checked in by mdewsnip, 20 years ago

The GsdlCollageApplet: a classifier that displays a collage of the images in a collection. By Katrina Edgar (kde2).

  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1package org.nzdl.gsdl.GsdlCollageApplet;
2
3import java.awt.*;
4import java.awt.geom.*;
5import java.io.*;
6
7/**
8 * @author Katrina Edgar
9 * @author David Bainbridge
10 *
11 * Data Structure to store an image once it is displayed in the applet.
12 * This structure remembers the images graphical representation, source url,
13 * name, width, height, position on the screen and whether or not it has
14 * previously been displayed. It provides methods to set the dimensions and
15 * position of the image. It also provides access methods to the translated
16 * x and y co-ordinates of the image */
17public class CollageImage {
18
19 /** Graphical representation of the image */
20 Image image_ = null;
21 /** Source url of the image */
22 String from_url_ = null;
23 /** Name of the image */
24 String name_ = null;
25 /** Width of the image */
26 int image_x_dim_ = 0;
27 /** Height of the image */
28 int image_y_dim_ = 0;
29 /** Indicates whether or not the image has been drawn on the applet previously */
30 boolean fresh = true;
31
32 /** Reflects the translation of the image from the origin to its position on the screen */
33 AffineTransform af_ = null;
34 /** Left x co-ordinate */
35 protected int xl_ = 0;
36 /** Top y co-ordinate */
37 protected int yt_ = 0;
38 /** Right x co-ordinate */
39 protected int xr_ = 0;
40 /** Bottom y co-ordinate */
41 protected int yb_ = 0;
42
43 /** Constructs an CollageImage from the three specified parameters
44 *
45 * @param image The graphical representation of the image
46 * @param from_url The source url for the image
47 * @param name The file name of the image */
48 public CollageImage(Image image, String from_url, String name)
49 {
50 image_ = image;
51 from_url_ = from_url;
52 name_ = name;
53 }
54
55 /** Sets the width and height of the image
56 *
57 * @param x_dim The new width of the image
58 * @param y_dim The new height of the image */
59 public void setDimensions(int x_dim, int y_dim)
60 {
61 image_x_dim_ = x_dim;
62 image_y_dim_ = y_dim;
63 }
64
65 /** Sets the translation for the image from the origin
66 * And regenerate the image as the scaled version
67 *
68 * @param af The AffineTransform translation that has been calculated */
69 public void setAffineTransform(MyAffineTransform af, boolean is_java2_)
70 {
71 image_x_dim_ = (int) (image_x_dim_ * af.scaleX);
72 image_y_dim_ = (int) (image_y_dim_ * af.scaleY);
73 image_ = image_.getScaledInstance(image_x_dim_, image_y_dim_, Image.SCALE_DEFAULT);
74
75 if (is_java2_) {
76 af_ = new AffineTransform();
77 af_.translate(af.translateX, af.translateY);
78 af_.scale(1.0, 1.0);
79
80 calculate_rect();
81 }
82 else {
83 double trans_x = af.translateX;
84 double trans_y = af.translateY;
85
86 xl_ = Math.round((float)trans_x);
87 xr_ = Math.round((float)(trans_x + image_x_dim_)) -1;
88 yt_ = Math.round((float)trans_y);
89 yb_ = Math.round((float)(trans_y + image_y_dim_)) -1;
90 }
91 }
92
93 public void expand () {
94
95 // still expands a little too fast... how to fix this?
96 magnify(new Rectangle((int) (xl_ + (image_x_dim_ * 0.000001)),
97 (int) (yt_ + (image_y_dim_ * 0.000001)),
98 (int) (image_x_dim_ * 0.999998),
99 (int) (image_y_dim_ * 0.999998)));
100
101 }
102
103 public void magnify(Rectangle border) {
104
105 double magX = image_x_dim_/(double)border.width;
106 double magY = image_y_dim_/(double)border.height;
107 int x = border.x + border.width/2;
108 int y = border.y + border.height/2;
109 paintImage(x,y,magX, magY);
110 }
111
112 public void paintImage(int magCenterX, int magCenterY, double magX, double magY){
113
114 try {
115 //Point2D mgp = null;
116 //mgp = af_.inverseTransform((new Point(magCenterX, magCenterY)),(Point)mgp);
117 //double x = (mgp.getX()*magX)-mgp.getX();
118 //double y = (mgp.getY()*magY)-mgp.getY();
119 //scale(-x,-y, magX, magY);
120 }catch (Exception e) {System.out.println(e); }
121 }
122
123 public void scale(double magOffsetX, double magOffsetY, double magX, double magY){
124
125 af_.translate(magOffsetX,magOffsetY);
126 af_.scale(magX, magY);
127 }
128
129 /** Calculates the new image co-ordinates after translation has occurred */
130 protected void calculate_rect()
131 {
132 double trans_x = af_.getTranslateX();
133 double trans_y = af_.getTranslateY();
134
135 xl_ = Math.round((float)trans_x);
136 xr_ = Math.round((float)(trans_x + image_x_dim_)) -1;
137 yt_ = Math.round((float)trans_y);
138 yb_ = Math.round((float)(trans_y + image_y_dim_)) -1;
139 }
140
141 /** Determines whether a given co-ordinate is inside this image
142 *
143 * @param x The x co-ordinate
144 * @param y The y co-ordinate */
145 public boolean inside(int x, int y)
146 {
147 return ((x>=xl_) && (x<=xr_)) && ((y>=yt_) && (y<=yb_));
148 }
149
150 /** Gets the width of the translated image */
151 public double getX() {
152 return af_.getTranslateX();
153 }
154
155 /** Gets the height of the translated image */
156 public double getY() {
157 return af_.getTranslateY();
158 }
159}
160
Note: See TracBrowser for help on using the repository browser.