Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageDisplay.java
More file actions
Latest commit
executable file
·216 lines (191 loc) · 5.98 KB
/
Copy pathImageDisplay.java
File metadata and controls
executable file
·216 lines (191 loc) · 5.98 KB
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
importjavax.swing.*;
importjava.awt.*;
importjava.awt.image.*;
/**
* Class to display an image and the current location with a + sign
*
* @author Barb Ericson ericson@cc.gatech.edu
*/
publicclassImageDisplayextendsJPanelimplementsScrollable
{
/////////////////////////// fields (attributes ///////////////////////////
/** the image to draw */
privateImageimage;
/** the preferred size of the display */
privateDimensionprefSize;
/** the current x index */
privateintcurrentX = 0;
/** the current y index */
privateintcurrentY = 0;
//////////////////////////// constructors /////////////////////////////////
/**
* Constructor that takes the image to display
* @param theImage the image to display
*/
publicImageDisplay(ImagetheImage)
{
image = theImage;
prefSize = newDimension(image.getWidth(this),image.getHeight(this));
setPreferredSize(prefSize);
revalidate();
}
/**
* Constructor that takes the image and current x and y
* @param theImage the image to display
* @param x the current x value to use
* @param y the current y value to use
*/
publicImageDisplay(ImagetheImage, intx, inty)
{
this(theImage);
currentX = x;
currentY = y;
}
////////////////////// methods /////////////////////////////////////////////
/**
* Method to get the image
* @return the image
*/
publicImagegetImage() { returnimage; }
/**
* Method to get the current x
* @return the current x value
*/
publicintgetCurrentX() { returncurrentX; }
/**
* Method to get the current y
* @return the current y value
*/
publicintgetCurrentY() { returncurrentY; }
/**
* Method to set the current x
* @param x the x value to use
*/
publicvoidsetCurrentX(intx)
{
currentX = x;
repaint();
}
/**
* Method to set the current y
* @param y the y value to use
*/
publicvoidsetCurrentY(inty)
{
currentY = y;
repaint();
}
/**
* Method to set the image
* @param theImage the new image to use
*/
publicvoidsetImage(ImagetheImage)
{
image = theImage;
setPreferredSize(newDimension(image.getWidth(this),image.getHeight(this)));
repaint();
}
/**
* Method to return the preferred size
* @return the preferred size of this component
*/
publicDimensiongetPreferredScrollableViewportSize()
{
returnprefSize;
}
/**
* Method to return the unit increment for scrolling
* @param visibleRect the visible rectangle
* @param orientation vertical or horizontal
* @param direction neg is up or left and pos is right or down
* @return the unit increment for arrow clicks
*/
publicintgetScrollableUnitIncrement(RectanglevisibleRect,
intorientation,
intdirection)
{ return1; }
/**
* Method to return the block increment for scrolling
* @param visibleRect the visible rectangle
* @param orientation vertical or horizontal
* @param direction neg is up or left and pos is right or down
* @return the block increment for clicking in scroll area
*/
publicintgetScrollableBlockIncrement(RectanglevisibleRect,
intorientation,
intdirection)
{
return10;
}
/**
* Method to check if the viewport width is the source width
* @return true if viewport and source have same width
*/
publicbooleangetScrollableTracksViewportWidth()
{ returnfalse; }
/**
* Method to check if the viewport height is the source height
* @return true if viewport and source have same height
*/
publicbooleangetScrollableTracksViewportHeight()
{ returnfalse; }
/**
* Method to handle displaying this object
* @param g the graphics object for drawing with
*/
publicvoidpaintComponent(Graphicsg)
{
super.paintComponent(g);
intnum = 3;
intxStart = currentX - num;
intxEnd = currentX + num;
intyStart = currentY - num;
intyEnd = currentY + num;
intwidth = image.getWidth(this);
intmaxX = width - 1;
intheight = image.getHeight(this);
intmaxY = height - 1;
// draw the image
g.drawImage(image,0,0,this);
// check if the current index is in the image
if (currentX >= 0 && currentX < width &&
currentY >= 0 && currentY < height)
{
// check that the start and end values are visible
if (xStart < 0)
xStart = 0;
if (xEnd > maxX)
xEnd = maxX;
if (yStart < 0)
yStart = 0;
if (yEnd > maxY)
yEnd = maxY;
// draw a small cross at the current x and y in yellow
g.setColor(Color.yellow);
g.drawLine(xStart,currentY,xEnd,currentY);
g.drawLine(currentX,yStart,currentX,yEnd);
g.setColor(Color.black);
// outline the cross in black so that it shows up better
intleftX = currentX - 1;
intrightX = currentX + 1;
intupY = currentY - 1;
intdownY = currentY + 1;
if (xStart <= leftX && upY >= 0)
g.drawLine(xStart,upY,leftX,upY);
if (yStart <= upY && leftX >= 0)
g.drawLine(leftX,yStart,leftX,upY);
if (yStart <= upY && rightX <= maxX)
g.drawLine(rightX,yStart,rightX,upY);
if (upY >= 0 && rightX <= xEnd)
g.drawLine(rightX,upY,xEnd,upY);
if (downY < height && rightX <= xEnd)
g.drawLine(rightX,downY,xEnd,downY);
if (downY <= yEnd && rightX < width)
g.drawLine(rightX,downY,rightX,yEnd);
if (xStart <= leftX && downY < height)
g.drawLine(xStart,downY,leftX,downY);
if (leftX >= 0 && downY <= yEnd)
g.drawLine(leftX,downY,leftX,yEnd);
}
}
}