-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileMain.java
More file actions
143 lines (124 loc) · 5.23 KB
/
Copy pathTileMain.java
File metadata and controls
143 lines (124 loc) · 5.23 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
// CSE 143, Homework 1, Tiles
// This provided main program uses your TileManager class.
// It displays a DrawingPanel, creates several random Tile objects,
// and adds them to your manager. It also listens for mouse clicks, notifying
// your tile manager when the mouse buttons are pressed.
// A left-click raises a tile to the top of the Z-order.
// A Shift-left-click lowers a tile to the bottom of the Z-order.
// A right-click (or a Ctrl-left-click for Mac people) deletes a tile.
// A Shift-right-click (or a Shift-Ctrl-left-click for Mac people) deletes
// all tiles touching the mouse point.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.Random;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;
public class TileMain {
// constants for the drawing panel size, tile sizes, and # of tiles
public static final int WIDTH = 300;
public static final int HEIGHT = 300;
public static final int MIN_SIZE = 20;
public static final int MAX_SIZE = 100;
public static final int TILES = 20;
// set to true to catch and print any exceptions that occur
private static final boolean CATCH_EXCEPTIONS = true;
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(WIDTH, HEIGHT);
Graphics g = panel.getGraphics();
// create several random tiles and put them into a manager
TileManager tiles = new TileManager();
for (int i = 0; i < TILES; i++) {
Tile tile = makeRandomTile();
tiles.addTile(tile);
}
tiles.drawAll(g);
// listen for key presses
RectangleKeyListener listener = new RectangleKeyListener(panel, tiles);
panel.addKeyListener(listener);
// listen for mouse clicks
RectangleMouseListener listener2 = new RectangleMouseListener(panel, tiles);
panel.addMouseListener(listener2);
}
// Creates and returns a random tile to be placed into the tile manager.
public static Tile makeRandomTile() {
// choose random coordinates
Random rand = new Random();
int w = rand.nextInt(MAX_SIZE - MIN_SIZE + 1) + MIN_SIZE;
int h = rand.nextInt(MAX_SIZE - MIN_SIZE + 1) + MIN_SIZE;
int x = rand.nextInt(WIDTH - w);
int y = rand.nextInt(HEIGHT - h);
// choose random color (avoid very darkest range of palette)
int red = rand.nextInt(206) + 50;
int green = rand.nextInt(206) + 50;
int blue = rand.nextInt(206) + 50;
Color color = new Color(red, green, blue);
// add random tile to manager
Tile tile = new Tile(x, y, w, h, color);
return tile;
}
// A class for responding to mouse clicks on the drawing panel.
public static class RectangleMouseListener extends MouseInputAdapter {
private DrawingPanel panel;
private TileManager tiles;
public RectangleMouseListener(DrawingPanel panel, TileManager tiles) {
this.panel = panel;
this.tiles = tiles;
}
public void mousePressed(MouseEvent event) {
int x = event.getX() / panel.getZoom();
int y = event.getY() / panel.getZoom();
try {
if (event.isControlDown() || SwingUtilities.isRightMouseButton(event)) {
// treat right-clicks and control-left-clicks as the same (for Mac users)
if (event.isShiftDown()) {
tiles.deleteAll(x, y);
} else {
tiles.delete(x, y);
}
} else {
if (event.isShiftDown()) {
tiles.lower(x, y);
} else {
tiles.raise(x, y);
}
}
// repaint all of the tiles
panel.clear();
Graphics g = panel.getGraphics();
tiles.drawAll(g);
} catch (RuntimeException e) {
if (CATCH_EXCEPTIONS) {
e.printStackTrace(System.err);
} else {
throw e;
}
}
}
}
// A class for responding to key presses on the drawing panel.
public static class RectangleKeyListener extends KeyAdapter {
private DrawingPanel panel;
private TileManager tiles;
public RectangleKeyListener(DrawingPanel panel, TileManager tiles) {
this.panel = panel;
this.tiles = tiles;
}
public void keyPressed(KeyEvent event) {
int code = event.getKeyCode();
if (code == KeyEvent.VK_N) {
Tile newTile = makeRandomTile();
tiles.addTile(newTile);
Graphics g = panel.getGraphics();
tiles.drawAll(g);
} else if (code == KeyEvent.VK_S) {
tiles.shuffle(WIDTH, HEIGHT);
Graphics g = panel.getGraphics();
panel.clear();
tiles.drawAll(g);
}
}
}
}