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
|
package ru.olamedia.asset;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Sprite {
private BufferedImage combinedImage;
Graphics g;
int width;
int height;
int w;
int h;
int sizeX;
int sizeY;
int size;
int length = 0;
SpriteOffset current;
public void dispose(){
if (null != combinedImage){
combinedImage = null;
}
if (null != g){
g.dispose();
}
}
int waiting = 0;
public BufferedImage getImage() {
return combinedImage;
}
public Sprite(int width, int height, int w, int h) {
this.width = width;
this.height = height;
this.w = w;
this.h = h;
sizeX = width / w;
sizeY = height / h;
size = sizeY * sizeX;
current = new SpriteOffset(0, 0);
combinedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
g = combinedImage.getGraphics();
/*g.setColor(Color.darkGray);
g.setPaintMode();
g.fillRect(0, 0, width, height);*/
}
public void next() {
current.x += w;
if (current.x + w > width) {
current.x = 0;
current.y += h;
}
}
public SpriteRectangle addImage(String path) throws AssetNotFoundException {
final Asset asset = AssetManager.getAsset(path);
BufferedImage image;
try {
image = ImageIO.read(asset.getInputStream());
return addImage(image);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public SpriteRectangle addImage(BufferedImage img) {
if (current.y + h > height) {
throw new RuntimeException("Out of bounds while creating Sprite");
}
final int x = (int) current.x;
final int y = (int) current.y;
waiting++;
// img.getWidth(new ImageObserver() {
// @Override
// public boolean imageUpdate(Image img1, int arg1, int arg2, int arg3,
// int arg4, int arg5) {
//System.out.println("Sprite: draw " + img.getWidth(null));
/*g.setColor(Color.magenta);
g.fillRect(x, y, w, h);*/
g.drawImage(img, x, y, w, h, null, null);
waiting--;
// return false;
// }
// });
SpriteRectangle area = new SpriteRectangle((1 / (float) width) * current.x, (1 / (float) height) * current.y,
(1 / (float) width) * (current.x + 16), (1 / (float) height) * (current.y + 16));
next();
return area;
}
public void write(String path) {
/*
* while (waiting > 0) {
* try {
* Thread.sleep(100);
* } catch (InterruptedException e) {
* e.printStackTrace();
* }
* }
*/
g.dispose();
try {
ImageIO.write(combinedImage, "PNG", new File(path));
} catch (IOException e) {
e.printStackTrace();
}
}
}
|