This program will help you enhance your mouse event concepts in Java. As the title indicates you will be playing with a brush after you go through the tutorial.
What You need to Know before reading this tutorial:
- Basic Java
- Clear concepts of events in java
- Knowledge of Swing
- A bit of common sense
Explanation
The algorithm for a painter program in Java is simple using the event class in awt; You first declare
two variables to get the x and y coordinate values; Then using MouseMotionLister you trace the values of x and y coordinates where the mouse is being dragged and then you fill an oval there using
fillOval.
Program
First you need to import the classes required for the program execution, as explained above we are going to use the event class which resides insite awt. Also we are using swing for the Graphical User Interface (GUI) of the program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
Next we Declare a class which extends JFrame,then we declare two variables which are intialized to -10 so that the mouse pointer will start from x and y coordinates of -10 ,then we declare a public function Painter and setup the title of the titlebar using super(),then we add a label telling the user to “Click and drag” on bottom:
public class Painter extends JFrame
{
int xvalue = -10, yvalue = -10;
public Painter()
{
super( "Drag to paint" );
getContentPane().add( new Label( "Click and drag" ), BorderLayout.SOUTH );
Below is the most important structure of the program, we first add MouseMotionLister() then we declare
a public function included in MouseMotionListener, named mouseDragged() which detects information
related to dragging (dragging as in clicking and moving the mouse simultaneously); Using this function
we try to get the values of the x and y coordinates of the application where the mouse is being dragged. We use event.getX() and event.getY() for this purpose. repaint() calls the paint function.
addMouseMotionListener
(
new MouseMotionAdapter()
{
public void mouseDragged( MouseEvent event )
{
xvalue = event.getX();
yvalue = event.getY();
repaint();
}
}
);
Next we just set the size of window, and set its visibility to true:
setSize( 500,500 );
setVisible( true );
}
Now we have programmed to take the x and y coordinates value, where the drag event is taking
place; now the last step is to just paint the area where the event is happening; the below lines just fill an oval on x and y coordinates of application where the mouse cursor is being dragged; you can make the brush thinner by reducing 10 to 5 or a smaller number inside fillOval().
public void paint ( Graphics g )
{
g.fillOval( xvalue, yvalue, 10, 10 );
}
In the lines below we just declare a painter class inside main and set the program up for closing the function. If you do not know about WindowListener you can use below line instead:
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
public static void main( String args[] )
{
Painter application = new Painter();
application.addWindowListener
(
new WindowAdapter()
{
public void windowClosing()
{
System.exit( 0 );
}
}
);
}
}
Project
Okay try to alter the above code and modify it. Try to create something such as a brush resizer program, it can be simple… just take input using any means for example two radio buttons having labels “Small” and “Large”, and setup different values of brush size, and instead of giving integer values you will declare variables and set values for them according to “Small” and “Large” radio buttons like g.fillOval( xvalue, yvalue, b_thickness1, b_thickness2 );
Hope that the above tutorial has helped you, if you have any questions you can comment below or email me at [email protected].