1 | /* |
2 | * Copyright (c) 2005 The PseudoQ Project. |
3 | * |
4 | * This file is part of PseudoQ. |
5 | * |
6 | * PseudoQ is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU Lesser General Public License as published by |
8 | * the Free Software Foundation; either version 2.1 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * PseudoQ is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public License |
17 | * along with PseudoQ; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
19 | */ |
20 | |
21 | package net.sourceforge.pseudoq.gui; |
22 | |
23 | import java.awt.Color; |
24 | import java.awt.Dimension; |
25 | import java.awt.Font; |
26 | import java.awt.FontMetrics; |
27 | import java.awt.Graphics; |
28 | import java.util.HashMap; |
29 | import java.util.Map; |
30 | import java.util.Set; |
31 | |
32 | import net.sourceforge.pseudoq.model.Coordinate; |
33 | import net.sourceforge.pseudoq.model.Grid; |
34 | |
35 | /** |
36 | * Abstract superclass for grid painters. |
37 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
38 | */ |
39 | public abstract class AbstractGridPainter implements GridPainter { |
40 | private static final int DEFAULT_SIZE = 30; |
41 | protected static final int BORDER = 5; |
42 | |
43 | protected int size = DEFAULT_SIZE; |
44 | |
45 | public abstract void drawGrid(Graphics g); |
46 | |
47 | public abstract Dimension getPreferredSize(); |
48 | |
49 | public int getSize() { |
50 | return size; |
51 | } |
52 | |
53 | public void setSize(int size) { |
54 | this.size = size; |
55 | } |
56 | |
57 | public void printValues(Graphics g, Grid grid, Set<Coordinate> givens) { |
58 | for (Coordinate cell : grid.keySet()) { |
59 | Integer value = grid.get(cell); |
60 | if (value != null && value.intValue() != 0) { |
61 | if (givens.contains(cell)) { |
62 | g.setColor(Color.BLACK); |
63 | } else { |
64 | g.setColor(Color.BLUE); |
65 | } |
66 | String digit = Grid.digit(value.intValue()); |
67 | Dimension d = getDigitDimension(g, digit); |
68 | int x = translate(cell.getColumn()) + ((size - d.width) / 2); |
69 | int y = translate(cell.getRow()) + ((size + d.height) / 2); |
70 | g.drawString(digit, x, y); |
71 | } |
72 | } |
73 | } |
74 | |
75 | private Map<String, Dimension> digitDimensionMap = new HashMap<String, Dimension>(); |
76 | |
77 | private Dimension getDigitDimension(Graphics g, String digit) { |
78 | Dimension ret = digitDimensionMap.get(digit); |
79 | if (ret == null) { |
80 | Font f = g.getFont(); |
81 | FontMetrics fm = g.getFontMetrics(f); |
82 | int width = fm.stringWidth(digit); |
83 | int height = fm.getAscent(); // Digits don't have tails |
84 | ret = new Dimension(width, height); |
85 | digitDimensionMap.put(digit, ret); |
86 | } |
87 | return ret; |
88 | } |
89 | |
90 | public void highlightCell(Graphics g, Coordinate cell) { |
91 | g.setColor(Color.RED); |
92 | int x1 = translate(cell.getColumn()) + 2; |
93 | int x2 = translate(cell.getColumn() + 1) - 2; |
94 | int y1 = translate(cell.getRow()) + 2; |
95 | int y2 = translate(cell.getRow() + 1) - 2; |
96 | g.drawPolyline(new int[] {x1, x2, x2, x1, x1}, |
97 | new int[] {y1, y1, y2, y2, y1}, 5); |
98 | g.drawPolyline(new int[] {x1 + 1, x2 - 1, x2 - 1, x1 + 1, x1 + 1}, |
99 | new int[] {y1 + 1, y1 + 1, y2 - 1, y2 - 1, y1 + 1}, 5); |
100 | } |
101 | |
102 | public void unhighlightCell(Graphics g, Coordinate cell) { |
103 | // g.setColor(Color.BLACK); |
104 | // int x1 = 5 + (cell.getColumn() * size); |
105 | // int x2 = 5 + ((cell.getColumn() + 1) * size); |
106 | // int y1 = 5 + (cell.getRow() * size); |
107 | // int y2 = 5 + ((cell.getRow() + 1) * size); |
108 | // g.drawPolyline(new int[] {x1, x2, x2, x1, x1}, |
109 | // new int[] {y1, y1, y2, y2, y1}, 5); |
110 | } |
111 | |
112 | /** |
113 | * Draws a line between the specified (cell-based) coordinates. |
114 | * @param g Graphics context |
115 | * @param x1 first x-coordinate |
116 | * @param y1 first y-coordinate |
117 | * @param x2 second x-coordinate |
118 | * @param y2 second y-coordinate |
119 | * @param linewidth width of the line in pixels |
120 | * @param c Color of the line |
121 | */ |
122 | protected void drawLine(Graphics g, int x1, int y1, int x2, int y2, int linewidth, Color c) { |
123 | g.setColor(c); |
124 | if (linewidth > 1 || (x1 != x2 && y1 != y2)) { |
125 | if (x1 == x2) { |
126 | // vertical |
127 | for (int x = translate(x1) - (linewidth / 2); x <= translate(x1) + (linewidth / 2); x++) { |
128 | g.drawLine(x, translate(y1), x, translate(y2)); |
129 | } |
130 | } else { |
131 | // horizontal |
132 | for (int y = translate(y1) - (linewidth / 2); y <= translate(y1) + (linewidth / 2); y++) { |
133 | g.drawLine(translate(x1), y, translate(x2), y); |
134 | } |
135 | } |
136 | } else { |
137 | // no width, or a diagonal (which we don't handle) |
138 | g.drawLine(translate(x1), translate(y1), translate(x2), translate(y2)); |
139 | } |
140 | } |
141 | |
142 | /** |
143 | * Draws a rectangular outline around the specified cells. |
144 | * @param g Graphics context |
145 | * @param x column of the top-left cell |
146 | * @param y row of the top-left cell |
147 | * @param width width in cells of the area to fill |
148 | * @param height height in cells of the area to fill |
149 | * @param linewidth width of the line in pixels |
150 | * @param c Color of the line |
151 | */ |
152 | protected void drawBox(Graphics g, int x, int y, int width, int height, int linewidth, Color c) { |
153 | int x1 = translate(x); |
154 | int x2 = translate(x + width); |
155 | int y1 = translate(y); |
156 | int y2 = translate(y + height); |
157 | g.setColor(c); |
158 | for (int j = -(linewidth / 2); j <= (linewidth / 2); j++) { |
159 | g.drawLine(x1 - (linewidth / 2), y1 + j, x2 + (linewidth / 2), y1 + j); |
160 | g.drawLine(x1 + j, y1 - (linewidth / 2), x1 + j, y2 + (linewidth / 2)); |
161 | g.drawLine(x1 - (linewidth / 2), y2 + j, x2 + (linewidth / 2), y2 + j); |
162 | g.drawLine(x2 + j, y1 - (linewidth / 2), x2 + j, y2 + (linewidth / 2)); |
163 | } |
164 | } |
165 | |
166 | /** |
167 | * Draws a filled rectangle covering the specified cells. |
168 | * @param g Graphics context |
169 | * @param x column of the top-left cell |
170 | * @param y row of the top-left cell |
171 | * @param width width in cells of the area to fill |
172 | * @param height height in cells of the area to fill |
173 | * @param c Color to fill the area width |
174 | */ |
175 | protected void fillBox(Graphics g, int x, int y, int width, int height, Color c) { |
176 | g.setColor(c); |
177 | g.fillRect(translate(x), translate(y), width * size, height * size); |
178 | } |
179 | |
180 | /** |
181 | * Convert (scale & translate) a cell-based measure to pixels. |
182 | */ |
183 | protected int translate(int x) { |
184 | return BORDER + (x * size); |
185 | } |
186 | |
187 | } |