Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

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.3 with 15 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.

by GUST_UNAD on Thursday, October 30th 2008 at 10:58 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 palet color.
I need it to have an option form 3D.

Many thanks for any support that you may extend to me.
I am Colombian nothing inglish.

by Lia on Thursday, November 20th 2008 at 01:59 PM

Just like most of the previous requesting people I need the source code as well, I need it to be able to have the shapes(rectangle, oval etc.) resized and rotated. I have my source code which is good for drawing only but I'm having a very hard time with using matrix rotating and resizing so PLEASE, give me a hand. It's very urgent because it is for a very important school assignment and if I don't do it I will fail. Thank you in advance.

by yoni on Monday, April 27th 2009 at 11:51 PM

Can you give me a paint brush coding??

by yoni on Monday, April 27th 2009 at 11:51 PM

Can you give me a paint brush coding??

by mm on Tuesday, May 5th 2009 at 10:45 PM

Can you give me a paint brush coding??

by Sandun on Wednesday, June 3rd 2009 at 02:42 AM

Hi,
I am doing an assignment regarding a drawing tool. I would like to see your source code, if it is possible, please send on my e-mail. it is "sandun_2222@yahoo.com"

Thanks alot,
Sandun

by pooja on Thursday, July 2nd 2009 at 01:10 PM

Hi,
I like this idea.
I m student of M.Sc. nd i have to do project this year.
So, can you suggest some project topics like this or any other which can help me.
Plz reply.

by pooja on Thursday, July 2nd 2009 at 01:10 PM

Hi,
I like this idea.
I m student of M.Sc. nd i have to do project this year.
So, can you suggest some project topics like this or any other which can help me.
Plz reply.

by jigs on Wednesday, August 5th 2009 at 04:25 AM

source code site

by Udaykumar patil on Thursday, August 13th 2009 at 02:48 AM




I will try with your suport,
I would like to have a copy if this project, if possible

thank you sir

by Pravin on Tuesday, September 15th 2009 at 01:14 AM

Hi,
I like this idea.
I m student of M.Sc. nd i have to do project this year.
So, can you suggest some project topics like this or any other which can help me.
Plz reply. Can you give me a paint brush coding??

by Anagha on Wednesday, September 23rd 2009 at 11:49 PM

I want java source code to implement paint brush in java painter

by Mark on Friday, October 2nd 2009 at 06:53 PM

Some parts were hard to get. When it said addMouseMotionListener, it turned out to be an error.

by neha on Wednesday, October 28th 2009 at 07:36 AM

i want java coding of paint brush as i am working on dis project...can u jst give me d coding as well as explanztion so that i would get sum help
thanking u!!

by HAILU CHEMIR on Monday, January 4th 2010 at 11:40 AM

Hi
Youre progarm sorce code is best.But less, please send to me a full source code of paint in java to my email.

by HAILU CHEMIR on Monday, January 4th 2010 at 11:41 AM

Hi
Youre progarm sorce code is best.But less, please send to me a full source code of paint in java to my email.

by widy on Wednesday, February 17th 2010 at 07:20 PM

nice share :)

by surabhi on Friday, February 19th 2010 at 12:27 AM

its a nice.
i would be pleased if u send mme a copy of this plz wid coding................

thanks

by prathmesh on Sunday, March 7th 2010 at 01:27 AM

thank u very much for this code.

by Jery on Wednesday, March 10th 2010 at 01:05 PM

Sir,
I want to learn hw the paint brush was developed, if u send the source code that will very helpful for me.

Thanks alot..

by Sanjay Sharma on Sunday, March 21st 2010 at 01:40 PM

Sir,
I want to learn hw the paint brush was developed, if u send the source code that will very helpful for me.

by Jery on Friday, March 26th 2010 at 04:47 AM

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
/*<applet code="Painter" width=350 height=250>
</applet>*/
public class Painter extends Applet
{
int xvalue,yvalue,x1,y1;

public void init()
{
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
class MouseHandler implements MouseListener
{
public void mousePressed(MouseEvent e)
{

}
public void mouseReleased(MouseEvent e)
{
xvalue=0;yvalue=0;
x1=0;y1=0;
}
public void mouseClicked(MouseEvent e)
{
xvalue=e.getX();
yvalue=e.getY();
//repaint();
}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
}
class MouseMotionHandler implements MouseMotionListener
{
public void mouseDragged(MouseEvent e)
{
x1=e.getX();
y1=e.getY();
repaint();
}
public void mouseMoved(MouseEvent e)
{
//x1=e.getX();
//y1=e.getY();
//repaint();
}
}
public void paint(Graphics g)
{
g.drawOval(xvalue,yvalue,x1,y1);
}
}

by saisindhu on Thursday, April 15th 2010 at 04:10 AM

hello ,how to draw a free hand picture with mouse can u send the code to my mail

by 7anan on Sunday, May 2nd 2010 at 02:22 AM

سلام عليكم
لو سمحتو بدي برنامج الرسام بلغة الجافا

by chandrika on Monday, May 3rd 2010 at 02:35 PM

can u tell me the advantages of paint application using java applet over others??

by 7anan on Monday, May 10th 2010 at 02:42 AM

Programming a Paint in Java‎

by 7anan on Monday, May 10th 2010 at 02:42 AM

Programming a Paint in Java‎

by 7anan on Monday, May 10th 2010 at 02:43 AM

Programming a Paint in Java‎

by 7anan on Monday, May 10th 2010 at 02:43 AM

Programming a Paint in Java‎

by Sehar on Tuesday, May 18th 2010 at 11:34 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:34 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:34 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:34 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:34 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:34 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:34 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:34 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:34 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:34 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:34 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:34 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:35 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:35 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:35 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by Sehar on Tuesday, May 18th 2010 at 11:35 AM

Hello Sir,
I am a Student of MSc IT..
Can u please give some projects topics like this for my project..

thank u.
waiting 4 reply..

by 2ala2 on Wednesday, June 9th 2010 at 08:08 AM

hi sir,
i would like to have some help in my java paint program if you please

the problem is that i could not know how to resize the shapes drawn

thanks

by 2ala2 on Wednesday, June 9th 2010 at 08:08 AM

hi sir,
i would like to have some help in my java paint program if you please

the problem is that i could not know how to resize the shapes drawn

thanks

by kalyan on Monday, June 28th 2010 at 03:37 AM

its very useful for freshers, and the persons who have passion to learn java.

by deeksha on Tuesday, July 20th 2010 at 06:37 AM

its good topic for a project so can u please send me the coding of this "paint brush" with the explanation so that it will be helpful for me
plz
Thank u

by sarin on Tuesday, August 3rd 2010 at 06:08 AM

Hi ...
This is very interesting to me so pls send full source code of paint in java to my email.

by ahmad sulaiman on Monday, August 23rd 2010 at 06:11 PM

send me a complete java code for paintig

by sanjay on Saturday, August 28th 2010 at 01:50 PM

hi everyone if you need any kind of projects in java
you can mail me on-quimztech@gmail.com

by sanjay on Saturday, August 28th 2010 at 01:50 PM

hi everyone if you need any kind of projects in java
you can mail me on-quimztech@gmail.com

by neelima on Wednesday, September 1st 2010 at 09:46 PM

Hello,

could u please send the whole code to my mail is.. thanks a lot.


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 >>
Sponsors
Discover Geekpedia

Other Resources