-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphicsBaseFrame.java
1738 lines (1424 loc) · 70.1 KB
/
GraphicsBaseFrame.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
GraphicsBaseFrame.java
Simplest possible frame and drawing tools / methods to use Graphics to draw and manipulate images
(Well, that was the intent. More could be scrapped away for simplicity)
IN ACTUALITY, this is purely designed to take an image, create a copy and filter that copy, and then
print the image next to the original to see how well the filter worked. In essence, this is a big test
file for filters written, and is not at all designed well, but it works
@author Peter Olson
@version 6/2/19
@see ImageSetter.java
@see Pixel.java
@see filter( ImageSetter image ) -- @@NOTE: Adjust this method for controlling what filters are used
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.image.BufferedImage;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.MediaTracker;
import java.awt.Graphics2D;
import java.awt.Dimension;
import javax.imageio.ImageIO;
import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.awt.Color;
import java.awt.AlphaComposite;
import java.awt.Rectangle;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.awt.Point;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.RenderingHints;
import java.awt.Point;
import javax.swing.BorderFactory;
import java.awt.Insets;
import java.awt.FlowLayout;
public class GraphicsBaseFrame extends JPanel {
//protected JFrame win;
private ImagePanel ip;
/*
Available files:
flags.jpg, fry_meme.jpg, heavy_breathing.jpg, player_right.png, boromir_meme.jpg, balloons.jpg
*/
public String fileName = "rose.jpeg"; //boromir_meme.jpg
public String secondFileName = "heavy_breathing.jpg"; //heavy_breathing.jpg
public String fileNameFiltered = ""; //fileName of filtered image --> set in saveImage( BufferedImage image )
public boolean filtered = false; //Used to tell if a filter is used on the image, so that the name can of the save file can be adjusted
public boolean latticeImage = false; //Used to tell if an image has multiple saves in a method or not -- used for naming conventions
public int imageWidth;
public int imageHeight;
public final int BUFFER = 20;
public int index;
public boolean save = true;
/*
//@@NOTE: Add this back for independent use
public static void main( String[] args ) {
GraphicsBaseFrame base = new GraphicsBaseFrame();
}
*/
/**
Create a JFrame and ImagePanel to display images.
Draw the original images, apply filters, and save the filtered image
@see ImagePanel private class
@see drawImage( String fileName )
@see filterNewImage( String fileName )
@see saveImage( BufferedImage image )
*/
public GraphicsBaseFrame() {
/* @@NOTE: DUAL-SETTINGS FOR THIS FILE -- THIS FILE CAN BE RUN INDEPENDENTLY, OR THROUGH A PROGRAM.
If run independently, uncomment the code for the JFrame below and for the
main method, as well as the win. calls, the code below, and the global instance of the JFrame window.
If running through a program, use this file as a JPanel and add it to your container. Additionally, this file should extend JPanel and all of the win. should be removed
*/
/*
@@NOTE: Add this back for independent use; also, the main method, and all the win.repaint() calls
win = new JFrame( "Graphics Drawing Space" );
win.setPreferredSize( new Dimension( 1500, 1000 ) );
ip = new ImagePanel( this );
win.add( ip, BorderLayout.CENTER );
win.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
win.pack();
win.setVisible( true );
*/ip = new ImagePanel( this ); // this is needed from the commented section above
add( ip );
setLocation( new Point(0,0) );
setSize( new Dimension( GameWindow.getWindowWidth(), GameWindow.getWindowHeight() ) );
//setPreferredSize( new Dimension( WIDTH, HEIGHT ) );
//setFocusable( true );
//requestFocus();
fileName = getTextImage( "Testing bruh", "Grobold", Font.PLAIN, 30, "Testing bruh image" );
drawImage( fileName ); //draws image to panel
//drawNewImage( fileName ); //functions draw on top of image instead of changing pixels
//filterNewImage( fileName, secondFileName ); //functions change pixels of actual image
/*
@NOTE if using drawNewImage and filterNewImage:
If drawing over an image and then filtering, keep in mind that the image saved is saved to the secondFileName
file, so if drawing over an image and then filtering, the filename file should be the name of the drawn over
file, which would in this case be 'secondFileName', so you would want to pass in secondFileName as the first
parameter, not fileName
*/
}
public GraphicsBaseFrame( int width, int height ) {
ip = new ImagePanel( this ); // this is needed from the commented section above
ip.setLayout( new FlowLayout( FlowLayout.CENTER ) );
add( ip, FlowLayout.CENTER );
setLocation( new Point(0,0) );
setSize( new Dimension( width, height ) );
setLayout( new FlowLayout( FlowLayout.CENTER ) );
setOpaque( true );
setBackground( Color.BLACK );
//setBorder( BorderFactory.createLineBorder( Color.black, 5 ) );
fileName = getTextImage( "The sound of your sister giggling alerts you to the fact\nthat there’s a world outside the dishes you’re"
+ " currently washing.\n\nJin-Ah looks up at you, a wide smile on her face.\nYou always wonder where she gets"
+ " the energy to bounce\naround after a long day of school.\nOr the joyfulness. You’re glad she’s here.", "Arial", Font.PLAIN, 45, "Testing bruh image" );
//@@DEBUG
/*
File tmpDir = new File( fileName );
boolean exists = tmpDir.exists();
SOPln( "Working Directory = " + System.getProperty( "user.dir" ) );
*/
BufferedImage img = null;
try {
img = ImageIO.read( new File( fileName ) );
} catch( IOException e ) {
System.out.println( "Error: " + e );
}
//SOPln( "img width: " + img.getWidth() + "height: " + img.getHeight() );
ip.setSize( new Dimension( img.getWidth(), img.getHeight() ) );
ip.setPreferredSize( new Dimension( img.getWidth(), img.getHeight() ) );
ip.setBackground( Color.BLACK );
drawImage( fileName ); //draws image to panel
//drawNewImage( fileName ); //functions draw on top of image instead of changing pixels
//filterNewImage( fileName, secondFileName ); //functions change pixels of actual image
/*
@NOTE if using drawNewImage and filterNewImage:
If drawing over an image and then filtering, keep in mind that the image saved is saved to the secondFileName
file, so if drawing over an image and then filtering, the filename file should be the name of the drawn over
file, which would in this case be 'secondFileName', so you would want to pass in secondFileName as the first
parameter, not fileName
*/
}
public GraphicsBaseFrame( int x, int y, int width, int height, String text, String fontName, int fontType, int fontSize, String imageName, int index ) {
this.index = index;
this.save = save;
ip = new ImagePanel( this ); // this is needed from the commented section above
ip.setLayout( null );
add( ip );
Insets insets = getInsets();
Dimension size = new Dimension( width, height );
ip.setBounds( 0, 0, size.width, size.height );
//@@DEBUG
//ip.setBorder( BorderFactory.createLineBorder( Color.YELLOW ) );
repaint();
fileName = getTextImage( text, fontName, fontType, fontSize, imageName );
BufferedImage img = null;
try {
img = ImageIO.read( new File( fileName ) );
} catch( IOException e ) {
System.out.println( "Error: " + e );
}
//@@DEBUG
//SOPln( "img width: " + img.getWidth() + "height: " + img.getHeight() );
imageWidth = img.getWidth();
imageHeight = img.getHeight();
Insets insets2 = getInsets();
Dimension size2 = new Dimension( imageWidth, imageHeight );
setBounds( x + insets2.left, y + insets2.top, size2.width, size2.height );
setLayout( null );
setOpaque( true );
ip.setOpaque( true );
setBackground( Color.BLACK );
ip.setBackground( Color.BLACK );
//@@DEBUG
//setBorder( BorderFactory.createLineBorder( Color.YELLOW ) );
repaint();
drawImage( fileName ); //draws image to panel
//drawNewImage( fileName ); //functions draw on top of image instead of changing pixels
//filterNewImage( fileName, secondFileName ); //functions change pixels of actual image
/*
@NOTE if using drawNewImage and filterNewImage:
If drawing over an image and then filtering, keep in mind that the image saved is saved to the secondFileName
file, so if drawing over an image and then filtering, the filename file should be the name of the drawn over
file, which would in this case be 'secondFileName', so you would want to pass in secondFileName as the first
parameter, not fileName
*/
}
public int getImageWidth() {
return imageWidth;
}
public int getImageHeight() {
return imageHeight;
}
/**
Controls what filters are applied to the image. Comment out or uncomment filter calls to use
@param image The ImageSetter object to filter (essentially, an image whose pixels are accessible for direct modification)
@param image2 The ImageSetter object that is used in conjunction with another image to create a filter
@see additionFilter( ImageSetter image )
@see bitwiseANDFilter( ImageSetter image )
@see flipHorizontalFilter( ImageSetter image )
@see flipVerticalFilter( ImageSetter image )
@see gaussianFilter( ImageSetter image )
@see grayFilter( ImageSetter image )
@see inverseFilter( ImageSetter image )
@see laplacianFilter( ImageSetter image )
@see rotateCounterClockwiseFilter( ImageSetter image )
@see rotateClockwiseFilter( ImageSetter image )
@see sharpenFilter( ImageSetter image );
*/
private void filter( ImageSetter image, ImageSetter image2 ) {
/* @@@@@@ UNCOMMENT TO USE @@@@@@@@ */
/* @@@@@@ NOTE: filtered boolean var used so that original file is not overwritten @@@@@ */
//additionFilter( image ); filtered = true;
//bitwiseANDFilter( image, image2 ); filtered = true;
//flipHorizontalFilter( image ); filtered = true;
//flipVerticalFilter( image ); filtered = true;
//gaussianFilter( image ); filtered = true;
//grayFilter( image ); filtered = true;
//inverseFilter( image ); filtered = true;
//laplacianFilter( image ); filtered = true;
//rotateCounterClockwiseFilter( image ); filtered = true; // @@NOTE: ONLY CAN BE USED ON SQUARE IMAGES (for now)
//rotateClockwiseFilter( image ); filtered = true; // @@NOTE: ONLY CAN BE USED ON SQUARE IMAGES (for now)
//sharpenFilter( image ); filtered = true;
//layerImage( image, image2, 100, 0 ); filtered = true;
/*@NOTE: second parameter is blurriness 'strength'. 9 = gaussian blur, 400 = strongly blurred
Also, strength should be a perfect square eg. 9, 25, 36, 49, 64, 100, 225, 400
*/
blurFilter( image, 100.0f ); filtered = true;
/*win.*/repaint();
}
/**
Method that controls what drawing methods are called and which aren't.
For any methods that you do not want to use, comment them out, and for whatever methods that you want to use,
uncomment them.
@see rotate( double theta )
@see outlineBoundingBox( Color c )
*/
private void drawOver( ) {
/* @@@@@@ UNCOMMENT TO USE @@@@@@@@ */
//rotate( 45.0 ); filtered = true; /* @@@@@@ NOTE: filtered boolean var used so that original file is not overwritten @@@@@ */
//cropImage( new Rectangle( 100, 100, 150, 150 ) ); filtered = true;
//outlineBoundingBox( Color.YELLOW ); filtered = true;
//setTransparency( 0.5f ); filtered = true; //value must be between 0.0f and 1.0f
cropLatticeImage( new Rectangle( 0, 0, 192, 192 ) ); filtered = true; latticeImage = false;
}
/**
Draws the original image to the screen (or whatever image is set to ip.bi)
@param fileName The file to draw
*/
public void drawImage( String fileName ) {
updateImages( fileName );
/*win.*/repaint();
}
/**
Helper method used to avoid deep-copy issues, and sets image of original file
@param fileName The nameo of the image to set
@see drawImage( String fileName )
*/
private void updateImages( String fileName ) {
BufferedImage temp = getImage( fileName );
fileName = makeCopy( temp ); //avoids deep copy issues by setting original image to a saved copy of the first image
ip.bi = getImage( fileName );
ip.bi = convertRGBAtoRGB( ip.bi, fileName );
}
/**
Converts a RGB BufferedImage to a RGBA BufferedImage. Note that in place of transparent pixels, black pixels are drawn
@param rgba The RGBA image to convert
@param fileName The new file to create as an RGB image
@return BufferedImage The new image
@see BufferedImage.getWidth()
@see BufferedImage.getHeight()
@see BufferedImage.createGraphics()
@see Graphics.drawImage( BufferedImage image, int x, int y, int width, int height, ImageObserver observer )
@see ImageIO.write( BufferedImage image, String extension, File file )
*/
public BufferedImage convertRGBAtoRGB( BufferedImage rgba, String fileName ) {
BufferedImage newRGB = new BufferedImage( rgba.getWidth(), rgba.getHeight(), BufferedImage.TYPE_INT_RGB );
newRGB.createGraphics().drawImage( rgba, 0, 0, rgba.getWidth(), rgba.getHeight(), null );
try {
ImageIO.write( newRGB , "PNG", new File( fileName ) );
} catch ( IOException e ) {}
return newRGB;
}
/**
Adds a filter to an image by changing the actual pixels of the image and saves the image
Note that this method takes in two file names for filters that use the combination of two images, so if a filter is being used
that just includes one image, any file name can be used for the second image and nothing will be affected unless you are using
a method that requires two images to add a filter (such as bitwiseADDFilter(..))
@param fileName The name of the original file to filter
@param fileName2 The name of the second file to use in case the filter method uses two images
@see ImagePanel.filterImage() -- private class method that draws and sets image
@see saveImage( BufferedImage image )
*/
public void filterNewImage( String fileName, String fileName2 ) {
String name = filtered ? fileNameFiltered : fileName; //Pull right file name --> don't want original if file has been drawn on
ip.bi2 = getFilterImage( name );
ip.bi3 = getFilterImageSecond( fileName2 );
ip.filterImage();
/*win.*/repaint();
saveImage( ip.bi2 );
}
/**
Draw on top of the image instead of changing its pixels and then save that image
@param fileName The name of the file to draw onto
@see ImagePanel.paintImage() - private class method that calls the drawing methods and sets the image
@see saveImage( BufferedImage )
*/
public void drawNewImage( String imageName ) {
BufferedImage temp = getImage( imageName );
imageName = makeCopy( temp );
ip.bi2 = getFilterImage( imageName );
ip.paintImage();
/*win.*/repaint();
saveImage( ip.bi2 ); //save once drawing is complete
}
/**
Adds a filter to an image and overwrites the image instead of creating a new one; saves the result over the old image file too
@see ImagePanel.applyFilter() -- private class method
@see filterNewImage( String fileName, String fileName2 ) -- method that adds a filter but creates a new image
@see saveImage( BufferedImage image )
*/
public void drawCurrentFilter() {
ip.applyFilter();
/*win.*/repaint();
saveImage( ip.bi );
}
/**
Makes a copy of the image and saves it, appending "_copy" to the name (and before the extension).
Used to avoid deep copy issues, particularly in using BufferedImage.subimage(..)
@param image The image to copy and save
@see drawImage( String fileName )
@see ImageIO.write(..)
*/
public String makeCopy( BufferedImage image ) {
String[] fileSplit = fileName.split( "\\." );
String extension = "." + fileSplit[ fileSplit.length - 1 ];
//Automatically name file based on filters applied
String name = "";
//in case fileName has multiple periods... eg. flags.market.street.jpg [is that even a valid file name?]
for( int i = 0; i < fileSplit.length - 1; i++ )
name += fileSplit[i];
//add to name to not overwrite original file
if( !name.contains("_copy") )
name += "_copy";
name += extension; //add extension to fileName
fileName = name;
extension = extension.replaceAll( "\\.", "" ); //extension needs to not have period for ImageIO.write call...
try {
ImageIO.write( image, extension, new File( name ) );
} catch ( IOException e ) {
System.out.println( e.getMessage() );
}
return fileName;
}
/**
Saves the current state of the image to a the original file or a new file (depends on whether the image has been filtered
or not, which is saved in the global boolean variabe 'filtered'
@param image The image to save
@see ImageIO.write( BufferedImage image , String imageType, File file )
@see filtered -- global variable that determines whether original file is overwritten or not
*/
public void saveImage( BufferedImage image ) {
String[] fileSplit = fileName.split( "\\." );
String extension = "." + fileSplit[ fileSplit.length - 1 ];
//Automatically name file based on filters applied
String name = "";
//in case fileName has multiple periods... eg. flags.market.street.jpg [is that even a valid file name?]
for( int i = 0; i < fileSplit.length - 1; i++ )
name += fileSplit[i];
//add to name to not overwrite original file
if( filtered )
name += "_Filtered";
//add to name for lattice images (multiple images saved per function call)
if( latticeImage ) {
name += "_" + latticeRow + "_" + latticeCol;
name = name.replace("copy_", "");
}
name += extension; //add extension to fileName
fileNameFiltered = name;
extension = extension.replaceAll( "\\.", "" ); //extension needs to not have period for ImageIO.write call...
try {
ImageIO.write( image, extension, new File( name ) );
} catch ( IOException e ) {
System.out.println( e.getMessage() );
}
}
/**
Given a String text and the text's properties, create and save an image file of that text
@param text The text to create an image out of
@param fontName The name of the font
@param FONT_TYPE The type of font; Eg: Font.PLAIN, Font.BOLD, Font.ITALIC
@param fontSize The size of the font, in pixels
@param fileName The name of the file to be saved
@return String The name of the new file saved
@see getTextImage(...) <see below>
@see BufferedImage.createGraphics()
@see Graphics2D.setFont( Font font )
@see Graphics2D.getFontMetrics()
@see FontMetrics.stringWidth( String text )
@see FontMetrics.getHeight()
@see Graphics2D.dispose()
@see Graphics2D.setRenderingHint(...)
@see Graphics2D.setColor( Color c )
@see Graphics2D.drawString( String text, int x, int y )
@see FontMetrics.getAscent()
@see ImageIO.write( BufferedImage img, String extension, File file )
@see IOException.printStackTrace()
*/
public String getTextImage( String text, String fontName, int FONT_TYPE, int fontSize, String fileName ) {
final int TEXT_BOX_BUFFER = 20;
final String TAB_SPACES = " ";
text = text.replaceAll( "\\\\t", TAB_SPACES );
// Because font metrics is based on a graphics context, create a small, temp image to ascertain the width and height of the final image
BufferedImage img = new BufferedImage( 1, 1, BufferedImage.TYPE_INT_ARGB );
Graphics2D g2d = img.createGraphics();
Font font = new Font( fontName, FONT_TYPE, fontSize );
g2d.setFont( font );
FontMetrics fm = g2d.getFontMetrics();
int width = getTextWidth( fm, text ) + TEXT_BOX_BUFFER;
int height = fm.getHeight()*( text.split("\\\\n").length ) + TEXT_BOX_BUFFER;
g2d.dispose();
img = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB );
g2d = img.createGraphics();
g2d.setRenderingHint( RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY );
g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g2d.setRenderingHint( RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY );
g2d.setRenderingHint( RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE );
g2d.setRenderingHint( RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON );
g2d.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR );
g2d.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY );
g2d.setRenderingHint( RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE );
g2d.setFont( font );
fm = g2d.getFontMetrics();
g2d.setColor( Color.WHITE );
drawString( g2d, text, 0, 0 );
g2d.dispose();
try {
ImageIO.write( img, DEFAULT_EXTENSION, new File( fileName + "." + DEFAULT_EXTENSION ) );
} catch ( IOException ex ) {
ex.printStackTrace();
}
return fileName + "." + DEFAULT_EXTENSION;
}
final String DEFAULT_EXTENSION = "png";
final String DEFAULT_FONT = "Arial";
final int DEFAULT_FONT_TYPE = Font.PLAIN;
final int DEFAULT_FONT_SIZE = 12;
public String getTextImage( String text, String fontName, int FONT_TYPE, int fontSize ) { return getTextImage( text, fontName, FONT_TYPE, fontSize, text + "" ); }
public String getTextImage( String text, String fontName, int fontSize, String fileName ) { return getTextImage( text, fontName, DEFAULT_FONT_TYPE, fontSize, fileName ); }
public String getTextImage( String text, int FONT_TYPE, int fontSize, String fileName ) { return getTextImage( text, DEFAULT_FONT, FONT_TYPE, fontSize, fileName ); }
public String getTextImage( String text, String fileName ) { return getTextImage( text, DEFAULT_FONT, DEFAULT_FONT_TYPE, DEFAULT_FONT_SIZE, fileName ); }
/**
Since Graphics2D.drawString doesn't handle new line characters, create individually drawn lines occuring at each line break character
@param g The graphics object
@param text The text to draw
@param x The starting x value location of the text
@param y The starting y value location of the text
@see Graphics2D.drawString( String text, int x, int y )
@see String.split( String delimiter )
@see getTextImage( String text, String fontName, int FONT_TYPE, int fontSize, String fileName )
*/
private void drawString( Graphics g, String text, int x, int y ) {
for( String line : text.split("\\\\n") )
g.drawString( line, x, y += g.getFontMetrics().getHeight() );
}
/**
Get the widest line in the paragraph of text and return the width of that line
@param fm The FontMetrics of the text
@param text The text to get the width of
@return int The width of the longest line, in pixels
@see FontMetrics.stringWidth( String text )
@see String.split( String delimiter )
@see getTextImage( String text, String fontName, int FONT_TYPE, int fontSize, String fileName )
*/
private int getTextWidth( FontMetrics fm, String text ) {
int widestValue = 0;
//SOPln( "Text is: " + text );
String[] splitLines = text.split( "\\\\n" );
for( int i = 0; i < splitLines.length; i++ )
widestValue = fm.stringWidth( splitLines[i] ) > widestValue ? fm.stringWidth( splitLines[i] ) : widestValue;
return widestValue;
}
/**
Helper function that sets the image
@param theta The angle to rotate the image by
@see rotateImage( double theta )
*/
public void rotate( double theta ) {
ip.bi2 = rotateImage( theta );
saveImage( ip.bi2 );
}
/**
Rotates an image by a given angle
@param theta The angle to rotate the image by
@return BufferedImage The rotated image
@see rotate( double theta )
*/
private BufferedImage rotateImage( double theta ) {
// Determine the size of the rotated image
double cos = Math.abs( Math.cos( theta ) );
double sin = Math.abs( Math.sin( theta ) );
double width, height;
// Make sure to get data from right image
if( filtered ) {
width = ip.bi2.getWidth();
height = ip.bi2.getHeight();
} else {
width = ip.bi.getWidth();
height = ip.bi.getHeight();
}
int w = (int)( width * cos + height * sin );
int h = (int)( width * sin + height * cos );
// Rotate and paint the original image onto a BufferedImage
BufferedImage out = new BufferedImage( w, h, ip.bi2.getType() );
Graphics2D g2 = out.createGraphics();
g2.setPaint( UIManager.getColor( "Panel.background" ) );
g2.fillRect( 0, 0, w, h );
double x = w / 2;
double y = h / 2;
AffineTransform at = AffineTransform.getRotateInstance( theta, x, y );
x = ( w - width ) / 2;
y = ( h - height ) / 2;
at.translate( x, y );
g2.drawRenderedImage( ip.bi2, at );
g2.dispose();
return out;
}
/**
Helper function that sets the image
@param c The color of the border
@see outlineBB( Color c )
*/
public void outlineBoundingBox( Color c ) {
ip.bi2 = outlineBB( c );
saveImage( ip.bi2 );
}
/**
Paint a thin border around the edge of an image
@param c The color of the border to paint
@return BufferedImage The resulting image with the border around it
@see outlineBoundingBox( Color c )
@see BORDER_WIDTH_PERCENT Determines width of border
*/
private BufferedImage outlineBB( Color c ) {
final int BORDER_WIDTH_PERCENT = 20; //determines the width of the border equal to the average of the width and height divided by this value
int width, height;
//Make sure to get data from right image
if( filtered ) {
width = ip.bi2.getWidth();
height = ip.bi2.getHeight();
} else {
width = ip.bi.getWidth();
height = ip.bi.getHeight();
}
BufferedImage out = new BufferedImage( width, height, ip.bi2.getType() );
Graphics2D g2 = out.createGraphics();
g2.setPaint( UIManager.getColor( "Panel.background" ) ); //set paint to image
g2.fillRect( 0, 0, width, height );
g2.drawRenderedImage( ip.bi2, new AffineTransform() ); //paint image
g2.setColor( c );
int borderWidth = ( (width + height) / 2 ) / BORDER_WIDTH_PERCENT;
g2.fillRect( 0, 0, width, borderWidth );
g2.fillRect( width - borderWidth, 0, borderWidth, height );
g2.fillRect( 0, height - borderWidth, width, height );
g2.fillRect( 0, 0, borderWidth, height );
g2.dispose();
return out;
}
/**
Helper functions that draws the image and sets it
@param percent The percent transparent to set the image to. Range is from (invisible) 0.0f to 1.0f (opaque)
@see transparency( float percent )
*/
public void setTransparency( float percent ) {
ip.bi2 = transparency( percent );
saveImage( ip.bi2 );
}
/**
Set the transparency of an image
@param percent The percent transparent to set the image to. Range is from (invisible) 0.0f to 1.0f (opaque)
@return BufferedImage The transparent image
@see Graphics2D.setComposite( Composite comp )
*/
private BufferedImage transparency( float percent ) {
int width, height;
//Make sure to get data from right image
if( filtered ) {
width = ip.bi2.getWidth();
height = ip.bi2.getHeight();
} else {
width = ip.bi.getWidth();
height = ip.bi.getHeight();
}
BufferedImage out = new BufferedImage( width, height, ip.bi2.getType() );
Graphics2D g2 = out.createGraphics();
g2.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, percent ) );
g2.drawImage( ip.bi2, 0, 0, width, height, null );
g2.dispose();
return out;
}
/**
Method that crops the image and saves the result
@param src The ImageSetter object for this image
@param rect The crop boundaries held in a Rectangle object. A rectangle is defined by ( x, y, w, h ), which corresponds to
top left x coordinate, top left y coordinate, width of rectangle, height of rectangle
@see crop( BufferedImage image, Rectangle rect )
@see saveImage( BufferedImage image )
*/
public void cropImage( Rectangle rect ) {
ip.bi2 = crop( new ImageSetter( ip.bi2 ), rect ); //first param: get current ImageSetter for image
saveImage( ip.bi2 );
}
/**
Globals for cropLatticeImage function. These are implemented because this is the only function that parses images into
multiple save files right now. Perhaps these could be worked into locals, but it didn't fit so well, and I was trying to
do this function in >2 hours (which I did :)
Anyways, latticeRow and latticeCol keep track of coordinate naming system for cropped images produced.
The boolean variable latticeImage is set when cropLatticeImage is called.
*/
public int latticeRow = 0;
public int latticeCol = 0;
/**
Crops an image into multiple cropped images, like a lattice. Eg. Can crop a 400 x 400 px image into 400 total 20x20 px images.
This uses two globals which should be above, latticeRow and latticeCol, to set the names of each new saved image.
Uses cropImage( Rectangle rect ), and simply goes through the entire picture based on the crop size (a rectangle), which is set
when the function is called.
@version 01/25/19 Only works if rectangles evenly tile image (width and height of crop rectangle need to evenly divide pic width
and pic height). cropImage(..) does allow crops that go past the image boundary, but this hasn't been written
into this function yet when iterating the window of the crop rectangle.
@param rect The boundary of the cropped images. Its width and height need to divide pic width and height evenly
@see crop( Rectangle rect )
@see cropLatticeHelper( Rectangle rect ) This is a special image-saving helper method since the changed image needs to not overwrite
the copy of the original (ip.bi2) each time the image is cropped. Ie. ip.bi2 is cropped, but
new crop data saved to ip.bi3, leaving ip.bi2 the same size. This is needed due to the ever-
aggravating nature of working with deep copies using BufferedImage (which sucks)
*/
public void cropLatticeImage( Rectangle rect ) {
latticeImage = true;
int imageWidth = new ImageSetter( ip.bi2 ).getWidth();
int imageHeight = new ImageSetter( ip.bi2 ).getHeight();
int origX = rect.x;
int origY = rect.y;
for( ; rect.x < imageWidth; rect.x += rect.width ) {
for( ; rect.y < imageHeight; rect.y += rect.height ) {
cropLatticeHelper( rect );
latticeCol++;
}
latticeCol = 0;
latticeRow++;
rect.y = origY;
}
}
/**
Similar to cropImage( Rectangle rect ), this function is needed since the copy of the original needs to not be
overwritten when cropping for the second and subsequent images. Ie. the data in ip.bi2 is used, but is altered and
saved to ip.bi3, which is then written and saved
@param rect The boundary of the crop
@see crop( ImageSetter setter, Rectangle rect )
*/
private void cropLatticeHelper( Rectangle rect ) {
ip.bi3 = crop( new ImageSetter( ip.bi2 ), rect ); //first param: get current ImageSetter for image
saveImage( ip.bi3 );
}
/**
Crop an image based on a given Rectangle's coordinates
@param src The image to crop
@param boundary The coordinates specifying what part of the image to keep (rect should be smaller than image)
@return BufferedImage The cropped image
@see BufferedImage.getSubimage( int x, int y, int w, int h )
*/
private BufferedImage crop( ImageSetter src, Rectangle boundary ) {
//one-liner works, but doesn't create deep-copy
//BufferedImage result = src.getSubimage( boundary.x, boundary.y, boundary.width, boundary.height );
Pixel[][] srcData = src.getData();
int newWidth = ( boundary.width > srcData.length ) ? srcData.length : boundary.width;
int newHeight = ( boundary.height > srcData[0].length ) ? srcData[0].length : boundary.height;
Pixel[][] cropData = new Pixel[ newWidth ][ newHeight ];
for( int row = 0, srcRow = boundary.x; row < boundary.height; row++, srcRow++ ) {
for( int col = 0, srcCol = boundary.y; col < boundary.width; col++, srcCol++ ) {
cropData[ row ][ col ] = srcData[ srcRow ][ srcCol ];
}
}
src.setSize( new Rectangle( boundary.x, boundary.y, newWidth, newHeight ) );
src.setData( cropData );
return src.getImage();
}
/**
Creates a deep copy of a BufferedImage. For some methods, such as BufferedImage.getSubimage(..), changes to the original image can
get relayed back to the original image and get it as well. Thus, this method can produce a BufferedImage that creates a truly
deep copy of a BufferedImage
@param image The image to copy
@return BufferedImage The copy of the image, independent of the original if changes are made to it
@see BufferedImage.copyData( WriteableRaster raster )
*/
public static BufferedImage deepCopy( BufferedImage image ) {
ColorModel colorModel = image.getColorModel();
boolean isAlphaPremultiplied = colorModel.isAlphaPremultiplied();
WritableRaster raster = image.copyData( null );
return new BufferedImage( colorModel, raster, isAlphaPremultiplied, null );
}
/**
@NOTUSED: This method copies a BufferedImage, paints the image, and returns the copy, which should be a deep copy
@param source The image to copy and paste and return
@return BufferedImage The deep-copy image
*/
public static BufferedImage copyImage( BufferedImage source ){
BufferedImage image = new BufferedImage( source.getWidth(), source.getHeight(), source.getType() );
Graphics g = image.getGraphics();
g.drawImage( source, 0, 0, null );
g.dispose();
return image;
}
/**
Given an image and another image to layer on this image, as well as the local coordinate for the layer image,
paste in the layered portion onto the original image directly into the pixels of the original image.
Note that this function can layer on an image even if the layer is not completely contained in the image
( see helper function getOverlappingRectAndLayerPoint(..) )
@param image The original image to layer upon
@param layer The image to layer on this image
@param x The x coordinate of where to place the layer image on this image. The layer can be placed such that the layered
image's contents go outside the original image's content, such that the part not overlapping is not included in the
new image. This means that the x and y coordinate can be negative and still work accordingly. eg. -100, -100 would
place the layered image up to the left and above the original image, so that the layered image's right corner is the
only part that is layered and saved onto the new image
@param y The y coordinate of where to place the layer image on this image.
@return void Returns the original image if the layer does not overlap this image
@see getOverlappingRectAndLayerPoint( Rectangle dataRect, Rectangle layerRect, int x, int y )
*/
public void layerImage( ImageSetter image, ImageSetter layer, int x, int y ) {
// get the data from the images
Pixel[][] data = image.getData();
Pixel[][] layerData = layer.getData();
Rectangle dataRect = new Rectangle( data[0].length, data.length );
Rectangle layerRect = new Rectangle( layerData[0].length, layerData.length );
if( !dataRect.intersects( layerRect ) )
return; // No overlap means no layering
Object[] newRectAndPoint = getOverlappingRectAndLayerPoint( dataRect, layerRect, x, y );
Rectangle newLayer = ( Rectangle )newRectAndPoint[0];
Point start = ( Point )newRectAndPoint[1];
int newLayerX = -1;
int newLayerY = -1;
int layerRowCount = 0; // Tells when we have completed layering based on height of overlap Rectangle
Pixel[][] newData = new Pixel[ data.length ][ data[0].length ];
boolean layering = false;
int dataW = data[0].length;
int dataH = data.length;
for( int row = 0; row < dataH; row++ ) {
for( int col = 0; col < dataW; col++ ) {
//Start using the layering pixels instead
if( row == newLayer.y && col == newLayer.x ) {
layering = true;
newLayerX = start.x;
newLayerY = start.y;
}
//If started layering, and if we're back at the x coordinate of the layered Rectangle, start layering again
if( newLayerX >= 0 && col == newLayer.x ) {
layering = true;
}
//Choose the right pixel, either from the original image or the layer based on whether layering or not
if( layering ) {
Pixel debugPix = layerData[ newLayerY ][ newLayerX++ ];
newData[ row ][ col ] = debugPix;
} else {
newData[ row ][ col ] = data[ row ][ col ];
}
//Determine if no longer layering anymore and reset layering x coordinate
if( layerRowCount < newLayer.height && (newLayerX >= newLayer.width + start.x || newLayerY >= newLayer.height + start.y) ) {
layering = false;
newLayerX = start.x;
layerRowCount++;
newLayerY++;
}
//If reach end of layered image and there is still more pixels of the original, make sure layering stops
else if ( layerRowCount >= newLayer.height ) {
newLayerX = -1;
}
}
}
//Set new data to image
image.setData( newData );
}
/**
Given two Rectangle objects, returns the overlapping Rectangle based on the original rectangles coordinates, as well
as the Point of intersection based on the layered Rectangle's coordinates. The layered Rectangle is placed
according to the x and y integer parameters, which should be the coordinates based on the original image's coordinate
system. This function returns the original rectangle if the Rectangles do not overlap. This function also handles
all possibilities of overlap
@param dataRect The original Rectangle
@param layerRect The rectangle placed on the original Rectangle
@param x The x coordinate of where the layered Rectangle is placed, based on the coordinate system of the original Rectangle
@param y The y coordinate of where the layerd Rectangle is placed, based on the coordinate system of the original Rectangle
@return Object[] A two element list, containing the resulting overlapping Rectangle, and secondly, the Point corresponding
to the upper left hand corner of the overlapping Rectangle based in the layered Rectangle coordinate system
@see layerImage( ImageSetter image, ImageSetter layer, int x, int y )
*/
private Object[] getOverlappingRectAndLayerPoint( Rectangle dataRect, Rectangle layerRect, int x, int y ) {
if( !dataRect.intersects( layerRect ) )
return new Object[]{ dataRect, new Point( 0, 0 ) }; // No overlap means no layering
Rectangle newLayer; //Rectangle that holds the offset values for the area to add the layer pixels to
Point start; //Point in the layerRect to start copying pixels
/* SET NEW LAYER RECTANGLE AND STARTING POINT */
//layerRect is completely inside dataRect
if( x >= 0 && y >= 0 && x + layerRect.width < dataRect.width && y + layerRect.height < dataRect.height ) { //do nothing -- layer rectangle is set
newLayer = new Rectangle( x, y, layerRect.width, layerRect.height );
start = new Point( 0, 0 );
}
//dataRect is completely inside layerRect
else if( x < 0 && y < 0 && x + layerRect.width > dataRect.width && y + layerRect.height > dataRect.height ) {
newLayer = dataRect;
start = new Point( Math.abs(x), Math.abs(y) );
}
//layerRect's wider than image and top corners are not overlapping, or bottom corners, or all four corners
else if ( x < 0 && x + layerRect.width > dataRect.width ) {
if( y < 0 ) { //top
newLayer = new Rectangle( 0, 0, dataRect.width, layerRect.height + y );
start = new Point( Math.abs(x), Math.abs(y) );