diff options
Diffstat (limited to 'src/com/fourisland/fourpuzzle/window/MessageWindow.java')
-rw-r--r-- | src/com/fourisland/fourpuzzle/window/MessageWindow.java | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/src/com/fourisland/fourpuzzle/window/MessageWindow.java b/src/com/fourisland/fourpuzzle/window/MessageWindow.java new file mode 100644 index 0000000..9e482ba --- /dev/null +++ b/src/com/fourisland/fourpuzzle/window/MessageWindow.java | |||
@@ -0,0 +1,139 @@ | |||
1 | /* | ||
2 | * To change this template, choose Tools | Templates | ||
3 | * and open the template in the editor. | ||
4 | */ | ||
5 | |||
6 | package com.fourisland.fourpuzzle.window; | ||
7 | |||
8 | import com.fourisland.fourpuzzle.Game; | ||
9 | import java.awt.Font; | ||
10 | import java.awt.FontFormatException; | ||
11 | import java.awt.Graphics2D; | ||
12 | import java.awt.Rectangle; | ||
13 | import java.awt.TexturePaint; | ||
14 | import java.awt.image.BufferedImage; | ||
15 | import java.io.File; | ||
16 | import java.io.IOException; | ||
17 | import java.util.ArrayList; | ||
18 | import java.util.List; | ||
19 | import java.util.logging.Level; | ||
20 | import java.util.logging.Logger; | ||
21 | |||
22 | /** | ||
23 | * | ||
24 | * @author hatkirby | ||
25 | */ | ||
26 | public class MessageWindow { | ||
27 | |||
28 | private static final int SPACER = 4; | ||
29 | private static final int HEIGHT = 4*(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics().getFontMetrics().getHeight()+SPACER); | ||
30 | |||
31 | private List<String> messages; | ||
32 | int width; | ||
33 | BufferedImage cacheBase; | ||
34 | public MessageWindow(String message) | ||
35 | { | ||
36 | width = Game.WIDTH - Window.Default.getFullWidth(0); | ||
37 | messages = new ArrayList<String>(); | ||
38 | |||
39 | initalizeMessages(message, new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).createGraphics()); | ||
40 | |||
41 | cacheBase = new BufferedImage(Game.WIDTH, Window.Default.getFullHeight(HEIGHT), BufferedImage.TYPE_INT_ARGB); | ||
42 | Graphics2D g2 = cacheBase.createGraphics(); | ||
43 | |||
44 | g2.drawImage(SystemGraphic.getMessageBackground(), 1, 1, Game.WIDTH-2, Window.Default.getFullHeight(HEIGHT)-2, null); | ||
45 | g2.drawImage(Window.Default.getImage(width, HEIGHT), 0, 0, null); | ||
46 | } | ||
47 | |||
48 | private void initalizeMessages(String message, Graphics2D g) | ||
49 | { | ||
50 | setFont(g); | ||
51 | |||
52 | String temp = message; | ||
53 | int len = 0; | ||
54 | while (!temp.isEmpty()) | ||
55 | { | ||
56 | while ((g.getFontMetrics().stringWidth(temp.substring(0, len)) < (width - SPACER)) && (len < temp.length())) | ||
57 | { | ||
58 | len++; | ||
59 | } | ||
60 | |||
61 | if (len != temp.length()) | ||
62 | { | ||
63 | while ((!temp.substring(len, len+1).equals(" ")) && (len > 0)) | ||
64 | { | ||
65 | len--; | ||
66 | } | ||
67 | } | ||
68 | |||
69 | messages.add(temp.substring(0, len)); | ||
70 | |||
71 | if (len != temp.length()) | ||
72 | { | ||
73 | temp = temp.substring(len+1); | ||
74 | } else { | ||
75 | temp = ""; | ||
76 | } | ||
77 | |||
78 | len = 0; | ||
79 | } | ||
80 | } | ||
81 | |||
82 | public void render(Graphics2D g2) | ||
83 | { | ||
84 | int y = MessageWindowLocation.Bottom.getY(); | ||
85 | |||
86 | g2.drawImage(cacheBase, 0, y, null); | ||
87 | |||
88 | setFont(g2); | ||
89 | |||
90 | int fh = g2.getFontMetrics().getHeight(); | ||
91 | int ty = Window.Default.getTopY()+fh-(SPACER/2)+y; | ||
92 | int msgs = Math.min(messages.size(), 4); | ||
93 | for (int i=0;i<msgs;i++) | ||
94 | { | ||
95 | String message = messages.get(i); | ||
96 | int fw = g2.getFontMetrics().stringWidth(message); | ||
97 | int tx = Window.Default.getLeftX(); | ||
98 | |||
99 | g2.setPaint(new TexturePaint(SystemGraphic.getTextColor(), new Rectangle(tx, ty, fw, fh))); | ||
100 | g2.drawString(message, tx, ty); | ||
101 | |||
102 | ty+=(SPACER+g2.getFontMetrics().getHeight()); | ||
103 | } | ||
104 | |||
105 | g2.setFont(g2.getFont().deriveFont(Font.PLAIN)); | ||
106 | } | ||
107 | |||
108 | public static enum MessageWindowLocation | ||
109 | { | ||
110 | Top(0), | ||
111 | Middle((Game.HEIGHT/2)-(Window.Default.getFullHeight(MessageWindow.HEIGHT)/2)), | ||
112 | Bottom(Game.HEIGHT-Window.Default.getFullHeight(MessageWindow.HEIGHT)); | ||
113 | |||
114 | private int y; | ||
115 | private MessageWindowLocation(int y) | ||
116 | { | ||
117 | this.y = y; | ||
118 | } | ||
119 | |||
120 | public int getY() | ||
121 | { | ||
122 | return y; | ||
123 | } | ||
124 | } | ||
125 | |||
126 | public static void setFont(Graphics2D g) | ||
127 | { | ||
128 | try { | ||
129 | g.setFont(Font.createFont(Font.TRUETYPE_FONT, new File("/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf"))); | ||
130 | } catch (FontFormatException ex) { | ||
131 | Logger.getLogger(MessageWindow.class.getName()).log(Level.SEVERE, null, ex); | ||
132 | } catch (IOException ex) { | ||
133 | Logger.getLogger(MessageWindow.class.getName()).log(Level.SEVERE, null, ex); | ||
134 | } | ||
135 | |||
136 | g.setFont(g.getFont().deriveFont(Font.PLAIN, 10)); | ||
137 | } | ||
138 | |||
139 | } | ||