forked from drazion/Java-Signature-Pad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvasPainting.java
177 lines (150 loc) · 5.39 KB
/
CanvasPainting.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package signature;
/**
* Developed by Aaron Harvey
* Build Date: April 20, 2012
* Copyright 2012 Aaron Harvey
Copyright (c) 2012 Aaron Harvey
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of
the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;
public class CanvasPainting extends Applet implements MouseMotionListener, MouseListener, ActionListener {
//Variables
int width = 400, height = 55;
private ArrayList<Point> points = new ArrayList<Point>();
HttpURLConnection httpUrlConnection;
//Image save
BufferedImage backbuffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics backg = backbuffer.getGraphics();
Upload upload = new Upload();
//Buttons
Button saveButton;
Button clearButton;
public void init() {
setSize(width, height+50);
backg.setColor( Color.white );
backg.fillRect( 0, 0, width+1, height );
backg.setColor( Color.black );
setBackground(Color.black);
addMouseMotionListener( this );
addMouseListener(this);
setLayout(null);
saveButton = new Button("Save");
clearButton = new Button("Clear");
saveButton.setBounds(10, height+10, 100, 30);
clearButton.setBounds(120, height+10, 100, 30);
add(saveButton);
add(clearButton);
clearButton.addActionListener(this);
saveButton.addActionListener(this);
}
/**
* Draw the line when user drags mouse
*/
public void mouseDragged( MouseEvent e ) {
points.add(e.getPoint());
Graphics2D g2 = (Graphics2D)backg;
g2.setStroke(new BasicStroke(2));
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
for (int i = 0; i < points.size() - 2; i++) {
Point p1 = points.get(i);
Point p2 = points.get(i + 1);
g2.drawLine(p1.x, p1.y, p2.x, p2.y);
}
repaint();
e.consume();
}
/**
* Action Listener for the Applet Buttons
*/
public void actionPerformed(ActionEvent evt) {
//Save Image
if(evt.getSource() == saveButton) {
try {
//Move buttons off screen
saveButton.setBounds(-100, -100, 100, 30);
clearButton.setBounds(-100, -100, 100, 30);
//Anti Alias
Graphics2D g2 = (Graphics2D)backg;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
repaint();
//Blur Signature
float[] matrix = {
0.111f, 0.111f, 0.111f,
0.111f, 0.111f, 0.111f,
0.111f, 0.111f, 0.111f,
};
BufferedImageOp op = new ConvolveOp(new Kernel(3,3, matrix), ConvolveOp.EDGE_NO_OP,null );
backbuffer = op.filter(backbuffer, null);
repaint();
//Convert image information to Array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(backbuffer, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
//Pass Image to be uploaded
upload.httpConn(imageInByte);
baos.close();
//Clear signature and thank customer
points.clear();
//Catch Error
} catch (IOException e) {
e.printStackTrace();
}
}
//Clear the drawing Area
else if (evt.getSource() == clearButton) {
saveButton.setBounds(10, height+10, 100, 30);
backg.setColor(Color.white);
backg.fillRect(0,0,width,height);
points.clear();
repaint();
//Set Color back to black for drawing
backg.setColor(Color.black);
}
}
public void mouseReleased(MouseEvent e) {
points.clear();
}
public void update( Graphics g ) {
g.drawImage(backbuffer,0,0, this);
}
public void paint( Graphics g ) {
update( g );
}
/**
* Do nothing with these methods
*/
@Override
public void mouseClicked(MouseEvent e) { }
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseExited(MouseEvent e) { }
@Override
public void mousePressed(MouseEvent e) { }
@Override
public void mouseMoved(MouseEvent e) { }
}