33import com .ndsl .graphics .pos .Pos ;
44import com .ndsl .graphics .pos .Rect ;
55
6+ import javax .imageio .ImageIO ;
67import java .awt .*;
78import java .awt .image .BufferedImage ;
9+ import java .io .File ;
10+ import java .io .IOException ;
11+ import java .util .ArrayList ;
12+ import java .util .List ;
813
914/**
1015 * Custom Image Clazz
1116 */
1217public class GImage {
1318 public static final Pos left_zero_zero =new Pos (0 ,0 );
1419
15- @ Deprecated
16- public Image image ;
1720 public BufferedImage bufferedImage ;
21+ public Image exportedImage ;
22+ public boolean isChanged =false ;
23+ public static GImage get (String path ) throws IOException {
24+ return get (new File (path ));
25+ }
26+
27+ public static GImage get (File file ) throws IOException {
28+ return new GImage (file );
29+ }
30+
31+ public static GImage [] getAll (String ... path ) throws IOException {
32+ List <File > files =new ArrayList <>();
33+ for (String path_ :path ){
34+ files .add (new File (path_ ));
35+ }
36+ return getAll (files .toArray (new File [0 ]));
37+ }
38+
39+ public static GImage [] getAll (File ... files ) throws IOException {
40+ List <GImage > images =new ArrayList <>();
41+ for (File file :files ){
42+ images .add (new GImage (file ));
43+ }
44+ return images .toArray (new GImage [0 ]);
45+ }
1846 /**
1947 * Default left_up is (0,0)
2048 * Default right_down is (Img.width,Img.height)
2149 */
2250 public Rect size_rect ;
51+ public GImage (String file_Path ) throws IOException {
52+ this (new File (file_Path ));
53+ }
54+
55+ public GImage (File file ) throws IOException {
56+ this (ImageIO .read (file ));
57+ }
58+
2359 public GImage (Image image ){
24- this .image =image ;
2560 this .bufferedImage = (BufferedImage ) image ;
2661 this .size_rect =new Rect (new Pos (0 ,0 ),new Pos (image .getWidth (null ),image .getHeight (null )));
2762 }
@@ -31,14 +66,18 @@ public GImage(Image image){
3166 public GImage trim (Rect trimRect ){
3267 this .size_rect =trimRect ;
3368 isTrimed =true ;
69+ isChanged =true ;
3470 return this ;
3571 }
3672
73+ @ SuppressWarnings ("UnusedReturnValue" )
3774 public Image export (){
3875 Image img =bufferedImage ;
3976 if (isTrimed ){
4077 img =bufferedImage .getSubimage (size_rect .left_up .x ,size_rect .left_up .y ,size_rect .getWidth (),size_rect .getWidth ());
4178 }
79+ this .exportedImage =img ;
80+ this .isChanged =false ;
4281 return img ;
4382 }
4483
@@ -51,6 +90,17 @@ private boolean isZoomed(){
5190 */
5291 public GImage zoom (double zoomScale ){
5392 this .size_rect .zoom (zoomScale );
93+ this .isChanged =true ;
5494 return this ;
5595 }
96+
97+ public Rect getSize (){
98+ return this .size_rect ;
99+ }
100+
101+ public Image getCachedImage (){
102+ if (isChanged ) export ();
103+ if (exportedImage ==null ) export ();
104+ return exportedImage ;
105+ }
56106}
0 commit comments