-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvas.java
More file actions
executable file
·428 lines (335 loc) · 11.1 KB
/
Copy pathCanvas.java
File metadata and controls
executable file
·428 lines (335 loc) · 11.1 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.lang.Thread;
/*
* Draws the pixels stored in the BufferedImage variable image in chunks.
* Yields thread control for visual updates between chunks.
* Selects a 3.5:2 rectangle when clicked on
*
* @author Liz Matthews
*
*/
public class Canvas extends JPanel implements MouseListener, MouseMotionListener {
// Variables
private boolean doneRendering;
private int renderX, renderY;
private int width, height;
private Point posStart;
private Point posEnd;
private Rectangle drawRect;
private BufferedImage image;
private Graphics2D gImg;
private double scale;
private setCalculator setC;
public static String setName = "Mandelbrot Set";
private double newLim;
private Rainbow r = Rainbow.getInstance((int)newLim);
// Final variables
final private Color colorSelect = new Color(0, 200, 200);
final private int chunkSize = 50;
// SetGradient
public void setGradient(String gradientName){
r.setGradient(gradientName);
}
//Increase Limit
public void increaseLimit(){
newLim = newLim *2;
r.setLimit((int)newLim);
setC.setLimit((int)newLim);
}
//Decrease Limit
public void decreaseLimit(){
if (newLim > 32){
newLim = newLim / 2;
}
else{
newLim = 32;
}
r.setLimit((int)newLim);
setC.setLimit((int)newLim);
}
//Reset Limit
public void resetLimit(){
newLim = 32;
r.setLimit((int)newLim);
setC.setLimit((int)newLim);
}
/*
* Scaled constructor for the canvas. Sets the scale to the parameter passed in.
* @author Liz Matthews
* @param scale how much to scale up the canvas from the default size of 350 by 200
*
*/
public Canvas(double scale) {
super();
this.scale = scale;
setC = new setCalculator();
newLim = setC.limit;
setup();
setupCanvas();
// Start the first render
resetRender();
}
/*
* Method to set up certain variables. Kept separate from the constructor so that setupCanvas and resetRender can be used elsewhere.
* @author Liz Matthews
*
*/
private void setup() {
// Initial state of variables
doneRendering = false;
renderX = 0;
renderY = 0;
drawRect = null;
// Listen for mouse movement or input
addMouseListener(this);
addMouseMotionListener(this);
}
/*
* Method to create the images for the canvas
* @author Liz Matthews
*
*/
public void setupCanvas() {
// Solid dimensions
double rectRatio;
width = (int)(350 * scale);
if(setName == "Julia Set"){
rectRatio = 1.0;
}
else{
rectRatio = (2/3.5);
}
height = (int)(width * (rectRatio));
// Image which is drawn upon
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
gImg = image.createGraphics();
}
/*
* Overridden paintComponent to draw the BufferedImage variable to the panel
* @author Liz Matthews
* @param g Graphics variable linked to this panel
*
*/
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// Draw the image so far
g.drawImage(image, 0, 0, width, height, 0, 0, width, height, null);
// Draw drag rectangle if it is there
if (drawRect != null) {
g.setColor(colorSelect);
g.drawRect((int)drawRect.getX(), (int)drawRect.getY(),
(int)drawRect.getWidth(), (int)drawRect.getHeight());
}
}
// Methods needed for mouse listeners but not needed to implement
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
/*
* Method to detect click on the canvas. Sets up a position start and end and invokes {@link #updateRectangle()} to update the drag rectangle dimensions.
* @author Liz Matthews
* @param e Mouse event that occured
*
*/
@Override
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
posStart = new Point(e.getX(), e.getY());
posEnd = new Point(e.getX(), e.getY());
updateRectangle();
}
}
/*
* Method to detect the mouse button is no longer held down. Frees up the drag variables and invokes {@link #resetRender()}.
* @author Liz Matthews
* @param e Mouse event that occured
*
*/
@Override
public void mouseReleased(MouseEvent e) {
posEnd.setLocation(e.getX(), e.getY());
updateRectangle();
// Resize the viewing area here
double minX = drawRect.getX();
double minY = drawRect.getY();
double maxX = drawRect.getX() + drawRect.getWidth();
double maxY = drawRect.getY() + drawRect.getHeight();
double minXPercent = ((double) minX) / width;
double minYPercent = ((double) minY) / height;
double maxXPercent = ((double) maxX) / width;
double maxYPercent = ((double) maxY) / height;
setC.dragZoom(minXPercent, minYPercent, maxXPercent, maxYPercent );
// Free up the draw variables
drawRect = null;
posStart = null;
posEnd = null;
// Start the drawing process over again if we're not already rendering.
if (doneRendering) {
resetRender();
}
}
/*
* Method to detect mouse movement on the canvas. Invokes {@link #updateRectangle()}.
* @author Liz Matthews
* @param e Mouse event that occured
*
*/
@Override
public void mouseDragged(MouseEvent e) {
if (drawRect != null) {
posEnd.setLocation(e.getX(), e.getY());
updateRectangle();
}
}
/*
* Method which updates the drag rectangle. Maintains a ratio of 3.5:2.
* @author Liz Matthews
*
*/
public void updateRectangle() {
int distX, distY;
// If we don't have a drag rectangle already, make one
if (drawRect == null) {
drawRect = new Rectangle(0, 0, 0, 0);
}
int width = (int)Math.abs(posEnd.getX() - posStart.getX());
int height = (int)Math.abs(posEnd.getY() - posStart.getY());
int left = (int)Math.min(posStart.getX(), posEnd.getX());
int top = (int)Math.min(posStart.getY(), posEnd.getY());
// Calculate Y-value based on x and ratio
double rectRatio;
distX = Math.abs(width);
if(setName=="Julia Set"){
rectRatio=1.0;
}
else{
rectRatio = (2/3.5);
}
distY = (int)(distX * (rectRatio));
// Set up rectangle to the correct four corners
drawRect.setLocation(left, top);
drawRect.setSize(distX,
distY);
// Let paintComponent handle this later
repaint();
}
/*
* Method which resets the chunk rendering. Clears out the canvas with black before invoking {@link #renderAll()}.
* @author Liz Matthews
*
*/
public void resetRender() {
// Start the rendering over again, set current x,y render chunk to 0
renderX = 0;
renderY = 0;
doneRendering = false;
// Clear out whatever is on the image
gImg.setPaint(Color.BLACK);
gImg.fillRect(0, 0, width , height );
// Start up the render
renderAll();
// Re-draw the panel
repaint();
}
public void setSetName(String setName){
this.setName = setName;
setC.changeSet(setName);
}
/*
* Method which renders chunks of an image at a time. Yields control of the thread for visualization of each chunk.
* @author Liz Matthews
*
*/
public void renderAll() {
// Continue until the entire image is done
while (!doneRendering) {
try {
// relinquish control
Thread.yield();
}
// Catch anything that goes wrong
catch (Exception e) {
System.out.println(e);
}
// Render next chunk
render();
}
}
/*
* Renders the next chunk.
* @author Liz Matthews
*
*/
private void render() {
// Variables
Color color;
// If we're not done with the entire image...
if (!doneRendering) {
r.createGradient((int)newLim);
// Iterate over each pixel in the render chunk
//need na if else here to return black or put it in rainbow if it hiss the limit
for(int x = renderX; x < renderX + chunkSize; x++) {
for(int y = renderY; y < renderY + chunkSize; y++) {
// Get the mandelbrot limit for that x/y
// ???
double xPercent = ((double)x)/width;
double yPercent = ((double)y)/height;
//c = the mandelbrot getT value
/* int newT = (int)setC.spillTheT(xPercent,yPercent);
int c = (int)(newT / newLim * 255);
if (newT >= newLim){
color = new Color(0,0,0);
}
else{
color = new Color(c,c,c);
}
*/
int t = setC.spillTheT(xPercent,yPercent);
color = r.getColor(t);
// Set the pixel in the image to the appropriate color
image.setRGB(x, y, color.getRGB());
}
}
// Move to next chunk
renderX += chunkSize;
if (renderX >= width ) {
renderX = 0;
renderY += chunkSize;
}
// If our next y-coordinate is off the end of the image then the entire image is rendered
if (renderY >= height ) {
doneRendering = true;
renderX = 0;
renderY = 0;
}
}
// Paint NOW to force the chunk visualization
paintImmediately(0, 0, width, height);
}
public BufferedImage getIMG(){
return image;
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
}