JScratch
Loading...
Searching...
No Matches
Costume.java
Go to the documentation of this file.
1package com.jscratch;
2
3import java.awt.Image;
4import java.awt.image.BufferedImage;
5import java.io.File;
6import java.io.IOException;
7import java.net.URL;
8import javax.imageio.ImageIO;
9
10public class Costume {
11 private String name;
12 private BufferedImage image;
13 private int rotationCenterX;
14 private int rotationCenterY;
15
16 public Costume(String name, String imagePath, int cx, int cy) {
17 this.name = name;
18 this.rotationCenterX = cx;
19 this.rotationCenterY = cy;
20 try {
21 File f = new File(imagePath);
22 if (!f.exists()) {
23 String projectRoot = System.getProperty("jscratch.project.root");
24 if (projectRoot != null) {
25 f = new File(projectRoot, imagePath);
26 }
27 }
28
29 if (f.exists()) {
30 this.image = ImageIO.read(f);
31 } else {
32 // Try classpath via context class loader
33 ClassLoader cl = Thread.currentThread().getContextClassLoader();
34 if (cl == null) cl = getClass().getClassLoader();
35
36 URL url = cl.getResource(imagePath);
37 if (url == null && imagePath.startsWith("/")) {
38 url = cl.getResource(imagePath.substring(1));
39 }
40 if (url == null && !imagePath.startsWith("/")) {
41 url = cl.getResource("/" + imagePath);
42 }
43
44 if (url != null) {
45 this.image = ImageIO.read(url);
46 } else {
47 System.err.println("Failed to load costume: " + imagePath + " (Checked project root: " + System.getProperty("jscratch.project.root") + ")");
48 this.image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
49 }
50 }
51 } catch (IOException e) {
52 System.err.println("Error loading costume: " + imagePath);
53 this.image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
54 }
55 }
56
57 public Costume(String name, BufferedImage img, int cx, int cy) {
58 this.name = name;
59 this.image = img;
60 this.rotationCenterX = cx;
61 this.rotationCenterY = cy;
62 }
63
64 public BufferedImage getImage() {
65 return image;
66 }
67
68 public String getName() {
69 return name;
70 }
71
72 public int getRotationCenterX() {
73 return rotationCenterX;
74 }
75
76 public int getRotationCenterY() {
77 return rotationCenterY;
78 }
79}
BufferedImage getImage()
Definition Costume.java:64
BufferedImage image
Definition Costume.java:12
Costume(String name, BufferedImage img, int cx, int cy)
Definition Costume.java:57
Costume(String name, String imagePath, int cx, int cy)
Definition Costume.java:16