Source code: org/zazof/jteg/gui/MissionCanvas.java
1 package org.zazof.jteg.gui;
2
3 import org.zazof.jteg.*;
4 import java.awt.*;
5 import java.awt.image.*;
6 import java.awt.image.BufferedImage;
7 import java.awt.event.*;
8 import javax.swing.*;
9 import java.net.URL;
10 import java.net.URLClassLoader;
11 import java.util.StringTokenizer;
12
13 /**
14 * MissionCanvas is the class responsible for drawing the MissionScreen
15 *
16 * @author Yves Vandewoude
17 *
18 */
19 public class MissionCanvas extends Canvas implements MessageListener{
20
21 /**
22 * Constructor
23 *
24 */
25 public MissionCanvas(JFrame parent)
26 {
27 super();
28 $parent = parent;
29 // needed to load the png image from the jar
30 URLClassLoader urlLoader = (URLClassLoader)getClass().getClassLoader();
31
32 URL fileLoc = urlLoader.findResource("org/zazof/jteg/resources/stock/objetivo.png");
33 $missionImage = Toolkit.getDefaultToolkit().createImage(fileLoc);
34 // Active waiting until width and height are known...
35 do{
36 $width = (int)($missionImage.getWidth(null)*1.5);
37 $height = (int)($missionImage.getHeight(null)*1.5);
38 }
39 while (!(($width > 0) && ($height > 0)));
40 if (DEBUG) System.out.println("Scaled image has size: " + $width + "x"+ $height);
41 $missionImage = $missionImage.getScaledInstance($width,$height,Image.SCALE_SMOOTH);
42 setBackground(Color.white);
43 $parent.setSize($width+7, $height+25);
44 setSize($width+6, $height+24);
45 }
46
47
48 /**
49 *
50 * @see org.zazof.jteg.gui.BoardCanvas update method
51 *
52 */
53 public void paint(Graphics g) {
54 update(g);
55 }
56
57 /**
58 *
59 * Draw & Paint method. Draws the missionscreen
60 *
61 *
62 */
63 public void update(Graphics g)
64 {
65 // Give the missionframe the correct title
66 $parent.setTitle(LanguageManager.getInstance().lookup("yourmission"));
67
68 // Draw the graphics
69 Graphics2D g2 = (Graphics2D) g;
70 BufferedImage bi = (BufferedImage) createImage($width, $height);
71 Graphics2D g2d = bi.createGraphics();
72 g2d.drawImage($missionImage,0,0,this);
73
74 // Print your mission (using pretty printing)
75 StringTokenizer token = new StringTokenizer(getMissionString());
76 int nbOfWords = token.countTokens();
77 int index = 0;
78 int regelNummer = 0;
79 while (index < nbOfWords)
80 {
81 String tempString = "";
82 while ((tempString.length() < 30)&& (index <nbOfWords))
83 {
84 tempString += token.nextToken() + " ";
85 index++;
86 }
87 g2d.drawString(tempString, 20, 40+(regelNummer*20));
88 regelNummer ++;
89 }
90 g2.drawImage(bi, 0, 0, this);
91 }
92
93
94 /**
95 * Required to implement the messageListener interface
96 *
97 */
98 public void messageArrived(Message m){
99 if (m.getMessageName().equals("mission"))
100 {
101 if (DEBUG) System.out.println("Received a MissionMessage!");
102 MissionMessage mm = (MissionMessage) m;
103 $missionType = mm.getMissionType();
104 $missionNumber = mm.getMissionNumber();
105 if (! (($missionNumber == 0) && ($missionType == 1)))
106 {
107 $parent.setVisible(true);
108 }
109 }
110 }
111
112
113 /*
114 * Private method to receive the languagestring of the current mission
115 *
116 */
117 private String getMissionString()
118 {
119 if ($missionType == 1)
120 {
121 // Private Mission
122 return (LanguageManager.getInstance().lookup("mission"+$missionNumber));
123 }
124 else if ($missionType == 0)
125 {
126 // Conquer the world...
127 return (LanguageManager.getInstance().lookup("conquerworld"));
128 }
129 else
130 return "";
131 }
132
133
134
135 /*
136 * Private Variables
137 *
138 */
139 private Image $missionImage;
140 private JFrame $parent;
141 private static final boolean DEBUG = false;
142 private int $width;
143 private int $height;
144 private int $missionType; // the type of the mission: 0 for conquer world, 1 for secret mission
145 private int $missionNumber; // the number of the mission in case it was a secret mission
146 }
147