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

Last change on this file since 11647 was 11647, checked in by shaoqun, 18 years ago

add a new variable (url) to CollageImage class, so we known where the images was downloaded

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