-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClaimView.java
More file actions
306 lines (286 loc) · 11 KB
/
ClaimView.java
File metadata and controls
306 lines (286 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
/**
* Visuelle Darstellung eines {@link Claim}: eine Draufsicht als Raster links
* und eine Seitenansicht (gestapelte Erdschichten) rechts für die selektierte
* Parzelle.
*/
public class ClaimView extends JFrame {
private Claim claim;
private int cellSize = 40;
private JPanel gridPanel;
private JPanel sidePanel;
private int selectedRow = -1;
private int selectedCol = -1;
private JTextArea outputArea;
private JPanel legendPanel;
public ClaimView(Claim pClaim, int pCellSize) {
super("Claim Viewer");
this.claim = pClaim;
if (pCellSize > 5)
this.cellSize = pCellSize;
initUI();
}
private void initUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int rows = claim.gibZeilen();
int cols = claim.gibSpalten();
gridPanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawGrid((Graphics2D) g);
}
};
gridPanel.setFocusable(true);
gridPanel.setPreferredSize(new Dimension(cols * cellSize, rows * cellSize));
gridPanel.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int cols = claim.gibSpalten();
int rows = claim.gibZeilen();
int w = gridPanel.getWidth();
int h = gridPanel.getHeight();
if (w <= 0 || h <= 0 || cols <= 0 || rows <= 0)
return;
int col = Math.min(cols - 1, Math.max(0, e.getX() * cols / w));
int row = Math.min(rows - 1, Math.max(0, e.getY() * rows / h));
if (row >= 0 && row < rows && col >= 0 && col < cols) {
gridPanel.requestFocusInWindow();
selectedRow = row;
selectedCol = col;
sidePanel.repaint();
gridPanel.repaint();
}
}
});
// Key bindings for arrow keys (work when window focused)
InputMap im = gridPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = gridPanel.getActionMap();
im.put(KeyStroke.getKeyStroke("LEFT"), "left");
im.put(KeyStroke.getKeyStroke("RIGHT"), "right");
im.put(KeyStroke.getKeyStroke("UP"), "up");
im.put(KeyStroke.getKeyStroke("DOWN"), "down");
am.put("left", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
moveSelection(0, -1);
}
});
am.put("right", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
moveSelection(0, 1);
}
});
am.put("up", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
moveSelection(-1, 0);
}
});
am.put("down", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
moveSelection(1, 0);
}
});
sidePanel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawSideView((Graphics2D) g);
}
};
sidePanel.setPreferredSize(new Dimension(300, Math.max(300, rows * cellSize)));
getContentPane().setLayout(new BorderLayout());
JPanel left = new JPanel(new BorderLayout());
left.add(gridPanel, BorderLayout.CENTER);
getContentPane().add(left, BorderLayout.CENTER);
getContentPane().add(sidePanel, BorderLayout.EAST);
// Bottom area: legend + output
JPanel bottom = new JPanel(new BorderLayout());
legendPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
legendPanel.add(new JLabel("Legende: a = Abbauen, s = Stabilitätsprüfung, Pfeiltasten = Selektion"));
bottom.add(legendPanel, BorderLayout.NORTH);
outputArea = new JTextArea(4, 60);
outputArea.setEditable(false);
outputArea.setLineWrap(true);
outputArea.setWrapStyleWord(true);
JScrollPane sp = new JScrollPane(outputArea);
bottom.add(sp, BorderLayout.CENTER);
getContentPane().add(bottom, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
/**
* Fügt eine Key-Bindung zum internen Grid-Panel hinzu. `keySpec` ist ein
* KeyStroke-String wie "typed a" oder "pressed A".
*/
public void addKeyAction(String keySpec, String actionName, Action action) {
InputMap im = gridPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = gridPanel.getActionMap();
im.put(KeyStroke.getKeyStroke(keySpec), actionName);
am.put(actionName, action);
}
/** Hängt eine Zeile an den Ausgabebereich an. */
public void appendOutput(String msg) {
if (outputArea != null) {
outputArea.append(msg + "\n");
outputArea.setCaretPosition(outputArea.getDocument().getLength());
}
}
/** Liefert aktuell selektierte Zeile oder -1. */
public int gibSelectedZeile() {
return selectedRow;
}
/** Liefert aktuell selektierte Spalte oder -1. */
public int gibSelectedSpalte() {
return selectedCol;
}
/** Erzwingt ein Neuzeichnen der View-Teile. */
public void repaintView() {
if (sidePanel != null)
sidePanel.repaint();
if (gridPanel != null)
gridPanel.repaint();
}
private void drawGrid(Graphics2D g2d) {
int rows = claim.gibZeilen();
int cols = claim.gibSpalten();
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
Parzelle p = claim.gibParzelle(r, c);
Color color = Color.LIGHT_GRAY;
if (p != null) {
Erdschicht top = p.gibObereErdschicht();
if (top != null)
color = colorForSorte(top.gibSorte());
}
int x = c * cellSize;
int y = r * cellSize;
g2d.setColor(color);
g2d.fillRect(x, y, cellSize, cellSize);
g2d.setColor(Color.DARK_GRAY);
g2d.drawRect(x, y, cellSize, cellSize);
if (r == selectedRow && c == selectedCol) {
g2d.setColor(new Color(255, 0, 0, 128));
g2d.fillRect(x + 2, y + 2, cellSize - 4, cellSize - 4);
}
}
}
}
private void drawSideView(Graphics2D g2d) {
int w = sidePanel.getWidth();
int h = sidePanel.getHeight();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, w, h);
if (selectedRow < 0 || selectedCol < 0) {
g2d.setColor(Color.BLACK);
g2d.drawString("Klicke eine Parzelle an", 10, 20);
return;
}
Parzelle p = claim.gibParzelle(selectedRow, selectedCol);
if (p == null)
return;
List<Erdschicht> layers = p.gibAlleSchichten();
double totalDicke = 0.0;
for (Erdschicht e : layers) {
if (e != null)
totalDicke += e.gibDicke();
}
if (totalDicke <= 0.0) {
g2d.setColor(Color.BLACK);
g2d.drawString("Keine Schichten", 10, 20);
return;
}
int x = 20;
int layerX = x;
int layerWidth = w - 40;
int maxH = h - 20; // maximal mögliche Höhe für Parzelle
// Die Parzellenhöhe ist proportional zur aktuellen Gesamtdicke (maximal maxH)
double maxDicke = 0.0;
// Ermittle maximale Dicke aller Parzellen im Claim (für Vergleich)
for (int r = 0; r < claim.gibZeilen(); r++) {
for (int c = 0; c < claim.gibSpalten(); c++) {
Parzelle pz = claim.gibParzelle(r, c);
if (pz != null) {
double d = 0.0;
for (Erdschicht e : pz.gibAlleSchichten()) {
if (e != null)
d += e.gibDicke();
}
if (d > maxDicke)
maxDicke = d;
}
}
}
// Die aktuelle Parzelle bekommt eine Höhe proportional zu ihrer Gesamtdicke
int parzellenH = (maxDicke > 0.0) ? (int) Math.round((totalDicke / maxDicke) * maxH) : 1;
int yBottom = h - 10; // Boden bleibt fest
double pixelPerDicke = (totalDicke > 0.0) ? (parzellenH / totalDicke) : 1.0;
int yPos = yBottom;
// Von unten nach oben: unterste Schicht zuerst
for (int idx = 0; idx < layers.size(); idx++) {
Erdschicht e = layers.get(idx);
if (e == null || e.gibDicke() <= 0.0)
continue;
int lh = Math.max(1, (int) Math.round(e.gibDicke() * pixelPerDicke));
yPos -= lh;
Color c = colorForSorte(e.gibSorte());
g2d.setColor(c);
g2d.fillRect(layerX, yPos, layerWidth, lh);
g2d.setColor(Color.DARK_GRAY);
g2d.drawRect(layerX, yPos, layerWidth, lh);
g2d.setColor(Color.BLACK);
g2d.drawString(String.format("%s (%.2f)", e.gibSorte(), e.gibDicke()), layerX + 5, yPos + lh / 2);
}
}
private Color colorForSorte(String s) {
if (s == null)
return Color.LIGHT_GRAY;
String key = s.toLowerCase();
switch (key) {
case "sand":
return new Color(237, 201, 175);
case "lehm":
return new Color(181, 101, 29);
case "humus":
return new Color(34, 139, 34);
case "ton":
return new Color(210, 180, 140);
case "erde":
return new Color(160, 82, 45);
case "gras":
return new Color(50, 205, 50);
default:
int h = Math.abs(key.hashCode());
int r = (h & 0xFF0000) >> 16;
int g = (h & 0x00FF00) >> 8;
int b = (h & 0x0000FF);
return new Color((r % 200) + 30, (g % 200) + 30, (b % 200) + 30);
}
}
/**
* Verschiebt die Selektion um die angegebenen Versätze (dr, dc).
*/
private void moveSelection(int dr, int dc) {
int rows = claim.gibZeilen();
int cols = claim.gibSpalten();
if (rows <= 0 || cols <= 0)
return;
if (selectedRow < 0 || selectedCol < 0) {
selectedRow = 0;
selectedCol = 0;
} else {
selectedRow = Math.min(rows - 1, Math.max(0, selectedRow + dr));
selectedCol = Math.min(cols - 1, Math.max(0, selectedCol + dc));
}
sidePanel.repaint();
gridPanel.repaint();
gridPanel.requestFocusInWindow();
}
}