-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJRandomBouncingImage.java
More file actions
219 lines (179 loc) · 5.97 KB
/
JRandomBouncingImage.java
File metadata and controls
219 lines (179 loc) · 5.97 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
/* copyright
*
* JRandomBouncingImage - Java clone of Common Desktop Environment
* 'Random bouncing image' screensaver
* (C) 2020 Keian Rao
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at * your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*
copyright */
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Toolkit;
import java.awt.Image;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.AlphaComposite;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
public class JRandomBouncingImage extends JFrame {
// Private variables // \\ // \\ // \\
Image image;
Color[] colours;
// Helper functions // \\ // \\ // \\
private static int randomInteger(int lowerBound, int upperBound) {
// Upper bound is exclusive, since in this application
// we use this function basically only for offsets.
double randomGain = Math.random() * (upperBound - lowerBound);
return lowerBound + (int)Math.floor(randomGain);
}
// Private classes // \\ // \\ // \\
private class Pane extends JPanel implements ActionListener {
public void actionPerformed(ActionEvent eA) {
repaint();
}
public void paintComponent(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
if (image == null) return;
int gridWidth = image.getWidth(this);
int gridHeight = image.getHeight(this);
if (gridWidth == -1) return;
// Image is still loading, so cancel.
int gridColumns = getWidth() / gridWidth;
int gridRows = getHeight() / gridHeight;
int xOffset = (getWidth() % gridWidth) / 2;
int yOffset = (getHeight() % gridHeight) / 2;
int bouncingsToGenerate = (gridColumns + gridRows) / 3;
Bouncing[] bouncings = new Bouncing[bouncingsToGenerate];
while (bouncingsToGenerate-- > 0) {
bouncings[bouncingsToGenerate] =
randomBouncing(gridColumns, gridRows);
}
// Okay, let's start actually drawing.
// Draw to an in-memory buffer first. We're going to use
// alpha composites, and we need trasparency for that.
BufferedImage buffer = new BufferedImage(
getWidth(), getHeight(),
BufferedImage.TYPE_INT_ARGB
);
Graphics gBuf = buffer.getGraphics();
Graphics2D g2Buf = (Graphics2D)gBuf;
for (Bouncing bouncing: bouncings) {
int normalX = (bouncing.gridX * gridWidth) + xOffset;
int normalY = (bouncing.gridY * gridHeight) + yOffset;
// Draw the image itself.
gBuf.drawImage(image, normalX, normalY, this);
// Now fill a coloured rect with SrcIn.
// The destination area, aside from the image drawn earlier,
// should be empty pixels (alpha 0) - so this should work.
g2Buf.setComposite(AlphaComposite.SrcIn);
g2Buf.setColor(bouncing.colour);
g2Buf.fillRect(
normalX, normalY,
gridWidth, gridHeight
);
g2Buf.setComposite(AlphaComposite.SrcOver);
}
// Alright, now just draw the buffer on top.
g.drawImage(buffer, 0, 0, this);
}
private Bouncing randomBouncing(int gridColumns, int gridRows) {
Bouncing returnee = new Bouncing();
returnee.gridX = randomInteger(0, gridColumns);
returnee.gridY = randomInteger(0, gridRows);
returnee.colour = colours[randomInteger(0, colours.length)];
return returnee;
}
}
private static class Bouncing {
int gridX, gridY;
Color colour;
}
// Constructors \\ // \\ // \\ // \\
private JRandomBouncingImage() {
super("Random bouncing image");
// Some of our properties first..
setBackground(Color.BLACK);
// Now, set up our content pane...
Pane pane = new Pane();
setContentPane(pane);
// Add some sort of input to let this app be closable..
pane.addMouseMotionListener(
new MouseMotionListener() {
public void mouseMoved(MouseEvent eM) {
// Programatically send a closing event.
JRandomBouncingImage.this
.dispatchEvent(
new WindowEvent(
JRandomBouncingImage.this,
/*
Apparently JPanel us can't be the source,
it has to be a window. Therefore
"window tells itself to close".
*/
WindowEvent.WINDOW_CLOSING
)
);
// Also, stow away ourselves so that a single
// sweep of the mouse doesn't create a dozen events.
// Which would be a huge pain if a close listener
// were to open a dialog window.
JRandomBouncingImage.this.setVisible(false);
}
public void mouseDragged(MouseEvent eM) { }
}
);
// Now, (we may) go fullscreen.
Toolkit toolkit = Toolkit.getDefaultToolkit();
if (toolkit.isFrameStateSupported(JFrame.MAXIMIZED_BOTH)) {
setExtendedState(JFrame.MAXIMIZED_BOTH);
}
else {
setSize(toolkit.getScreenSize());
}
setUndecorated(true);
// Load pixmap.
URL imageURL = getClass().getResource("./image.png");
if (imageURL == null) {
// File not found. What to do?
}
image = toolkit.createImage(imageURL);
// Set colours.
colours = new Color[] {
new Color(255, 192, 192),
new Color(255, 192, 255),
new Color(255, 255, 192),
new Color(192, 255, 192),
new Color(192, 255, 255),
new Color(192, 192, 255)
};
// Now start a timer.
Timer timer = new Timer(3000, pane);
timer.start();
}
// Main \\ // \\ // \\ // \\ // \\
public static void main(String... args) {
JRandomBouncingImage instance = new JRandomBouncingImage();
instance.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
instance.setVisible(true);
}
}