QuickDraw source code:
1 import
java.awt.*;
2 import
java.awt.event.*;
3 import
javax.swing.*;
class Draw extends JFrame implements
ActionListener{
4 protected int x_axis,
y_axis;
5 protected JColorChooser
jser;
6 protected JButton
btchooser;
7 protected Color fcolor;
8 Draw(){
9 fcolor=Color.BLACK;
10 x_axis=y_axis=0;
11 setDefaultCloseOperation(EXIT_ON_CLOSE);
12 addMouseListener(new
Readxy());
13 addMouseMotionListener(new
LineDraw());
14 setLayout(null);
15 setTitle("Quick
Draw");
16 setExtendedState(this.getExtendedState()
| this.MAXIMIZED_BOTH);
17 btchooser=new
JButton("Set pen color");
18 btchooser.setLocation(1,1);
19 btchooser.setSize(200,25);
20 btchooser.addActionListener(this);
21 add(btchooser);
22 setVisible(true);
23 requestFocus();
24 setCursor(new
Cursor(Cursor.HAND_CURSOR));
}
25 public void resetXY(int
x,int y){
x_axis=x;
y_axis=y;
}
26 class Readxy extends
MouseAdapter{
public void
mousePressed(MouseEvent e){
resetXY(e.getX(),e.getY());
}
}
27 class LineDraw extends
MouseMotionAdapter{
public void
mouseDragged(MouseEvent e){
int
x=e.getX();
int
y=e.getY();
Graphics
g=getGraphics();
g.setColor(fcolor);
g.drawLine(x_axis,y_axis,x,y);
resetXY(x,y);
}
}
28 public void
actionPerformed(ActionEvent e){
if(e.getSource()==btchooser)
{
jser=new
JColorChooser();
fcolor=jser.showDialog(this,"Color
Chooser",Color.BLACK);
requestFocus();
}
}
}
29 public
class QuickDraw{
public static void main(String
args[]){
new Draw();
}
}
Code Explanation:
1 Introduce classes in awt package to the program.
2 Introduce classes in awt.event package to the program.
3 Introduce classes in swing package to the program.
4 Declare variables x_axis, and y_axis to record the values of x-axis, and y-axis of the mouse pointer.
5 Declare variable jser to refer to the JColorChooser object.
6 Declare variable btchooser to refer to the JButton object.
7 Declare variable fcolor to store the color value chosen from the JColorChooser dialog box.
8 Draw is the constructor of the class Draw. The code in the constructor is executed when an object of the Draw class is created.
9 Initialize the fcolor variable to the black color.
10 Initialize the x_axis, and y_axis variables to zero.
11 Allow the frame window to close when the user clicks the close button.
12 Register the mouse event with the current frame window.
13 Register the mouse motion event with the current frame window.
14 Set the layout of the current frame window to null so that you can customize the location of the components on it.
15 Set title of the current frame window.
16 Maximize the current frame window when it opens.
17 Create a JButton object. The JButton object is clicked to display a JColorChooser dialogbox.
18 Define the location of the JButton object on the current frame window.
19 Define the size of the JButton object.
20 Register the action event with the JButton object. The ActionListener interface has the actionPerformed method to be rewritten to enable the button to perform an action when it is clicked.
21 Add the JButton object to the current frame window.
22 Make sure the current frame window visible.
23 Make focus on the current frame window.
24 Set cursor type.
25 The resetXY method is defined to assign values to x-axis and y-axis of the mouse pointer to x_axis, and y_axis variables.
26 The class Readxy extends the MouseAdapter class so that the mousePressed method can be inherited. The mousePressed method is executed when the mouse is pressed in the current frame window.
27 The class LineDraw extends the MouseMotionAdapter class to inherit the mouseDragged method. This method is executed when the mouse is dragged in the current frame window.
28 To able a button to perform an action when it is clicked, the actionPerformed method of the ActionListener interface has to be rewritten.
29 The QuickDraw class has the main method to start the program.
No comments:
Post a Comment