A 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.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This 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.
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.7 with 23 votes)
1. Basic Java
2. Clear concepts of events in java
3. Knowledge of Swing
4. A bit of common sense
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
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:
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:
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.
Next we just set the size of window, and set its visibility to 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().
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 );
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 firstname.lastname@example.org.
|Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList|
Rate this tutorial
There are no related tutorials.
Related Source Code
There is no related source code.
Java Job Search