GraphicsApp API
Eine überarbeitete und angepasste Variante der originalen GraphicsApp-Umgebung
Image.java
gehe zur Dokumentation dieser Datei
1package de.ur.mi.oop.graphics;
2
3import de.ur.mi.oop.utils.FileHelper;
4import de.ur.mi.oop.utils.GraphicsHelper;
5
6import javax.imageio.ImageIO;
7import java.awt.image.BufferedImage;
8import java.io.File;
9import java.io.IOException;
10
11/**
12 * Die Image-Klasse ist ein grafisches Objekt, das ein Bild anzeigt.
13 */
14public class Image extends GraphicsObject implements RotatableGraphicsObject {
15
16 private double rotationAngle;
17
18 private BufferedImage image;
19
20 /**
21 * Lädt und erstellt ein Bild, das an den Koordinaten x und y positioniert ist, und zwar mit
22 * die angegebene Breite und Höhe.
23 *
24 * @param x Die x-Position der linken oberen Ecke des Bildes.
25 * @param y Die y-Position der linken oberen Ecke des Bildes.
26 * @param filePath Der Dateipfad zum anzuzeigenden Bild
27 */
28 public Image(float x, float y, String filePath) {
29 super(x, y);
31 this.rotationAngle = 0;
32 loadImage(filePath);
33 super.setWidth(image.getWidth());
34 super.setHeight(image.getHeight());
35 rescale(true);
36 }
37
38 /**
39 * Liefert das AWT-Image-Objekt zurück
40 *
41 * @return das AWT-Image-Objekt
42 */
43 public BufferedImage getImage() {
44 return image;
45 }
46
47 public int[][] getPixelArray() {
48 int[][] pixels = new int[image.getHeight()][image.getWidth()];
49 for (int x = 0; x < image.getWidth(); x++) {
50 for (int y = 0; y < image.getHeight(); y++) {
51 pixels[y][x] = image.getRGB(x, y);
52 }
53 }
54 return pixels;
55 }
56
57 public void setPixelArray(int[][] pixels) {
58 for (int x = 0; x < image.getWidth(); x++) {
59 for (int y = 0; y < image.getHeight(); y++) {
60 image.setRGB(x, y, pixels[y][x]);
61 }
62 }
63 }
64
65 public void setWidth(float width) {
66 super.setWidth(width);
67 rescale(false);
68 }
69
70 public void setHeight(float height) {
71 super.setHeight(height);
72 rescale(false);
73 }
74
75 /**
76 * Setzt die Breite des Bildes.
77 *
78 * @param width die neue Breite
79 * @param smooth Schalter für Aktivierung qualitativ höherwertigen Skalierung
80 */
81 public void setWidth(float width, boolean smooth) {
82 super.setWidth(width);
83 rescale(smooth);
84 }
85
86 /**
87 * Setzt die Höhe des Bildes.
88 *
89 * @param height die neue Höhe
90 * @param smooth Schalter für Aktivierung qualitativ höherwertigen Skalierung
91 */
92 public void setHeight(float height, boolean smooth) {
93 super.setHeight(height);
94 rescale(smooth);
95 }
96
97 private void rescale(boolean smooth) {
98 image = GraphicsHelper.resizeImage(image, (int) getWidth(), (int) getHeight(), smooth);
99 }
100
101 private void loadImage(String filePath) {
102 try {
103 File file = FileHelper.loadFile(filePath);
104 image = ImageIO.read(file);
105 } catch (IOException e) {
106 e.printStackTrace();
107 }
108 }
109
110 @Override
112 return new Point(this.getXPos() + this.getWidth()/2, this.getYPos() + this.getHeight()/2);
113 }
114
115 /**
116 * Liefert den aktuellen Rotationswinkel des Objects
117 *
118 * @return Aktueller Rotationswinkel als Radians
119 */
120
121 public double getRotationAngle() {
122 return rotationAngle;
123 }
124
125 /**
126 * Liefert den aktuellen Rotationswinkel des Objects
127 *
128 * @return Aktueller Rotationswinkel in Grad
129 */
131 return Math.toRadians(rotationAngle);
132 }
133
134 /**
135 * Setzt den Rotationswinkel des Objekts
136 *
137 * @param angle Neuer Rotationswinkel in Grad
138 */
139 public void setRotationAngle(double angle) {
140 if (angle >= MIN_ROTATION_ANGLE && angle <= MAX_ROTATION_ANGLE) {
141 rotationAngle = angle;
142 }
143 }
144
145}
void setWidth(float width, boolean smooth)
Definition: Image.java:81
void setRotationAngle(double angle)
Definition: Image.java:139
BufferedImage getImage()
Definition: Image.java:43
Image(float x, float y, String filePath)
Definition: Image.java:28
double getRotationAngleInRadians()
Definition: Image.java:130
void setWidth(float width)
Definition: Image.java:65
void setHeight(float height, boolean smooth)
Definition: Image.java:92
void setHeight(float height)
Definition: Image.java:70
void setPixelArray(int[][] pixels)
Definition: Image.java:57
static BufferedImage resizeImage(BufferedImage image, int newWidth, int newHeight)