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

« back to all changes in this revision

Viewing changes to src/games/strategy/triplea/ui/screen/SmallMapImageManager.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
package games.strategy.triplea.ui.screen;
 
15
 
 
16
import java.awt.*;
 
17
import java.awt.image.BufferedImage;
 
18
import java.util.*;
 
19
import java.util.logging.*;
 
20
 
 
21
import games.strategy.engine.data.*;
 
22
import games.strategy.triplea.ui.MapData;
 
23
import games.strategy.triplea.util.Stopwatch;
 
24
import games.strategy.ui.ImageScrollerSmallView;
 
25
import games.strategy.ui.Util;
 
26
 
 
27
/**
 
28
 * 
 
29
 *
 
30
 *
 
31
 * @author Sean Bridges
 
32
 */
 
33
public class SmallMapImageManager
 
34
{
 
35
    private static final Logger s_logger =  Logger.getLogger(SmallMapImageManager.class.getName());
 
36
    private final int UNIT_BOX_SIZE = 4;
 
37
    
 
38
    private final ImageScrollerSmallView m_view;
 
39
    private final Image m_offscreen;
 
40
    private final TileManager m_tileManager;
 
41
    
 
42
    
 
43
    public SmallMapImageManager(final ImageScrollerSmallView view, BufferedImage offscreen, TileManager tileManager)
 
44
    {
 
45
        m_view = view;
 
46
        m_offscreen =  Util.copyImage(offscreen, false);
 
47
        m_tileManager = tileManager;
 
48
    }
 
49
    
 
50
    public void update(GameData data, MapData mapData)
 
51
    {
 
52
        Stopwatch stopwatch = new Stopwatch(s_logger, Level.FINEST, "Small map updating took");
 
53
        
 
54
        Graphics onScreenGraphics = m_view.getOffScreenImage().getGraphics();
 
55
        
 
56
        onScreenGraphics.drawImage(m_offscreen, 0,0,null);
 
57
    
 
58
        
 
59
        Iterator<UnitsDrawer> iter = new ArrayList<UnitsDrawer>(m_tileManager.getUnitDrawables()).iterator();
 
60
        while (iter.hasNext())
 
61
        {
 
62
            UnitsDrawer drawer = iter.next();
 
63
            int x = (int) (drawer.getPlacementPoint().x * m_view.getRatioX());
 
64
            int y = (int) (drawer.getPlacementPoint().y * m_view.getRatioY());
 
65
            
 
66
            onScreenGraphics.setColor(mapData.getPlayerColor(drawer.getPlayer()).darker() );
 
67
            onScreenGraphics.fillRect(x,y, UNIT_BOX_SIZE, UNIT_BOX_SIZE);
 
68
        }
 
69
        
 
70
        onScreenGraphics.dispose();
 
71
        stopwatch.done();
 
72
        
 
73
    }
 
74
    
 
75
    public void updateTerritoryOwner(Territory t, GameData data, MapData mapData)
 
76
    {
 
77
        if(t.isWater())
 
78
            return;
 
79
        
 
80
        Rectangle bounds = new Rectangle(mapData.getBoundingRect(t.getName()));
 
81
 
 
82
        //create a large image for the territory
 
83
        Image largeImage = Util.createImage(bounds.width, bounds.height, true);
 
84
        
 
85
        // make it transparent
 
86
        //http://www-106.ibm.com/developerworks/library/j-begjava/
 
87
        {
 
88
            Graphics2D g =  (Graphics2D) largeImage.getGraphics();
 
89
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
 
90
            g.setColor(new Color(0));
 
91
            g.fillRect(0, 0, bounds.width, bounds.height);
 
92
            g.dispose();
 
93
        }
 
94
        
 
95
        //draw the territory
 
96
        {
 
97
            Graphics g = largeImage.getGraphics();
 
98
            LandTerritoryDrawable drawable = new LandTerritoryDrawable(t.getName());
 
99
            drawable.draw(bounds, data, (Graphics2D)g , mapData, null, null);
 
100
            g.dispose();
 
101
        }
 
102
        
 
103
        //scale it down
 
104
        int thumbWidth = (int) (bounds.width * m_view.getRatioX()) ;        
 
105
        int thumbHeight = (int) (bounds.height * m_view.getRatioY());
 
106
        
 
107
        //make the image a little bigger
 
108
        //the images wont overlap perfectly after being scaled, make them a little bigger to rebalance that
 
109
        thumbWidth+=3;
 
110
        thumbHeight+=3;
 
111
 
 
112
        int thumbsX = (int) ( bounds.x * m_view.getRatioX()) -1; 
 
113
        int thumbsY = (int) ( bounds.y * m_view.getRatioY()) -1;
 
114
 
 
115
        
 
116
        //create the thumb image
 
117
        Image thumbImage = Util.createImage( thumbWidth, thumbHeight , true);
 
118
        {
 
119
            Graphics g = thumbImage.getGraphics();
 
120
            g.drawImage(largeImage, 0,0, thumbImage.getWidth(null), thumbImage.getHeight(null), null);
 
121
            g.dispose();
 
122
        }
 
123
        
 
124
        {
 
125
           Graphics g = m_offscreen.getGraphics();
 
126
            //draw it on our offscreen
 
127
            g.drawImage( thumbImage,
 
128
                                                                         thumbsX,
 
129
                                                         thumbsY,
 
130
                                                         thumbImage.getWidth(null),
 
131
                                                         thumbImage.getHeight(null),
 
132
                                                         null);
 
133
            g.dispose();
 
134
        }
 
135
        
 
136
    }
 
137
    
 
138
    
 
139
    
 
140
}