-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperPixelGrid.java
More file actions
153 lines (140 loc) · 6.18 KB
/
SuperPixelGrid.java
File metadata and controls
153 lines (140 loc) · 6.18 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
/***
* Runs the main application.
*
* @author kentcollins
* @version 3.1 adapted from StdLib to Java Swing
*/
public class SuperPixelGrid extends JFrame {
public static void main(String args[]) {
SuperPixelGrid spg = new SuperPixelGrid();
spg.setVisible(true);
System.out.println("Window should be open and listening");
}
private final int CANVAS_WIDTH = 512;
private final int CANVAS_HEIGHT = 512;
private final int CELL_SIZE = 16;
public SuperPixelGrid() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setTitle("SuperPixelGrid 3.1");
// center the window on the viewing screen
Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((int) screenDim.getWidth() / 2 - CANVAS_WIDTH / 2, (int) screenDim.getHeight() / 2 - CANVAS_HEIGHT / 2);
// create a panel component and add it to the window
this.add(new DisplayPanel());
this.pack();
}
private class DisplayPanel extends JPanel {
private CommandHandler c;
private SuperPixel[][] pixels;
private SuperPixel[][] buffer;
private SuperPixel[][] previous; // used for undo action
private boolean toolTips = false;
private int[] cellCoords = {-1, -1}; // row and column from upper left
private double[] lastMouse = {-1, -1}; // lastX, lastY
public DisplayPanel() {
this.setBackground(Color.DARK_GRAY);
this.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
pixels = new SuperPixel[CANVAS_WIDTH / CELL_SIZE][CANVAS_HEIGHT / CELL_SIZE];
for (int r = 0; r < pixels.length; r++) {
for (int c = 0; c < pixels[r].length; c++) {
pixels[r][c] = new SuperPixel(Color.BLACK);
}
}
previous = pixels;
buffer = pixels;
c = new CommandHandler();
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
c.commandUp(pixels);
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
c.commandLeft(pixels);
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
c.commandDown(pixels);
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
c.commandRight(pixels);
} else if (e.getKeyCode() == KeyEvent.VK_W) {
buffer = c.commandWhite(pixels);
} else if (e.getKeyCode() == KeyEvent.VK_R) {
buffer = c.commandRed(pixels);
} else if (e.getKeyCode() == KeyEvent.VK_G) {
buffer = c.commandGreen(pixels);
} else if (e.getKeyCode() == KeyEvent.VK_B) {
buffer = c.commandBlue(pixels);
} else if (e.getKeyCode() == KeyEvent.VK_C) {
c.commandClear(pixels);
} else if (e.getKeyCode() == KeyEvent.VK_L) {
previous = pixels;
buffer = c.lifeCommand(pixels);
pixels = buffer;
} else if (e.getKeyCode() == KeyEvent.VK_Z) {
if (previous != null) {
buffer = pixels;
pixels = previous;
previous = buffer;
}
} else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
if (buffer != null) {
previous = pixels;
pixels = buffer;
}
} else if (e.getKeyCode() == KeyEvent.VK_T) {
toolTips = !toolTips;
} else if (e.getKeyCode() == KeyEvent.VK_Q) {
System.exit(0);
}
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
lastMouse = new double[]{e.getX(), e.getY()};
int mouseRow = e.getY() / CELL_SIZE;
int mouseCol = e.getX() / CELL_SIZE;
cellCoords = new int[]{mouseRow, mouseCol};
repaint();
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
SuperPixel sp = pixels[e.getY() / CELL_SIZE][e.getX() / CELL_SIZE];
CommandHandler.clickSuperPixel(sp);
}
});
this.setFocusable(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (int r = 0; r < pixels.length; r++) {
for (int c = 0; c < pixels[r].length; c++) {
SuperPixel sp = pixels[r][c];
g2d.setPaint(sp.getColor());
g2d.fillOval(c * CELL_SIZE, r * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
if (toolTips) {
g2d.setPaint(java.awt.Color.WHITE);
int row = cellCoords[0]; // display upper left corner as (0, 0)
int col = cellCoords[1];
showMessage("[" + row + "] [" + col + "]", g2d);
}
}
public void showMessage(String s, Graphics2D g2d) {
Font font = new Font("SansSerif", Font.PLAIN, 16);
Rectangle2D textBox = font.getStringBounds(s, g2d.getFontRenderContext());
g2d.setFont(font);
g2d.setColor(Color.WHITE);
g2d.drawString(s, (int) (lastMouse[0] - textBox.getWidth() / 2), (int) lastMouse[1]);
}
}
}