-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreen.java
More file actions
207 lines (160 loc) · 5.41 KB
/
Screen.java
File metadata and controls
207 lines (160 loc) · 5.41 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
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.HashSet;
import javax.swing.JPanel;
public class Screen extends JPanel
implements ActionListener, KeyListener, MouseMotionListener, MouseListener {
// Holds the pixels that will painted to the screen on each frame
// Can be edited by different classes in order to change the frame
public Color[][] pixels = new Color[500][500];
// Create a new viewer object
public Screen() {
super();
this.addMouseMotionListener(this);
this.addMouseListener(this);
initSnowFlakes(100);
// this.setFocusable(true);
// this.requestFocusInWindow();
// this.addKeyListener(this);
}
public void actionPerformed(ActionEvent e) {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
pixels = new Color[getWidth()][getHeight()];
setScreenWhite();
World.updatePixels(this);
for (int i = 0; i < pixels.length; i++) {
for (int j = 0; j < pixels[0].length; j++) {
if (!pixels[i][j].equals(Color.WHITE)) {
g.setColor(pixels[i][j]);
g.fillRect(i - 5, j - 5, 10, 10);
}
}
}
// Add snowflakes to the screen
updateSnowFlakes();
for(int i = 0; i < snowFlakes.size(); i++){
Vector3 point = snowFlakes.get(i);
g.setColor(Color.GRAY);
g.fillOval((int)(point.x - 5), (int)point.y - 5, 10, 10);
}
}
ArrayList<Vector3> snowFlakes = new ArrayList<Vector3>();
ArrayList<Vector3> snowFlakeVelocity = new ArrayList<Vector3>();
public void initSnowFlakes(int count){
snowFlakes = new ArrayList<Vector3>();
snowFlakeVelocity = new ArrayList<Vector3>();
for(int i = 0; i < count; i++){
int x = (int)(Math.random() * getWidth());
snowFlakes.add(new Vector3(x, 0, 0));
snowFlakeVelocity.add(new Vector3(Math.random() * 10 - 5, Math.random() * 5, 0));
}
}
public void updateSnowFlakes(){
for(int i = 0; i < snowFlakes.size(); i++){
Vector3 velocity = snowFlakeVelocity.get(i);
Vector3 snow = snowFlakes.get(i);
snow.x += velocity.x;
snow.y += velocity.y;
if(snow.x >= getWidth() || snow.y >= getHeight()){
snow.x = Math.random() * getWidth();
snow.y = 0;
}
}
}
public void setScreenWhite() {
for (int i = 0; i < pixels.length; i++) {
for (int j = 0; j < pixels[0].length; j++) {
pixels[i][j] = Color.WHITE;
}
}
}
public static HashSet<Integer> buttonsDown = new HashSet<Integer>();
public void keyPressed(KeyEvent e) {
buttonsDown.add(e.getKeyCode());
}
public void keyReleased(KeyEvent e) {
buttonsDown.remove(new Integer(e.getKeyCode()));
}
private void moveMouse(Point p) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
// Search the devices for the one that draws the specified point.
for (GraphicsDevice device : gs) {
GraphicsConfiguration[] configurations = device.getConfigurations();
for (GraphicsConfiguration config : configurations) {
Rectangle bounds = config.getBounds();
if (bounds.contains(p)) {
// Set point to screen coordinates.
Point b = bounds.getLocation();
Point s = new Point(p.x - b.x, p.y - b.y);
try {
Robot r = new Robot(device);
r.mouseMove(s.x, s.y);
} catch (AWTException e) {
e.printStackTrace();
}
return;
}
}
}
// Couldn't move to the point, it may be off screen.
return;
}
Vector3 lastMouseCoords = new Vector3(-1, -1, -1);
Vector3 defaultCoords = new Vector3(100, 100, 0);
public void mouseDragged(MouseEvent e) {
if (e.getLocationOnScreen().x == defaultCoords.x && e.getLocationOnScreen().y == defaultCoords.y) {
return;
}
if(lastMouseCoords.x == -1 && lastMouseCoords.y == -1){
lastMouseCoords.x = e.getX();
lastMouseCoords.y = e.getY();
return;
}
// This code is for desktop
//int xDiff = (int) (e.getLocationOnScreen().x - defaultCoords.x);
//int yDiff = (int) (e.getLocationOnScreen().y - defaultCoords.y);
//moveMouse(new Point(100, 100));
// This code is for replit
int xDiff = (int) (e.getX() - lastMouseCoords.x);
int yDiff = (int) (e.getY() - lastMouseCoords.y);
lastMouseCoords.x = e.getX();
lastMouseCoords.y = e.getY();
World.camera.rotation.y += xDiff / 1000.0 * World.camera.turnSensitivity;
World.camera.rotation.x -= yDiff / 1000.0 * World.camera.turnSensitivity;
}
@Override
public void keyTyped(KeyEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
lastMouseCoords.x = -1;
lastMouseCoords.y = -1;
}
public void mouseClicked(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}