1046d6bd0191af519a608f2a62a5e752a676b0c7
[fw/altos] / altosuilib / AltosSiteMapTile.java
1 /*
2  * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_2;
19
20 import java.awt.*;
21 import java.awt.image.*;
22 import javax.swing.*;
23 import javax.imageio.*;
24 import java.awt.geom.Point2D;
25 import java.awt.geom.Line2D;
26 import java.io.*;
27 import java.util.*;
28 import org.altusmetrum.altoslib_4.*;
29
30 class AltosPoint {
31         Point2D.Double  pt;
32         int             state;
33
34         AltosPoint(Point2D.Double pt, int state) {
35                 this.pt = pt;
36                 this.state = state;
37         }
38 }
39
40 public class AltosSiteMapTile extends JComponent {
41         int px_size;
42         File file;
43
44         Point2D.Double  boost;
45         Point2D.Double  landed;
46
47         LinkedList<AltosPoint>  points;
48
49         public void loadMap(File pngFile) {
50                 file = pngFile;
51                 repaint();
52         }
53
54         public void clearMap() {
55                 boost = null;
56                 landed = null;
57                 points = null;
58                 file = null;
59         }
60
61         static Color stateColors[] = {
62                 Color.WHITE,  // startup
63                 Color.WHITE,  // idle
64                 Color.WHITE,  // pad
65                 Color.RED,    // boost
66                 Color.PINK,   // fast
67                 Color.YELLOW, // coast
68                 Color.CYAN,   // drogue
69                 Color.BLUE,   // main
70                 Color.BLACK   // landed
71         };
72
73         private void draw_circle(Graphics g, Point2D.Double pt) {
74                 g.drawOval((int)pt.x-5, (int)pt.y-5, 10, 10);
75                 g.drawOval((int)pt.x-20, (int)pt.y-20, 40, 40);
76                 g.drawOval((int)pt.x-35, (int)pt.y-35, 70, 70);
77         }
78
79         public void set_boost(Point2D.Double boost) {
80                 this.boost = boost;
81                 repaint();
82         }
83
84         public void paint(Graphics g) {
85                 Graphics2D      g2d = (Graphics2D) g;
86                 AltosPoint      prev = null;
87                 Image           img = null;
88
89                 if (file != null)
90                         img = AltosSiteMapCache.get_image(this, file, px_size, px_size);
91
92                 if (img != null) {
93                         g2d.drawImage(img, 0, 0, null);
94                 } else {
95                         g2d.setColor(Color.GRAY);
96                         g2d.fillRect(0, 0, getWidth(), getHeight());
97                 }
98
99                 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
100                                    RenderingHints.VALUE_ANTIALIAS_ON);
101                 g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
102
103                 if (points != null) {
104                         for (AltosPoint point : points) {
105                                 if (prev != null) {
106                                         if (0 <= point.state && point.state < stateColors.length)
107                                                 g2d.setColor(stateColors[point.state]);
108                                         g2d.draw(new Line2D.Double(prev.pt, point.pt));
109                                 }
110                                 prev = point;
111                         }
112                 }
113                 if (boost != null) {
114                         g2d.setColor(Color.RED);
115                         draw_circle(g2d, boost);
116                 }
117                 if (landed != null) {
118                         g2d.setColor(Color.BLACK);
119                         draw_circle(g2d, landed);
120                 }
121         }
122
123         public synchronized void show(AltosState state, AltosListenerState listener_state,
124                                       Point2D.Double last_pt, Point2D.Double pt)
125         {
126                 if (points == null)
127                         points = new LinkedList<AltosPoint>();
128
129                 points.add(new AltosPoint(pt, state.state));
130
131                 if (state.state == AltosLib.ao_flight_boost && boost == null)
132                         boost = pt;
133                 if (state.state == AltosLib.ao_flight_landed && landed == null)
134                         landed = pt;
135                 repaint();
136         }
137
138         public AltosSiteMapTile(int in_px_size) {
139                 px_size = in_px_size;
140                 setPreferredSize(new Dimension(px_size, px_size));
141         }
142 }