-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircles.java
More file actions
551 lines (492 loc) · 12.3 KB
/
circles.java
File metadata and controls
551 lines (492 loc) · 12.3 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
package circles;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.*;
public class circles extends JFrame implements MouseListener,
MouseMotionListener, MouseWheelListener, ActionListener
{
/**
* This is a small program(not really small, for ones like me:_) generating a picture
that combined with circles.
* Actually, you have to make it blur to make it clear.
*/
private static final long serialVersionUID = 1L;
String status_text = "default message";
int windowsize = 640;
int unit_len = 16;
int mws;
int default_cir_size = 6;
int full_cir_size = 5;
int maxt;
Image iBuffer;
Graphics gBuffer;
private boolean repaint_full_flag;
boolean dragging;
int []cir_size;
int []cir_x;
int []cir_y;
int ctmp = 0;
int mx, my;
public enum pic_type {Rect, Hexagon}
pic_type pt = pic_type.Rect;
//varibles
void resize()
{
this.setSize(windowsize, windowsize+30);
}
void showStatus(String str)
{
status_text = str;
}
public static void main(String[] args)
{
new circles();
}
void init_cir_pos()
{
switch(pt)
{
case Rect:
maxt = square(windowsize/unit_len+10);
cir_size = new int[maxt];
cir_x = new int[maxt];
cir_y = new int[maxt];
for(int i = 0; i < maxt; i++)
{
cir_size[i] = default_cir_size;
cir_x[i] = -10000;
cir_y[i] = -10000;
}
ctmp = 0;
for(int i = 0; i < windowsize/2; i+=unit_len)
{
for(int j = 0; j < windowsize/2; j+=unit_len)
{
cir_x[ctmp] = i;
cir_y[ctmp] = j;
ctmp++;
cir_x[ctmp] = -i;
cir_y[ctmp] = j;
ctmp++;
cir_x[ctmp] = i;
cir_y[ctmp] = -j;
ctmp++;
cir_x[ctmp] = -i;
cir_y[ctmp] = -j;
ctmp++;
}
}
break;
case Hexagon:
maxt = square(windowsize/unit_len*3);
cir_size = new int[maxt];
cir_x = new int[maxt];
cir_y = new int[maxt];
for(int i = 0; i < maxt; i++)
{
cir_size[i] = default_cir_size;
cir_x[i] = -10000;
cir_y[i] = -10000;
}
ctmp = 0;
for(int i = 0; i < windowsize/2*3; i+=unit_len)
{
for(int j = 0; j < windowsize/2*3; j+=unit_len)
{
cir_x[ctmp] = i-j/2;
cir_y[ctmp] = (int)(j*Math.sqrt(3)/2);
ctmp++;
cir_x[ctmp] = -i+j/2;
cir_y[ctmp] = -(int)(j*Math.sqrt(3)/2);
ctmp++;
cir_x[ctmp] = j-i/2;
cir_y[ctmp] = -(int)(i*Math.sqrt(3)/2);
ctmp++;
cir_x[ctmp] = -j+i/2;
cir_y[ctmp] = (int)(i*Math.sqrt(3)/2);
ctmp++;
}
}
break;
default:
System.out.println("Missing picture type:"+pt.name());
break;
}
}
circles()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.resize();
this.setBackground(new Color(17,18,125));
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.addMouseWheelListener(this);
MenuBar mbar = new MenuBar();
this.setMenuBar(mbar);
Menu file = new Menu("File");
MenuItem item1_1;
file.add(item1_1 = new MenuItem("Save to Clipboard"));
item1_1.addActionListener(this);
mbar.add(file);
Menu window = new Menu("Window");
MenuItem item2_1, item2_2, item2_3, item2_4;
window.add(item2_1 = new MenuItem("Clear"));
window.add(item2_2 = new MenuItem("Display Shortcut"));
window.add(item2_3 = new MenuItem("Use Default setting"));
window.add(item2_4 = new MenuItem("Professional..."));
item2_1.addActionListener(this);
item2_2.addActionListener(this);
item2_3.addActionListener(this);
item2_4.addActionListener(this);
mbar.add(window);
Menu picture = new Menu("Picture");
for(pic_type pt : pic_type.values())
{
MenuItem tmp;
picture.add(tmp = new MenuItem(pt.name()));
tmp.addActionListener(this);
}
mbar.add(picture);
Menu help = new Menu("Help");
MenuItem item3_1, item3_2;
help.add(item3_1 = new MenuItem("Help..."));
help.add(item3_2 = new MenuItem("About..."));
item3_1.addActionListener(this);
item3_2.addActionListener(this);
mbar.add(help);
//menus
this.init_cir_pos();
//initalize circles position
repaint_full_flag = true;
mx = my = -10;
mws = 10; //default mouse circle size
this.setVisible(true);
}
void draw()
{
if(repaint_full_flag)
{
iBuffer=createImage(windowsize,windowsize);
gBuffer=iBuffer.getGraphics();
repaint_full_flag = false;
}
gBuffer.setColor(getBackground());
gBuffer.fillRect(0,0,windowsize,windowsize);
gBuffer.setColor(Color.lightGray);
//gBuffer.drawLine(1, 3, 134, 436);
for(int i = 0; i < ctmp; i++)
{
gBuffer.fillOval(cir_x[i]+windowsize/2-cir_size[i],
cir_y[i]+windowsize/2-cir_size[i], 2*cir_size[i], 2*cir_size[i]);
}
}
public void paint(Graphics g)
{
if(repaint_full_flag)
{
draw();
repaint_full_flag = false;
}
g.drawImage(iBuffer, 0, 20, this);
//paint from buffer
g.setColor(Color.gray);
if(dragging)
{
g.fillOval(mx-mws, my-mws, mws*2, mws*2);
dragging = false;
}
else
g.drawOval(mx-mws, my-mws, mws*2, mws*2);
//showing status text
g.clearRect(0, windowsize+20, windowsize, 10);
g.setFont(new Font("dialog", 10, 10));
g.setColor(Color.white);
g.drawString(status_text, 10, this.getHeight()-10);
//repaint();
}
public void update(Graphics g)
{
paint(g);
}
@Override
public void mouseDragged(MouseEvent me)
{
mx = me.getX();
my = me.getY();
showStatus("Mouse at x:"+mx+"\t y:"+my);
if(me.getButton()==MouseEvent.BUTTON1)
engage(true);
else if(me.getButton()==MouseEvent.BUTTON3)
engage(false);
repaint();
}
public void mouseMoved(MouseEvent me)
{
mx = me.getX();
my = me.getY();
showStatus("Mouse at x:"+mx+"\t y:"+my);
repaint();
}
@Override
public void mouseClicked(MouseEvent me)
{
if(me.getButton()==MouseEvent.BUTTON1)
engage(true);
else if(me.getButton()==MouseEvent.BUTTON3)
engage(false);
repaint();
}
@Override
public void mouseEntered(MouseEvent arg0)
{
}
@Override
public void mouseExited(MouseEvent arg0)
{
//showStatus("Mouse is not in window");
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseWheelMoved(MouseWheelEvent mwe)
{
mws+=-mwe.getWheelRotation();
if(mws<2)
mws = 2;
if(mws>800)
mws = 800;
repaint();
}
private int square(int x)
{
return x*x;
}
void engage(boolean type)
{
dragging = true;
for(int i = 0; i < ctmp; i++)
{
if(square(cir_x[i]+windowsize/2-mx)+square(cir_y[i]+windowsize/2+20-my)<=square(mws))
{
System.out.println("Update circle no."+i+"\tat x:"+cir_x[i]+" \ty:"+cir_y[i]);
if(type == true)
cir_size[i] = full_cir_size;
if(type == false)
cir_size[i] = default_cir_size;
}
}
draw();
}
@SuppressWarnings("serial")
class shortcut extends JFrame
{
Image img;
shortcut(Image i)
{
img = i;
this.setSize(200, 220);
this.setVisible(true);
this.setTitle("Shortcut");
}
public void paint(Graphics g)
{
g.drawImage(img, 0, 20, 200, 200, this);
}
}
@SuppressWarnings("serial")
class help_window extends JFrame
{
help_window()
{
this.setSize(400,200);
setLayout(new FlowLayout(FlowLayout.CENTER));
this.setTitle("Help");
String val = "This is not much hard to try to use this program.\n"
+"You can paint whatever you want by dragging your mouse,\n"
+"\tor erase them using right clicks.\n"
+"And you can save the picture by screeshot software,\n"
+"\tor press File>Save to clipboard.\n\n"
+"Enjoy!";
TextArea text = new TextArea(val, 10, 40);
add(text);
text.setEditable(false);
this.setResizable(false);
this.setVisible(true);
}
}
@SuppressWarnings("serial")
class about_window extends JFrame
{
about_window()
{
this.setSize(200,100);
setLayout(new FlowLayout(FlowLayout.CENTER));
this.setTitle("About");
Label title = new Label(" ");
add(title);
add(new Label("Created by vitualize in 2016"));
add(new Label(" All rights reserved. "));
this.setResizable(false);
this.setVisible(true);
}
}
@SuppressWarnings("serial")
class settings extends JFrame implements ActionListener
{
TextField windowsizet = new TextField(4);
TextField unitt = new TextField(4);
TextField defaultt = new TextField(4);
TextField fullt = new TextField(4);
circles circ;
settings(circles cir)
{
circ = cir;
this.setSize(200,320);
this.setTitle("Settings");
setLayout(new FlowLayout(FlowLayout.LEFT));
Label windowsizep = new Label("Window size: \t", Label.RIGHT);
Label unitp = new Label("Unit length: \t", Label.RIGHT);
Label defaultp = new Label("Default length: \t", Label.RIGHT);
Label fullp = new Label("Full length: \t", Label.RIGHT);
windowsizet.setText(""+cir.windowsize);
unitt.setText(""+cir.unit_len);
defaultt.setText(""+cir.default_cir_size);
fullt.setText(""+cir.full_cir_size);
add(windowsizep);
add(windowsizet);
add(unitp);
add(unitt);
add(defaultp);
add(defaultt);
add(fullp);
add(fullt);
JButton j = new JButton("Apply");
add(j);
j.addActionListener(this);
windowsizet.addActionListener(cir);
unitt.addActionListener(cir);
defaultt.addActionListener(cir);
fullt.addActionListener(cir);
this.setResizable(false);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Apply"))
{
circ.windowsize = Integer.parseInt(windowsizet.getText());
circ.unit_len = Integer.parseInt(unitt.getText());
circ.default_cir_size = Integer.parseInt(defaultt.getText());
circ.full_cir_size = Integer.parseInt(fullt.getText());
circ.repaint_full_flag = true;
circ.resize();
circ.init_cir_pos();
circ.draw();
circ.repaint();
}
}
}
@Override
public void actionPerformed(ActionEvent ae)
{
String command = ae.getActionCommand();
if(command.equals("Save to Clipboard"))
{
circles.setImageClipboard(iBuffer);
this.showStatus("Saved to clipboard");
}
else if(command.equals("Clear"))
{
for(int i = 0; i < ctmp; i++)
cir_size[i] = default_cir_size;
draw();
this.showStatus("Window cleared");
}
else if(command.equals("Display Shortcut"))
{
new shortcut(iBuffer);
}
else if(command.equals("Help..."))
{
new help_window();
}
else if(command.equals("About..."))
{
new about_window();
}
else if(command.equals("Professional..."))
{
new settings(this);
}
else if(command.equals("Use Default setting"))
{
this.pt = pic_type.Rect;
this.windowsize = 640;
this.unit_len = 16;
this.default_cir_size = 6;
this.full_cir_size = 5;
this.repaint_full_flag = true;
this.resize();
this.init_cir_pos();
this.draw();
this.repaint();
}
else
{
for(pic_type pwt : pic_type.values())
{
if(pwt.name().equals(command))
{
pt = pwt;
this.repaint_full_flag = true;
this.resize();
this.init_cir_pos();
this.draw();
this.repaint();
this.showStatus("Picture type set to: "+pt);
}
}
}
repaint();
}
public static void setImageClipboard(Image image)
{
//this part is configring with saving to clipboard
ImageSelection imgSel = new ImageSelection(image);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);
}
public static class ImageSelection implements Transferable
{
private Image image;
public ImageSelection(Image image)
{
this.image = image;
}
public DataFlavor[] getTransferDataFlavors()
{
return new DataFlavor[]{DataFlavor.imageFlavor};
}
public boolean isDataFlavorSupported(DataFlavor flavor)
{
return DataFlavor.imageFlavor.equals(flavor);
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
{
if (!DataFlavor.imageFlavor.equals(flavor))
{
throw new UnsupportedFlavorException(flavor);
}
return image;
}
}
}