-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.java
112 lines (73 loc) · 1.84 KB
/
Image.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
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
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
// Classe Image - nao deve ser modificada
public class Image {
private int width, height;
private BufferedImage image;
private Graphics graphics;
private Color color;
private Color bgColor;
public Image(int w, int h, int r, int g, int b){
setup(w, h, r, g, b);
}
public Image(int w, int h){
setup(w, h, 0, 0, 0);
}
private void setup(int w, int h, int r, int g, int b){
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
width = image.getWidth();
height = image.getHeight();
graphics = image.createGraphics();
setBgColor(new Color(limit(r), limit(g), limit(b)));
setColor(Color.WHITE);
clear();
}
private int limit(int value){
return value < 0 ? 0 : (value > 255 ? 255 : value);
}
public int getWidth(){
return width;
}
public int getHeight(){
return height;
}
public void setBgColor(Color c){
bgColor = c;
}
public void setBgColor(int r, int g, int b){
setBgColor(new Color(limit(r), limit(g), limit(b)));
}
public void setColor(Color c){
color = c;
}
public void setColor(int r, int g, int b){
setColor(new Color(limit(r), limit(g), limit(b)));
}
public void clear(){
graphics.setColor(bgColor);
graphics.fillRect(0, 0, width, height);
}
public void setPixel(int x, int y){
if(x >= 0 && y >= 0 && x < width && y < height){
image.setRGB(x, y, color.getRGB());
}
}
public int getPixel(int x, int y){
return image.getRGB(x, y);
}
public void drawLine(int x1, int y1, int x2, int y2){
graphics.setColor(color);
graphics.drawLine(x1, y1, x2, y2);
}
public void save(String fileName){
try{
ImageIO.write(image, "png", new File(fileName));
}
catch(IOException e){
System.out.println("Unable to save image...");
e.printStackTrace();
}
}
}