- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotate.java
More file actions
Latest commit
144 lines (127 loc) · 4.34 KB
/
Copy pathRotate.java
File metadata and controls
144 lines (127 loc) · 4.34 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
****************************************************************************
* Flavio Andrade
* 4-25-2017
* Compilation: javac Rotate.java
* Execution: java Rotate Images/Ferrari.jpeg degrees
GrayImage.java has to be compiled before Rotate.java can work.
To display a rotated image without animation call rotateIt(degrees) and then showImage(image).
To rotate the image forever, call rotate().
Use a positive degree to rotate counter-clockwise or a negative degree to rotate clockwise.
*****************************************************************************
*/
importjava.awt.image.BufferedImage;
importjava.awt.Color;
importjavax.swing.ImageIcon;
importjavax.swing.JLabel;
importjava.awt.BorderLayout;
importjavax.swing.JFrame;
importjava.lang.Math;
importjava.awt.Graphics2D;
importjava.awt.geom.AffineTransform;
importjava.awt.image.AffineTransformOp;
publicclassRotate {
publicBufferedImageimage, newImage;
publicGrayImagegray;
publicStringname;
publicintwidth, height, diagonal;
publicRotate (Stringfilename) {
name = filename;
// Gray Image object
gray = newGrayImage(name);
intgW = gray.image.getWidth();
intgH = gray.image.getHeight();
// Scale the image if it is too big.
if (gW > 400 || gH > 400) {
image = this.scale(gray.image, 0.5, 0.5);
}
else {
// The original image
image = gray.returnOriginal();
}
width = image.getWidth();
height = image.getHeight();
// Height and width
this.diagonal();
}
// Compute length of the diagonal.
privatevoiddiagonal() {
diagonal = (int) Math.sqrt(width * width + height * height);
}
//Scale the image for faster animation
publicBufferedImagescale(BufferedImageimg, doublexscale, doubleyscale) {
intw = (int) (img.getWidth() * xscale);
inth = (int) (img.getHeight() * yscale);
BufferedImageresult = newBufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
AffineTransformaf = newAffineTransform();
af.scale(xscale, yscale);
AffineTransformOpscale = newAffineTransformOp(af, AffineTransformOp.TYPE_BILINEAR);
result = scale.filter(img, result);
returnresult;
}
// Take the image and rotate it n degrees.
privateint[] newCoordinate(doubledegrees, intc, inth) {
doubleradians = Math.toRadians(degrees);
// Rotational matrix operation on the coordinate (c, h);
intx = (int) Math.round(Math.cos(radians) * c - Math.sin(radians) * h);
inty = (int) Math.round(Math.sin(radians) * c + Math.cos(radians) * h);
int[] coordinates = { x, y };
returncoordinates;
}
// Take the image and rotate it.
publicvoidrotateIt(doubledegrees) {
newImage = newBufferedImage(diagonal + 10, diagonal + 10, BufferedImage.TYPE_INT_RGB);
intx = 0;
inty = 0;
for (inth = 0; h < height; h++) {
for (intc = 0; c < width; c++) {
// Shift x and y to the middle of the rotating image.
x = c - width / 2;
y = h - height / 2;
int[] a = this.newCoordinate(degrees, y, x);
intcolor = image.getRGB(c, h);
newImage.setRGB(a[1] + (diagonal + 10) / 2, a[0] + (diagonal + 10) / 2, color);
}
}
}
// show the new rotated image.
publicvoidshowImage(BufferedImageimg) {
ImageIconicon = newImageIcon(img);
JLabellabel = newJLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setIcon(icon);
JFrameframe = newJFrame(name);
frame.setSize(diagonal, diagonal);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.setVisible(true);
}
// Save the file.
publicvoidsave(Stringname) {
gray.save(this.newImage, name);
}
// Animation to rotate the image.
publicvoidrotate(doubled) {
//BufferedImage img = this.rotateIt(0, this.image);
gray.setUp(image, diagonal + 10, diagonal + 10, "Rotation");
intr = 0;
// Slow and not smooth with larger images.
while (true) {
this.rotateIt(r += d);
gray.icon = newImageIcon(this.newImage);
gray.label.setIcon(gray.icon);
gray.label.setHorizontalAlignment(JLabel.CENTER);
gray.frame.getContentPane().add(gray.label, BorderLayout.CENTER);
gray.frame.setVisible(true);
gray.frame.repaint();
}
//this.save("Rotated");
}
// Test Case.
publicstaticvoidmain(Stringargs[]) throwsInterruptedException {
Stringname = args[0];
doubledegrees = Double.parseDouble(args[1]);
Rotaterot = newRotate(name);
rot.rotate(degrees);
}
}