-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCanvas2D.java
More file actions
55 lines (47 loc) · 1.63 KB
/
Copy pathCanvas2D.java
File metadata and controls
55 lines (47 loc) · 1.63 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
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
public class Canvas2D extends Canvas {
private BufferedImage buffer;
float scale = 1;
public Canvas2D(int width, int height, Color Color) {
setSize(width, height);
setBackground(Color);
}
@Override
public Graphics2D getGraphics() {
try {
buffer = new BufferedImage((int) (scale * getWidth()), (int) (scale * getHeight()), BufferedImage.TYPE_INT_RGB);
}catch(IllegalArgumentException e) {
e.printStackTrace();
}
Graphics2D CTX = (Graphics2D) buffer.getGraphics();
CTX.setColor(getBackground());
CTX.fillRect(0, 0, buffer.getWidth(), buffer.getHeight());
AffineTransform transform = new AffineTransform();
transform.scale(scale, scale);
CTX.transform(transform);
return CTX;
}
public BufferedImage getBuffer() {
return deepCopy(buffer);
}
private static BufferedImage deepCopy(BufferedImage bi) {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
java.awt.image.WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
public void setResolution(float scale) {
this.scale = scale;
}
public float getResolution() {
return scale;
}
public void draw() {
super.getGraphics().drawImage(buffer, 0, 0, getWidth(), getHeight(), null);
}
}