-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayCanvas.java
More file actions
92 lines (83 loc) · 2.85 KB
/
DisplayCanvas.java
File metadata and controls
92 lines (83 loc) · 2.85 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
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyAdapter;
public class DisplayCanvas {
private JPanel panel;
private JFrame frame;
private Graphics g;
private InputHandler inputListener;
private final int[] RESOLUTION;
public DisplayCanvas(int resX, int resY, int widthMultiplier, int heightMultiplier, String windowName) {
RESOLUTION = new int[] {resX, resY};
frame = new JFrame(windowName);
frame.setSize(resX * widthMultiplier, resY * heightMultiplier);
panel = new JPanel(new FlowLayout());
panel.setPreferredSize(new Dimension(resX * widthMultiplier, resY * heightMultiplier));
BufferedImage img = new BufferedImage(resX * widthMultiplier, resY * heightMultiplier, BufferedImage.TYPE_INT_ARGB);
g = img.getGraphics();
JLabel label = new JLabel();
label.setIcon(new ImageIcon(img));
panel.add(label);
frame.add(panel);
frame.setLayout(new FlowLayout());
label.setBackground(Color.BLACK);
frame.setBackground(Color.BLACK);
panel.setBackground(Color.BLACK);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setFocusable(true);
inputListener = new InputHandler();
frame.addKeyListener(inputListener);
}
/*public DisplayCanvas(int width, int height, String windowName) {
DisplayCanvas(width, height, 1, 1, windowName);
}*/
public Graphics getGraphics() {
return g;
}
//returns size of panel as an int[]
public int[] getDimensions() {
return new int[] {(int) panel.getWidth(), (int) panel.getHeight()};
}
//returns boolean array of all keyboard input
public boolean[] getKeyInputs() {
return inputListener.getKeyArray();
}
//changes name for window to given name
public void setWindowName(String windowName) {
frame.setTitle(windowName);
}
//repaints the JFrame
public void refresh() {
frame.repaint();
}
public void zoomIn() {
panel.setSize(new Dimension(panel.getWidth() + RESOLUTION[0], panel.getHeight() + RESOLUTION[1]));
}
public void zoomOut() {
if (panel.getWidth() > RESOLUTION[0]) {
panel.setSize(new Dimension(panel.getWidth() + RESOLUTION[0], panel.getHeight() + RESOLUTION[1]));
}
}
public void zoomIn(int count) {
for (int i = 0; i < count; i++) {
zoomIn();
}
}
public void zoomOut(int count) {
for (int i = 0; i < count; i++) {
zoomOut();
}
}
public void zoomTo(int zoom) {
panel.setSize(new Dimension(RESOLUTION[0] * zoom, RESOLUTION[1] * zoom));
}
}