Skip to content

Commit 90b8909

Browse files
committed
add Animator and TimeScaledAnimator
1 parent aaa8129 commit 90b8909

8 files changed

Lines changed: 206 additions & 6 deletions

File tree

graphics/src/main/java/com/ndsl/graphics/GraphicsMain.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.ndsl.graphics.display.drawable.Drawable;
55
import com.ndsl.graphics.display.drawable.RectDrawable;
66
import com.ndsl.graphics.display.drawable.StringDrawable;
7+
import com.ndsl.graphics.display.drawable.animate.TimeScaledAnimator;
8+
import com.ndsl.graphics.display.drawable.img.GImage;
79
import com.ndsl.graphics.display.drawable.img.ImageDrawable;
810
import com.ndsl.graphics.display.drawable.ui.Button;
911
import com.ndsl.graphics.display.layer.Layer;
@@ -22,8 +24,8 @@ public class GraphicsMain {
2224

2325
public Display display;
2426

25-
public static void main(String[] args){
26-
new GraphicsMain().layer_test();
27+
public static void main(String[] args) throws IOException {
28+
new GraphicsMain().animatorTest();
2729
}
2830

2931
public void onRun(){
@@ -66,4 +68,13 @@ public void layer_test(){
6668
if (display.limiter.onUpdate()) display.update();
6769
}
6870
}
71+
72+
public void animatorTest() throws IOException {
73+
display = new Display("NDSL/Graphics",3,new Rect(new Pos(100,100),new Pos(600,600)));
74+
display.layerManager.set(new Layer("Animator"),1);
75+
display.layerManager.get("Animator").add(new Drawable(new TimeScaledAnimator("anime",1000,new Pos(100,100), GImage.getAll("graphics\\src\\main\\java\\com\\ndsl\\graphics\\display\\drawable\\img\\bun_face.jpg","graphics\\src\\main\\java\\com\\ndsl\\graphics\\display\\drawable\\img\\bun_face_privacy.jpg"))));
76+
while (true){
77+
if (display.limiter.onUpdate()) display.update();
78+
}
79+
}
6980
}

graphics/src/main/java/com/ndsl/graphics/display/drawable/ICustomDrawable.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
import java.awt.*;
77

8+
/**
9+
* @See IDrawable
10+
* @Deprecated
11+
*/
12+
@Deprecated
813
public interface ICustomDrawable {
914
void onDraw(Graphics g, Rect showingRect);
1015
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.ndsl.graphics.display.drawable.animate;
2+
3+
import com.ndsl.graphics.display.Display;
4+
import com.ndsl.graphics.display.drawable.IDrawable;
5+
import com.ndsl.graphics.display.drawable.img.GImage;
6+
import com.ndsl.graphics.pos.Pos;
7+
import com.ndsl.graphics.pos.Rect;
8+
9+
import java.awt.*;
10+
11+
public class Animator implements IDrawable {
12+
public GImage[] images;
13+
public int CurrentImageIndex=0;
14+
public GImage CurrentImage;
15+
public String id;
16+
public Rect rect;
17+
18+
public Animator(String id,GImage... images) {
19+
if(!isAllSameSize(images)) throw new IllegalArgumentException("Not Match Image Size");
20+
this.id=id;
21+
this.images=images;
22+
this.CurrentImage=this.images[this.CurrentImageIndex];
23+
this.rect=this.images[this.CurrentImageIndex].size_rect;
24+
}
25+
26+
public Animator(String id,int startIndex,GImage... images){
27+
this(id,images);
28+
this.CurrentImageIndex=startIndex;
29+
}
30+
31+
public Animator(String id, Pos left_up, GImage... images){
32+
this(id,images);
33+
this.rect.shift(left_up.x,left_up.y);
34+
}
35+
36+
public Animator(String id,int startIndex, Pos left_up, GImage... images){
37+
this(id,startIndex,images);
38+
this.rect.shift(left_up.x,left_up.y);
39+
}
40+
41+
@Override
42+
public void onDraw(Graphics g, Rect showingRect) {
43+
next();
44+
g.drawImage(CurrentImage.getCachedImage(),showingRect.left_up.x,showingRect.left_up.y,showingRect.getWidth(),showingRect.getHeight(),null);
45+
}
46+
47+
@Override
48+
public Rect getShowingRect() {
49+
return rect;
50+
}
51+
52+
@Override
53+
public boolean isShowing(Display display) {
54+
return display.isShowing(getShowingRect());
55+
}
56+
57+
@Override
58+
public String getID() {
59+
return id;
60+
}
61+
62+
private boolean isSameSize(GImage obj1,GImage obj2){
63+
return obj1.getSize().equals(obj2.getSize());
64+
}
65+
66+
private boolean isAllSameSize(GImage... objs){
67+
for (int i = 0; i < objs.length-1; i++) {
68+
if(!objs[i].getSize().equals(objs[i+1].getSize())) return false;
69+
}
70+
return true;
71+
}
72+
73+
public void next(){
74+
++CurrentImageIndex;
75+
if(images.length-1<CurrentImageIndex){
76+
CurrentImageIndex=0;
77+
}
78+
this.CurrentImage=images[CurrentImageIndex];
79+
}
80+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.ndsl.graphics.display.drawable.animate;
2+
3+
import com.ndsl.graphics.display.drawable.img.GImage;
4+
import com.ndsl.graphics.pos.Pos;
5+
6+
public class TimeScaledAnimator extends Animator{
7+
public long perFrameTime;
8+
public long latest_time=System.currentTimeMillis();
9+
10+
public TimeScaledAnimator(String id,long perFrameTime, GImage... images) {
11+
super(id, images);
12+
this.perFrameTime=perFrameTime;
13+
}
14+
15+
public TimeScaledAnimator(String id, int startIndex,long perFrameTime, GImage images) {
16+
super(id, startIndex, images);
17+
this.perFrameTime=perFrameTime;
18+
}
19+
20+
public TimeScaledAnimator(String id,long perFrameTime, Pos left_up, GImage... images){
21+
super(id,left_up,images);
22+
this.perFrameTime=perFrameTime;
23+
}
24+
25+
public TimeScaledAnimator(String id,long perFrameTime,int startIndex, Pos left_up, GImage... images){
26+
super(id,startIndex,left_up,images);
27+
this.perFrameTime=perFrameTime;
28+
}
29+
30+
@Override
31+
public void next() {
32+
if(System.currentTimeMillis()-latest_time>perFrameTime){
33+
super.next();
34+
latest_time = System.currentTimeMillis();
35+
}
36+
}
37+
}

graphics/src/main/java/com/ndsl/graphics/display/drawable/img/GImage.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,60 @@
33
import com.ndsl.graphics.pos.Pos;
44
import com.ndsl.graphics.pos.Rect;
55

6+
import javax.imageio.ImageIO;
67
import java.awt.*;
78
import 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
*/
1217
public 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
}

graphics/src/main/java/com/ndsl/graphics/display/drawable/img/ImageDrawable.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.ndsl.graphics.display.drawable.img;
22

33
import com.ndsl.graphics.display.Display;
4-
import com.ndsl.graphics.display.drawable.ICustomDrawable;
54
import com.ndsl.graphics.display.drawable.IDrawable;
65
import com.ndsl.graphics.pos.Pos;
76
import com.ndsl.graphics.pos.Rect;
6.29 KB
Loading

graphics/src/main/java/com/ndsl/graphics/pos/Rect.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,22 @@ public void zoom(double zoomScale) {
6767
public String toString() {
6868
return "{"+this.left_up.toString() + " " + this.right_down.toString()+"}";
6969
}
70+
71+
@Override
72+
public boolean equals(Object obj) {
73+
if(super.equals(obj)) return true;
74+
if(obj instanceof Rect){
75+
Rect r=((Rect)obj);
76+
if(left_up.equals(r.left_up) && right_down.equals(r.right_down)){
77+
return true;
78+
}
79+
}
80+
return false;
81+
}
82+
83+
public Rect shift(int x,int y){
84+
this.left_up.shift(x,y);
85+
this.right_down.shift(x,y);
86+
return this;
87+
}
7088
}

0 commit comments

Comments
 (0)