Geekpedia Programming Tutorials






Programming a Paint Brush in Java

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.

On Friday, February 10th 2006 at 09:34 AM
By Dorez Khan (View Profile)
****-   (Rated 3.6 with 11 votes)
Contextual Ads
More Java Resources
Advertisement
What You need to Know before reading this tutorial:

1. Basic Java

2. Clear concepts of events in java

3. Knowledge of Swing

4. 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 whosnext886@gmail.com.

Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by amit on Tuesday, February 21st 2006 at 09:52 AM

A Question

How To Draw A Free-Hand Picture With This Tool ?

Please Reply If U Have Any Soluction..

Thank You.

amit.

by shrinivas on Wednesday, February 22nd 2006 at 01:19 AM

yeah i would like to see your program please send me a copy of your program on my mail
thank you

by grounchiva on Tuesday, April 11th 2006 at 09:11 AM

I\'d also like a copy if thats possible :D

Thks...

by Ramachandran on Thursday, June 1st 2006 at 01:28 AM

It\'s very nice. please, If you send me a copy of this program. I will be thankful to you.

by Nitin on Monday, August 28th 2006 at 09:13 AM

I would like to have a copy if this project, if possible

by Hemanth on Sunday, October 15th 2006 at 01:23 AM

This is a very nice tutorial for begnners.


Java swing tutorial and source code contains brief tutorials for various components with examples.

Regards,
Hemanth
http://www.java-swing-tutorial.com

by sabha on Thursday, April 5th 2007 at 03:46 AM

yeah i would like to see your program please send me a copy of your program on my mail
thank you

by Hussein on Monday, May 14th 2007 at 04:30 AM

would you please send me a copy of your program on my email with code
thank you

by pido on Friday, June 15th 2007 at 11:27 PM

send me a copy of your java programs....

by hacky jack on Wednesday, July 4th 2007 at 02:17 AM

yup good simple one.can i keep in touch with to share more resources.and i think i can also learn more from u. can u mail me.

by J. Carlos on Sunday, December 16th 2007 at 08:26 AM

I would like a copy of this project, if is possible, please.
Thank you

by aniket on Saturday, January 26th 2008 at 04:32 AM

question:-
In paintbrush proj. I am using canvas but problem is
when we select the menubar then menuItems are go backside
of the canvas.
please help me...

by biruk on Sunday, February 3rd 2008 at 11:26 AM

i need java source code that can do the task of "paint" .i need it to have an option button with an option oval,rectangle,line and with a color option red,green,blue.
many thanks for any support that you may extend to me

by sumit jain on Monday, February 11th 2008 at 08:22 AM

Question:
How i write text on selected area with this code.If possible please sent copy of code in my mail and solve my problem soon.
Please reply soon
Thank You

by shankar on Wednesday, February 13th 2008 at 11:34 AM

i would love to have a copy of this project.
thank you.if possible please send me thru the above mail id.

by kopal on Friday, February 15th 2008 at 04:42 PM

i would like to see your program please send me a copy of your program on my mail
thank you

by Guilherme on Thursday, May 29th 2008 at 03:14 PM

I would like to see your source code, if is it possible, please send on my e-mail, thanks.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs Java Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons