2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
6
* This program is distributed in the hope that it will be useful,
7
* but WITHOUT ANY WARRANTY; without even the implied warranty of
8
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
* GNU General Public License for more details.
10
* You should have received a copy of the GNU General Public License
11
* along with this program; if not, write to the Free Software
12
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
* Created on October 30, 2001, 6:57 PM
20
package games.strategy.ui;
23
import java.awt.event.*;
24
import java.awt.geom.Rectangle2D;
27
import javax.swing.JComponent;
28
import javax.swing.border.EtchedBorder;
32
* @author Sean Bridges
35
* A small image that tracks a selection area within a small image. Generally
36
* used in conjunction with a ImageScrollerLarrgeView.
39
public class ImageScrollerSmallView extends JComponent
42
private final ImageScrollModel m_model;
44
private Image m_image;
47
public ImageScrollerSmallView(Image image, ImageScrollModel model)
52
Util.ensureImageLoaded(image);
53
setDoubleBuffered(false);
54
} catch (InterruptedException ie)
60
this.setBorder(new EtchedBorder());
62
int prefWidth = getInsetsWidth() + m_image.getWidth(this);
63
int prefHeight = getInsetsHeight() + m_image.getHeight(this);
64
Dimension prefSize = new Dimension(prefWidth, prefHeight);
66
setPreferredSize(prefSize);
67
setMinimumSize(prefSize);
68
setMaximumSize(prefSize);
70
this.addMouseListener(MOUSE_LISTENER);
71
this.addMouseMotionListener(MOUSE_MOTION_LISTENER);
72
model.addObserver(new Observer()
75
public void update(Observable o, Object arg)
83
public void changeImage(Image image)
87
Util.ensureImageLoaded(image);
88
setDoubleBuffered(false);
89
} catch (InterruptedException ie)
98
private int getInsetsWidth()
100
return getInsets().left + getInsets().right;
103
private int getInsetsHeight()
105
return getInsets().top + getInsets().bottom;
109
void setCoords(int x, int y)
115
public Dimension getImageDimensions()
117
return Util.getDimension(m_image, this);
121
public void paintComponent(Graphics g)
123
g.drawImage(m_image, 0, 0, this);
124
g.setColor(Color.white);
126
drawViewBox((Graphics2D) g);
130
private void drawViewBox(Graphics2D g)
132
if(m_model.getBoxWidth() > m_model.getMaxWidth() &&
133
m_model.getBoxHeight() > m_model.getMaxHeight() )
136
double ratioX = getRatioX();
137
double ratioY = getRatioY();
139
double x = m_model.getX() * ratioX;
140
double y = m_model.getY() * ratioY;
142
double width = m_model.getBoxWidth() * ratioX;
143
double height = m_model.getBoxHeight() * ratioY;
145
Rectangle2D.Double rect = new Rectangle2D.Double(x,y,width,height);
148
if(m_model.getScrollX())
150
double mapWidth = m_model.getMaxWidth() * ratioX;
154
rect.x -= 2 * mapWidth;
159
public Image getOffScreenImage()
164
private void setSelection(int x, int y)
169
private long mLastUpdate = 0;
170
private long MIN_UPDATE_DELAY = 30;
172
private final MouseMotionListener MOUSE_MOTION_LISTENER = new MouseMotionAdapter()
174
public void mouseDragged(MouseEvent e)
177
long now = System.currentTimeMillis();
178
if (now < mLastUpdate + MIN_UPDATE_DELAY)
183
Rectangle bounds = (Rectangle) getBounds().clone();
184
//if the mouse is a little off the screen, allow it to still scroll
188
if (!bounds.contains(e.getPoint()))
191
//try to center around the click
192
int x = (int) (e.getX() / getRatioX()) - (m_model.getBoxWidth() / 2);
193
int y = (int) (e.getY() / getRatioY()) - (m_model.getBoxHeight() / 2);
202
private final MouseAdapter MOUSE_LISTENER = new MouseAdapter()
204
public void mouseClicked(MouseEvent e)
206
//try to center around the click
207
int x = (int) (e.getX() / getRatioX()) - (m_model.getBoxWidth() / 2);
208
int y = (int) (e.getY() / getRatioY()) - (m_model.getBoxHeight() / 2);
215
public double getRatioY()
217
return m_image.getHeight(null) / (double) m_model.getMaxHeight();
220
public double getRatioX()
222
return m_image.getWidth(null) / (double) m_model.getMaxWidth();
b'\\ No newline at end of file'