-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawArea.java
More file actions
96 lines (84 loc) · 2.75 KB
/
DrawArea.java
File metadata and controls
96 lines (84 loc) · 2.75 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
package DigitRecogniser;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* Drawing area component
*
* @author codingmroberts
*
*/
public class DrawArea extends JComponent {
private Image image;
private Graphics2D g2;
private int currentX, currentY, oldX, oldY;
/**
* Sets the space the panel takes up and applies mouse listeners
*/
public DrawArea() {
setDoubleBuffered(false);
setMinimumSize(new Dimension(280, 280));
setMaximumSize(new Dimension(280, 280));
setPreferredSize(new Dimension(280, 280));
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
oldX = e.getX();
oldY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
currentX = e.getX();
currentY = e.getY();
if (g2 != null) {
// draw line if g2 context not null
g2.setStroke(new BasicStroke(12, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
g2.drawLine(oldX, oldY, currentX, currentY);
repaint();
oldX = currentX;
oldY = currentY;
}
}
});
}
protected void paintComponent(Graphics g) {
if (image == null) {
image = createImage(getSize().width, getSize().height);
g2 = (Graphics2D) image.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
clear();
}
g.drawImage(image, 0, 0, null);
}
/**
* Removes any drawings from the drawArea space
*/
public void clear() {
g2.setPaint(Color.white);
g2.fillRect(0, 0, getSize().width, getSize().height);
g2.setPaint(Color.black);
repaint();
}
/**
* A getter to retrieve the drawn image from the drawArea space
* @return image
*/
public File getImage(){
BufferedImage img = new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
File image = new File("tmp.jpg");
this.paint(img.getGraphics());
try {
ImageIO.write(img, "jpg", image);
}
catch (IOException e) {
e.printStackTrace();
}
return image;
}
}