~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to src/games/strategy/ui/ImageScrollerSmallView.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
13
 */
 
14
 
 
15
/*
 
16
 *
 
17
 * Created on October 30, 2001, 6:57 PM
 
18
 */
 
19
 
 
20
package games.strategy.ui;
 
21
 
 
22
import java.awt.*;
 
23
import java.awt.event.*;
 
24
import java.awt.geom.Rectangle2D;
 
25
import java.util.*;
 
26
 
 
27
import javax.swing.JComponent;
 
28
import javax.swing.border.EtchedBorder;
 
29
 
 
30
/**
 
31
 * 
 
32
 * @author Sean Bridges
 
33
 * @version 1.0
 
34
 * 
 
35
 * A small image that tracks a selection area within a small image. Generally
 
36
 * used in conjunction with a ImageScrollerLarrgeView.
 
37
 * 
 
38
 */
 
39
public class ImageScrollerSmallView extends JComponent
 
40
{
 
41
 
 
42
    private final ImageScrollModel m_model;
 
43
    
 
44
    private Image m_image;
 
45
 
 
46
        
 
47
    public ImageScrollerSmallView(Image image, ImageScrollModel model)
 
48
    {
 
49
        m_model = model;
 
50
        try
 
51
        {
 
52
            Util.ensureImageLoaded(image);
 
53
            setDoubleBuffered(false);
 
54
        } catch (InterruptedException ie)
 
55
        {
 
56
            ie.printStackTrace();
 
57
        }
 
58
        m_image = image;
 
59
 
 
60
        this.setBorder(new EtchedBorder());
 
61
 
 
62
        int prefWidth = getInsetsWidth() + m_image.getWidth(this);
 
63
        int prefHeight = getInsetsHeight() + m_image.getHeight(this);
 
64
        Dimension prefSize = new Dimension(prefWidth, prefHeight);
 
65
 
 
66
        setPreferredSize(prefSize);
 
67
        setMinimumSize(prefSize);
 
68
        setMaximumSize(prefSize);
 
69
 
 
70
        this.addMouseListener(MOUSE_LISTENER);
 
71
        this.addMouseMotionListener(MOUSE_MOTION_LISTENER);
 
72
        model.addObserver(new Observer()
 
73
        {
 
74
        
 
75
            public void update(Observable o, Object arg)
 
76
            {
 
77
              repaint();          
 
78
            }
 
79
        
 
80
        });
 
81
    }
 
82
 
 
83
    public void changeImage(Image image)
 
84
    {
 
85
        try
 
86
        {
 
87
            Util.ensureImageLoaded(image);
 
88
            setDoubleBuffered(false);
 
89
        } catch (InterruptedException ie)
 
90
        {
 
91
            ie.printStackTrace();
 
92
        }
 
93
        m_image = image;
 
94
    }
 
95
    
 
96
    
 
97
  
 
98
    private int getInsetsWidth()
 
99
    {
 
100
        return getInsets().left + getInsets().right;
 
101
    }
 
102
 
 
103
    private int getInsetsHeight()
 
104
    {
 
105
        return getInsets().top + getInsets().bottom;
 
106
    }
 
107
 
 
108
 
 
109
    void setCoords(int x, int y)
 
110
    {
 
111
       m_model.set(x, y);
 
112
        
 
113
    }
 
114
 
 
115
    public Dimension getImageDimensions()
 
116
    {
 
117
        return Util.getDimension(m_image, this);
 
118
    }
 
119
 
 
120
 
 
121
    public void paintComponent(Graphics g)
 
122
    {
 
123
        g.drawImage(m_image, 0, 0, this);
 
124
        g.setColor(Color.white);
 
125
        
 
126
        drawViewBox((Graphics2D) g);
 
127
 
 
128
    }
 
129
    
 
130
    private void drawViewBox(Graphics2D g)
 
131
    {
 
132
        if(m_model.getBoxWidth() > m_model.getMaxWidth() &&
 
133
           m_model.getBoxHeight() > m_model.getMaxHeight()     )
 
134
            return;
 
135
        
 
136
        double ratioX = getRatioX();
 
137
        double ratioY = getRatioY();
 
138
        
 
139
        double x = m_model.getX() * ratioX;
 
140
        double y = m_model.getY() * ratioY;
 
141
        
 
142
        double width = m_model.getBoxWidth() * ratioX;
 
143
        double height = m_model.getBoxHeight() * ratioY;
 
144
        
 
145
        Rectangle2D.Double rect = new Rectangle2D.Double(x,y,width,height);
 
146
        g.draw(rect);
 
147
        
 
148
        if(m_model.getScrollX())
 
149
        {
 
150
            double mapWidth = m_model.getMaxWidth() * ratioX;
 
151
            
 
152
            rect.x += mapWidth;
 
153
            g.draw(rect);
 
154
            rect.x -= 2 * mapWidth;
 
155
            g.draw(rect);
 
156
        }
 
157
    }
 
158
 
 
159
    public Image getOffScreenImage()
 
160
    {
 
161
        return m_image;
 
162
    }
 
163
 
 
164
    private void setSelection(int x, int y)
 
165
    {
 
166
      m_model.set(x, y);
 
167
    }
 
168
 
 
169
    private long mLastUpdate = 0;
 
170
    private long MIN_UPDATE_DELAY = 30;
 
171
 
 
172
    private final MouseMotionListener MOUSE_MOTION_LISTENER = new MouseMotionAdapter()
 
173
    {
 
174
        public void mouseDragged(MouseEvent e)
 
175
        {
 
176
 
 
177
            long now = System.currentTimeMillis();
 
178
            if (now < mLastUpdate + MIN_UPDATE_DELAY)
 
179
                return;
 
180
 
 
181
            mLastUpdate = now;
 
182
 
 
183
            Rectangle bounds = (Rectangle) getBounds().clone();
 
184
            //if the mouse is a little off the screen, allow it to still scroll
 
185
            // the screen
 
186
            bounds.grow(30, 0);
 
187
 
 
188
            if (!bounds.contains(e.getPoint()))
 
189
                return;
 
190
 
 
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);
 
194
 
 
195
            setSelection(x, y);
 
196
        }
 
197
    };
 
198
    
 
199
   
 
200
 
 
201
 
 
202
    private final MouseAdapter MOUSE_LISTENER = new MouseAdapter()
 
203
    {
 
204
        public void mouseClicked(MouseEvent e)
 
205
        {
 
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);
 
209
            
 
210
            m_model.set(x, y);
 
211
        }
 
212
    };
 
213
 
 
214
 
 
215
    public double getRatioY()
 
216
    {
 
217
        return m_image.getHeight(null) / (double) m_model.getMaxHeight();
 
218
    }
 
219
    
 
220
    public double getRatioX()
 
221
    {
 
222
        return m_image.getWidth(null) / (double) m_model.getMaxWidth();
 
223
    }
 
224
 
 
225
    
 
226
    
 
227
}
 
 
b'\\ No newline at end of file'