-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPicture.java
More file actions
50 lines (47 loc) · 1.5 KB
/
Picture.java
File metadata and controls
50 lines (47 loc) · 1.5 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
package T2;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Picture {
private BufferedImage image;
public int width;
private int height;
public Picture(String filename) throws IOException {
File file = new File(filename);
image = ImageIO.read(file);
width = image.getWidth();
height = image.getHeight();
}
public Picture(int width, int height){
this.width = width;
this.height = height;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public void setColor(int col, int row, Color c){
image.setRGB(col, row, c.getRGB());
}
public Color getColor(int col, int row){
int rgb = image.getRGB(col, row);
return new Color(rgb);
}
public void save(String filename) throws IOException {
String suffix = filename.substring(filename.lastIndexOf('.')+1);
if (suffix.equalsIgnoreCase("png") || suffix.equalsIgnoreCase("jpg"))
ImageIO.write(image, suffix, new File(filename));
}
public void darker(){
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++){
Color c = getColor(i, j);
setColor(i, j, c.darker());
}
}
}