-
Notifications
You must be signed in to change notification settings - Fork 0
/
TLabel.java
639 lines (509 loc) · 18.4 KB
/
TLabel.java
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelListener;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import javax.swing.*;
public class TLabel{
private static final long serialVersionUID = 1L;
public JLabel draw;
// pre-defined colors
public static final Color BLACK = Color.BLACK;
public static final Color BLUE = Color.BLUE;
public static final Color CYAN = Color.CYAN;
public static final Color DARK_GRAY = Color.DARK_GRAY;
public static final Color GRAY = Color.GRAY;
public static final Color GREEN = Color.GREEN;
public static final Color LIGHT_GRAY = Color.LIGHT_GRAY;
public static final Color MAGENTA = Color.MAGENTA;
public static final Color ORANGE = Color.ORANGE;
public static final Color PINK = Color.PINK;
public static final Color RED = Color.RED;
public static final Color WHITE = Color.WHITE;
public static final Color YELLOW = Color.YELLOW;
public static final Color NICEGREEN = new Color(0,153,0);
// default colors
public static final Color DEFAULT_PEN_COLOR = BLACK;
public static final Color DEFAULT_CLEAR_COLOR = WHITE;
// current pen color
private static Color penColor;
// default canvas size is SIZE-by-SIZE
static final int SIZE = 512;
public int width = SIZE;
private int height = SIZE;
// default pen radius
private static final double DEFAULT_PEN_RADIUS = 0.002;
// current pen radius
private static double penRadius;
// boundary of drawing canvas, 0% border
public double BORDER = 0.00;
private static final double DEFAULT_XMIN = 0.0;
private static final double DEFAULT_XMAX = 1.0;
private static final double DEFAULT_YMIN = 0.0;
private static final double DEFAULT_YMAX = 1.0;
public double xmin, ymin, xmax, ymax;
// default font
private final Font DEFAULT_FONT = new Font("Serif", Font.PLAIN, 16);
// current font
private static Font font;
// double buffered graphics
private BufferedImage offscreenImage, onscreenImage;
protected Graphics2D offscreen, onscreen;
// change the user coordinate system
public void setXscale() { setXscale(DEFAULT_XMIN, DEFAULT_XMAX); }
public void setYscale() { setYscale(DEFAULT_YMIN, DEFAULT_YMAX); }
public void setXscale(double min, double max) {
double size = max - min;
xmin = min - BORDER * size;
xmax = max + BORDER * size;
}
public void setYscale(double min, double max) {
double size = max - min;
ymin = min - BORDER * size;
ymax = max + BORDER * size;
}
// helper functions that scale from user coordinates to screen coordinates and back
protected double scaleX (double x) { return width * (x - xmin) / (xmax - xmin); }
protected double scaleY (double y) { return height * (ymax - y) / (ymax - ymin); }
public double factorX(double w) { return w * width / Math.abs(xmax - xmin); }
public double factorY(double h) { return h * height / Math.abs(ymax - ymin); }
public double userX (double x) { return xmin + x * (xmax - xmin) / width; }
public double userY (double y) { return ymax - y * (ymax - ymin) / height; }
//create a frame, insert self in frame, then show self
public void showInFrame() {
JFrame j = new JFrame();
j.setTitle("Configuration");
j.setContentPane(this.draw);
j.setVisible(true);
j.pack();
show();
}
// clear the screen with given color
public void clear() { clear(DEFAULT_CLEAR_COLOR); }
public void clear(Color color) {
offscreen.setColor(color);
offscreen.fillRect(0, 0, width, height);
offscreen.setColor(penColor);
}
public void clear(double x1, double x2, double y1, double y2)
{ clear(DEFAULT_CLEAR_COLOR, x1, x2, y1, y2); }
public void clear(Color color, double x1, double x2, double y1, double y2) {
int ix1, ix2, iy1, iy2;
ix1 = (int) scaleX(x1);
ix2 = (int) scaleX(x2);
iy1 = (int) scaleY(y1);
iy2 = (int) scaleY(y2);
offscreen.setColor(color);
offscreen.fillRect(ix1, iy1, ix2, iy2);
offscreen.setColor(penColor);
//show();
}
// set the pen size
public void setPenRadius() { setPenRadius(DEFAULT_PEN_RADIUS); }
public void setPenRadius(double r) {
penRadius = r * SIZE;
BasicStroke stroke = new BasicStroke((float) penRadius);
offscreen.setStroke(stroke);
}
// set the pen color
public void setPenColor() { setPenColor(DEFAULT_PEN_COLOR); }
public void setPenColor(Color color) {
penColor = color;
offscreen.setColor(penColor);
}
// write the given string in the current font
public void setFont() { setFont(DEFAULT_FONT); }
public void setFont(Font f) {
Toolkit toolkit = java . awt . Toolkit . getDefaultToolkit ();
double x = toolkit.getScreenSize().getWidth();
double y = toolkit.getScreenSize().getHeight();
double xscale = x/1400.0;
double yscale = y/1050.0;
double scale = Math.sqrt((xscale*xscale+yscale*yscale)/2);
font = f.deriveFont((float) (f.getSize()*scale));
}
public TLabel(int w, int h){
width = w;
height = h;
offscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
onscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
offscreen = offscreenImage.createGraphics();
onscreen = onscreenImage.createGraphics();
setXscale();
setYscale();
offscreen.setColor(DEFAULT_CLEAR_COLOR);
offscreen.fillRect(0, 0, width, height);
setPenColor();
setPenRadius();
setFont();
clear();
// add antialiasing
RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
offscreen.addRenderingHints(hints);
// frame stuff
ImageIcon icon = new ImageIcon(onscreenImage);
draw = new JLabel(icon);
}
public void add(Container frame, String spot){
frame.add(draw, spot);
}
public void addML(MouseListener frame){
draw.addMouseListener(frame);
}
public void addMML(MouseMotionListener frame){
draw.addMouseMotionListener(frame);
}
public void addKL(KeyListener frame){
draw.addKeyListener(frame);
}
public void addMWL(MouseWheelListener frame) {
draw.addMouseWheelListener(frame);
}
public void remML(MouseListener frame){
draw.removeMouseListener(frame);
}
public void remMML(MouseMotionListener frame){
draw.removeMouseMotionListener(frame);
}
public void remKL(KeyListener frame){
draw.removeKeyListener(frame);
}
public void remMWL(MouseWheelListener frame){
draw.removeMouseWheelListener(frame);
}
// draw a line from (x0, y0) to (x1, y1)
public void line(double x0, double y0, double x1, double y1) {
// System.out.println("drawing a line from " + new Point(x0, y0).toString()+ " to " + new Point(x1,y1).toString());
offscreen.draw(new Line2D.Double(scaleX(x0), scaleY(y0), scaleX(x1), scaleY(y1)));
}
// draw one pixel at (x, y)
public void pixel(double x, double y) {
offscreen.fillRect((int) Math.round(scaleX(x)), (int) Math.round(scaleY(y)), 1, 1);
}
// draw one pixel at (x, y)
public void pixelP(double x, double y) {
offscreen.fillRect((int) Math.round(x), (int) Math.round(y), 1, 1);
}
// draw one pixel at (x, y)
public void pixelP(double x, double y, Color c) {
setPenColor(c);
offscreen.fillRect((int) Math.round(x), (int) Math.round(y), 1, 1);
}
// draw point at (x, y)
public void point(double x, double y) {
double xs = scaleX(x);
double ys = scaleY(y);
double r = penRadius;
// double ws = factorX(2*r);
// double hs = factorY(2*r);
// if (ws <= 1 && hs <= 1) pixel(x, y);
if (r <= 1) pixel(x, y);
else offscreen.fill(new Ellipse2D.Double(xs - r/2, ys - r/2, r, r));
}
public void arc(double x, double y, double r, double startAngle, double arcRange) {
double xs = scaleX(x);
double ys = scaleY(y);
double ws = factorX(2*r);
double hs = factorY(2*r);
if (ws <= 1 && hs <= 1) pixel(x, y);
else offscreen.draw(new Arc2D.Double(xs - ws/2, ys - hs/2, ws, hs,startAngle, arcRange, Arc2D.OPEN));
}
// draw circle of radius r, centered on (x, y); degenerate to pixel if small
public void circle(double x, double y, double r) {
double xs = scaleX(x);
double ys = scaleY(y);
double ws = factorX(2*r);
double hs = factorY(2*r);
if (ws <= 1 && hs <= 1) pixel(x, y);
else offscreen.draw(new Ellipse2D.Double(xs - ws/2, ys - hs/2, ws, hs));
}
public void circleP(double x, double y, double r, Color col) {
setPenColor(col);
circleP(x,y,r);
}
public void circleP(double x, double y, double r) {
double ws = 2*r;
double hs = 2*r;
double xs = scaleX(x);
double ys = scaleY(y);
if (ws <= 1 && hs <= 1) pixel(x, y);
else offscreen.draw(new Ellipse2D.Double(xs - ws/2, ys - hs/2, ws, hs));
}
public void circle(double x, double y, double r, Color color) {
setPenColor(color);
circle(x,y,r);
}
// draw filled circle of radius r, centered on (x, y); degenerate to pixel if small
public void filledCircle(double x, double y, double r) {
double xs = scaleX(x);
double ys = scaleY(y);
double ws = factorX(2*r);
double hs = factorY(2*r);
if (ws <= 1 && hs <= 1) pixel(x, y);
else offscreen.fill(new Ellipse2D.Double(xs - ws/2, ys - hs/2, ws, hs));
}
public void filledCircleP(double x, double y, double r) {
double ws = 2*r;
double hs = 2*r;
if (ws <= 1 && hs <= 1) pixel(x, y);
else offscreen.fill(new Ellipse2D.Double(x - ws/2, y - hs/2, ws, hs));
}
// draw squared of side length 2r, centered on (x, y); degenerate to pixel if small
public void square(double x, double y, double r) {
// screen coordinates
double xs = scaleX(x);
double ys = scaleY(y);
double ws = factorX(2*r);
double hs = factorY(2*r);
if (ws <= 1 && hs <= 1) pixel(x, y);
else offscreen.draw(new Rectangle2D.Double(xs - ws/2, ys - hs/2, ws, hs));
}
// draw squared of side length 2r, centered on (x, y); degenerate to pixel if small
public void filledSquare(double x, double y, double r) {
// screen coordinates
double xs = scaleX(x);
double ys = scaleY(y);
double ws = factorX(2*r);
double hs = factorY(2*r);
if (ws <= 1 && hs <= 1) pixel(x, y);
else offscreen.fill(new Rectangle2D.Double(xs - ws/2, ys - hs/2, ws, hs));
}
//Draw an arrow of the appropriate scale, color, position
public void Arrow(double x, double y, double w, double h, double Scale, Color Color) {
rectangle(x, y, w, h, Color);
double[] xarray = {x+w/2-w/10+Scale*1/10*Math.sqrt(h*h*25+w*w)*Math.sqrt(3)/3,x+w/2-w/10,x+w/2-w/10};
double[] yarray = {y,y+Scale*1/10*Math.sqrt(h*h*25+w*w)*Math.sqrt(2)/2,y-Scale*1/10*Math.sqrt(h*h*25+w*w)*Math.sqrt(2)/2};
setPenColor(Color);
filledPolygon(xarray, yarray);
setPenColor();
}
// draw a polygon with the given (x[i], y[i]) coordinates
public void polygon(double[] x, double[] y) {
int N = x.length;
GeneralPath path = new GeneralPath();
path.moveTo((float) scaleX(x[0]), (float) scaleY(y[0]));
for (int i = 0; i < N; i++)
path.lineTo((float) scaleX(x[i]), (float) scaleY(y[i]));
path.closePath();
offscreen.draw(path);
}
// draw a polygon with the given (x[i], y[i]) coordinates
public void polygonP(double[] x, double[] y) {
int N = x.length;
GeneralPath path = new GeneralPath();
path.moveTo((float) x[0], (float) y[0]);
for (int i = 0; i < N; i++)
path.lineTo((float) x[i], (float) y[i]);
path.closePath();
offscreen.draw(path);
}
// draw a filled polygon with the given (x[i], y[i]) coordinates
public void filledPolygon(double[] x, double[] y) {
int N = x.length;
GeneralPath path = new GeneralPath();
path.moveTo((float) scaleX(x[0]), (float) scaleY(y[0]));
for (int i = 0; i < N; i++)
path.lineTo((float) scaleX(x[i]), (float) scaleY(y[i]));
path.closePath();
offscreen.fill(path);
}
// draw a filled polygon with the given (x[i], y[i]) coordinates
public void filledPolygonP(double[] x, double[] y) {
int N = x.length;
GeneralPath path = new GeneralPath();
path.moveTo((float) x[0], (float) y[0]);
for (int i = 0; i < N; i++)
path.lineTo((float) x[i], (float) y[i]);
path.closePath();
offscreen.fill(path);
}
//Draw rectangle at the given coordinates
public void rectangle(double x, double y, double w, double h) {
double[] xarray = {x-w/2,x-w/2,x+w/2,x+w/2};
double[] yarray = {y-h/2,y+h/2,y+h/2,y-h/2};
polygon(xarray,yarray);
}
public void rectangleLL(double x, double y, double w, double h) {
double[] xarray = {x,x,x+w,x+w};
double[] yarray = {y,y+h,y+h,y};
polygon(xarray,yarray);
}
public void rectangleP(double x, double y, double w, double h) {
double[] xarray = {x,x,x+w,x+w};
double[] yarray = {y,y+h,y+h,y};
polygonP(xarray,yarray);
}
public void rectangle(double x, double y, double w, double h, Color c) {
double[] xarray = {x-w/2,x-w/2,x+w/2,x+w/2};
double[] yarray = {y-h/2,y+h/2,y+h/2,y-h/2};
setPenColor(c);
filledPolygon(xarray,yarray);
setPenColor(DEFAULT_PEN_COLOR);
}
public void rectangleC(double x, double y, double w, double h, Color c) {
double[] xarray = {x,x,x+w,x+w};
double[] yarray = {y,y+h,y+h,y};
setPenColor(c);
filledPolygon(xarray,yarray);
setPenColor(DEFAULT_PEN_COLOR);
}
public void filledRectangleP(double x, double y, double w, double h, Color c) {
double[] xarray = {x,x,x+w,x+w};
double[] yarray = {y,y+h,y+h,y};
setPenColor(c);
filledPolygonP(xarray,yarray);
setPenColor(DEFAULT_PEN_COLOR);
}
public void filledRectangleLL(double x, double y, double w, double h, Color c) {
double[] xarray = {x,x,x+w,x+w};
double[] yarray = {y,y+h,y+h,y};
setPenColor(c);
filledPolygon(xarray,yarray);
setPenColor(DEFAULT_PEN_COLOR);
}
public void rectangle(double x, double y, double w, double h, Color c,boolean Border, Color BorderColor) {
double[] xarray = {x-w/2,x-w/2,x+w/2,x+w/2};
double[] yarray = {y-h/2,y+h/2,y+h/2,y-h/2};
if (c!=null) {
setPenColor(c);
filledPolygon(xarray,yarray);
}
setPenColor(BorderColor);
if (Border) polygon(xarray, yarray);
setPenColor();
}
// draw picture (gif, jpg, or png) upperLeft on (x, y), rescaled to w-by-h
public void image(double x, double y, Image image, double w, double h) {
double xs = scaleX(x);
double ys = scaleY(y);
double ws = factorX(w);
double hs = factorY(h);
if (ws <= 1 && hs <= 1) pixel(x, y);
else {
offscreen.drawImage(image, (int) Math.round(xs),
(int) Math.round(ys),
(int) Math.round(ws),
(int) Math.round(hs), null);
}
}
// draw picture (gif, jpg, or png) upperLeft on (x, y), rescaled to w-by-h
public void imageP(double x, double y, Image image) {
//if (ws <= 1 && hs <= 1) pixel(x, y);
offscreen.drawImage(image, (int) Math.round(x),(int) Math.round(y), null);
}
//Invert an image
public BufferedImage invert(Image image) {
BufferedImage b1 = new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB);
Graphics bg = b1.getGraphics();
bg.drawImage(image, 0, 0, null);
bg.dispose();
BufferedImage b2 = new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB);
DataBuffer db1 = b1.getRaster().getDataBuffer();
DataBuffer db2 = b2.getRaster().getDataBuffer();
for (int i = db1.getSize() - 1, j = 0; i >= 0; --i, j++) {
db2.setElem(j, db1.getElem(i));
}
for (int i = db1.getSize() - 1, j = 0; i >= 0; --i, j++) {
db1.setElem(i, db1.getElem(i));
}
return b2;
}
// write the given text string in the current font, center on (x, y)
public void text(double x, double y, String s) {
offscreen.setFont(font);
FontMetrics metrics = offscreen.getFontMetrics();
double xs = scaleX(x);
double ys = scaleY(y);
int ws = metrics.stringWidth(s);
int hs = metrics.getDescent();
offscreen.drawString(s, (float) (xs - ws/2.0), (float) (ys + hs));
}
public void textTop(double x, double y, String s) {
offscreen.setFont(font);
FontMetrics metrics = offscreen.getFontMetrics();
double xs = scaleX(x);
double ys = scaleY(y);
int ws = metrics.stringWidth(s);
int hs = metrics.getDescent();
offscreen.drawString(s, (float) (xs - ws/2.0), (float) (ys + hs*3));
}
public void textLeft(double x, double y, String s,Color c) {
setPenColor(c);
offscreen.setFont(font);
FontMetrics metrics = offscreen.getFontMetrics();
double xs = scaleX(x);
double ys = scaleY(y);
//int ws = metrics.stringWidth(s);
int hs = metrics.getDescent();
offscreen.drawString(s, (float) (xs), (float) (ys + hs));
setPenColor();
}
//Draw text at the appropriate point and color
public void text(double x, double y, String s,Color c) {
setPenColor(c);
offscreen.setFont(font);
FontMetrics metrics = offscreen.getFontMetrics();
double xs = scaleX(x);
double ys = scaleY(y);
int ws = metrics.stringWidth(s);
int hs = metrics.getDescent();
offscreen.drawString(s, (float) (xs - ws/2.0), (float) (ys + hs));
setPenColor();
}
// write the given text string in the current font, center on (x, y) sized to w, h
public void text(double x, double y, String s, double w, double h) {
offscreen.setFont(font);
//FontMetrics metrics = offscreen.getFontMetrics();
double xs = scaleX(x);
double ys = scaleY(y);
double ws = factorX(w);
double hs = factorY(h);
offscreen.drawString(s, (float) (xs - ws/2.0), (float) (ys + hs));
}
public void absText(String s, int x, int y) {
offscreen.drawString(s, (float) (x), (float) (y));
}
public void textinvert(double x, double y, String s) {
offscreen.setFont(font);
FontMetrics metrics = offscreen.getFontMetrics();
double xs = scaleX(x);
double ys = scaleY(y);
int ws = metrics.stringWidth(s);
int hs = metrics.getDescent();
BufferedImage bimage = new BufferedImage(ws, hs,BufferedImage.TYPE_INT_ARGB);
Graphics2D bimagegraphics = bimage.createGraphics();
bimagegraphics.drawString(s, (float) (xs - ws/2.0), (float) (ys + hs));
BufferedImage bimage2 = invert(bimage);
offscreen.drawImage(bimage2, (int) Math.round(xs - ws/2.0),
(int) Math.round(ys + hs), null);
}
// view on-screen, creating new frame if necessary
public void show() {
onscreen.drawImage(offscreenImage, 0, 0, null);
try{
draw.repaint();
//frame.paint(frame.getGraphics());
}
catch(NullPointerException e){
System.out.println("Null Pointer Exception in showatonce");
}
}
}