-
Notifications
You must be signed in to change notification settings - Fork 0
/
MouseDrawDemo.java
163 lines (128 loc) · 4.74 KB
/
MouseDrawDemo.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
// Author: Chuck Griffin
// Purpose: an example for my friends
// Date: 4/19/13
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class MouseDrawDemo extends JFrame
{
private static final long serialVersionUID = 1L;
public static void main(String[] args)
{
// This is that class that creates the frame, sets it's size
// and title and the calls my MyCanvas class and adds it
// to the frame.
// make the frame
JFrame mf = new JFrame();
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mf.setSize(800, 600);
mf.setTitle("My Little Painter");
mf.setVisible(true);
// create the canvas and add it to the frame.
MyCanvas mc = new MyCanvas();
mf.add(mc);
} // end of main
} // end of class that creates the frame
class MyCanvas extends JPanel implements MouseListener, MouseMotionListener
{
// This class creates a single JPanel for drawing and then implements
// mouse listeners.
private ArrayList<Point> brushStroke = new ArrayList<Point>();
private String mouseStatus = "Mouse Status:";
private int x;
private int y;
private int clickCount = 0;
public MyCanvas()
{
// Constructor
addMouseListener(this); // add the mouse listeners
addMouseMotionListener(this); // to this panel
this.setBackground(Color.darkGray); // set the color
} // end constructor
public void paintComponent(Graphics g)
{
// This class gets called when the program starts
// AND every time the repaint() method is called.
// this is where everything gets drawn to the panel
super.paintComponent(g); // Always do this first!
g.setColor(Color.green); // set the drawing color
// print some text to the screen
g.drawString("Mouse position: "+ x + " , " + y, 10, 10);
g.drawString(mouseStatus, 10, 20);
g.drawString("Click Count = " + clickCount, 10, 30);
g.drawString("Right click to reset", 10, 40);
// fill a little oval at each point in the brushstroke ArrayList
for(Point p : brushStroke)
{
// the Point class stores an X coordinate and a Y coordinate
// as a single object. The coordinates are stored as type double
// so I need to cast them back to an integer before I can use them.
// The oval is 10 pixels by 10 pixels, and it will be drawn from
// where my mouse is pointing, so to make it look like my mouse
// is drawing the circle, I needed to use a -5 to offset the drawing
g.fillOval((int)p.getX()-5, (int)p.getY()-5, 10, 10);
} // end of drawing the array of points
} // end of paintComponent
// ******** Mouse Listener Methods ********************
// any time you use the MouseListener Interface, you must
// implements these methods.
public void mouseEntered(MouseEvent event)
{
// when your mouse comes into the frame, set the
// mouseStatus variable and call the repaint() method.
mouseStatus = "Mouse Status: IN THE FRAME";
repaint();
}
public void mouseExited(MouseEvent event)
{
// when your mouse leaves the frame, set the mouseStatus
// variable, and set the values of x and y to a -1.
// This is only for your display so it doesn't show an
// X and Y coordinate when you're our of the panel.
// Then call the repaint() method to update the screen.
mouseStatus = "Mouse Status: OUT OF FRAME";
x = y = -1;
repaint();
}
public void mouseClicked(MouseEvent event)
{
if(event.getButton() == event.BUTTON3)
{
brushStroke.clear();
clickCount = 0;
}
else
{
x=event.getX();
y=event.getY();
Point p = new Point(x,y);
brushStroke.add(p);
}
repaint();
}
public void mousePressed(MouseEvent event)
{
clickCount += event.getClickCount();
repaint();
}
public void mouseReleased(MouseEvent event){}
// mouse motion listener
public void mouseDragged(MouseEvent event)
{
// This is where the drawing of the dots is done.
// To get a solid line, Polyline should be used and
// then we can set the stroke of the line.
x=event.getX();
y=event.getY();
Point p = new Point(x,y);
brushStroke.add(p);
repaint();
}
public void mouseMoved(MouseEvent event)
{
x=event.getX();
y=event.getY();
repaint();
}
}