-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.java
49 lines (42 loc) · 1.64 KB
/
utils.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
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
public class utils {
public static BufferedImage loadImage(String path){
BufferedImage image;
try{
image = ImageIO.read(new File(path));
}
catch(Exception e){
e.printStackTrace();
return null;
}
return image;
}
public static BufferedImage scaleImage(BufferedImage image, double xScale, double yScale){
AffineTransform tx = new AffineTransform();
tx.scale(xScale, yScale);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
return op.filter(image, null);
}
public static BufferedImage rotateImage(BufferedImage imageToRotate, int angle) {
int widthOfImage = imageToRotate.getWidth();
int heightOfImage = imageToRotate.getHeight();
BufferedImage newImageFromBuffer = new BufferedImage(widthOfImage, heightOfImage, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = newImageFromBuffer.createGraphics(); // ARGB for transparent bg
g.rotate(Math.toRadians(angle), widthOfImage / 2, heightOfImage / 2);
g.drawImage(imageToRotate, null, 0, 0);
return newImageFromBuffer;
}
public static void pause(int ms) {
try {
Thread.sleep(ms);
}
catch (InterruptedException ex) {
System.out.println("Error occurred!");
}
}
}