Free Hand In JAVA



Objective:
Define a class that enables the drawing of freehand lines on a screen through mouse clicking and dragging. Use anonymous inner classes to implement event listeners. The drawing should be cleared when a key is pressed and the line colour should be selectable. Define a test class to demonstrate the program.

Packages used:
  1. java.awt.*;   : Contains all of the classes for creating user interfaces and for painting graphics and images.
  2. java.awt.event.*  : Provides interfaces and classes for dealing with different types of events fired by AWT components.

Classes used:
  • Frame     : A Frame is a top-level window with a title and a border.The size of the frame includes any area designated for the border.
·         MenuBar  : The MenuBar class encapsulates the platform's concept of a menu bar bound to a frame. In order to associate the menu bar with a Frame object, call the frame's setMenuBar method.
·         Menu : A Menu object is a pull-down menu component that is deployed from a menu bar. Each item in a menu must belong to the MenuItem class. It can be an instance of MenuItem, a submenu (an instance of Menu), or a check box (an instance of CheckboxMenuItem).
·         MenuItem : All items in a menu must belong to the class MenuItem, or one of its subclasses. The default MenuItem object embodies a simple labeled menu item.
·         Graphics    : The Graphics class is the abstract base class for all graphics contexts that allow an application to draw onto components that are realized on various devices, as well as onto off-screen images.
·         Color          : The Color class is used to encapsulate colors in the default sRGB color space or colors in arbitrary color spaces identified by a ColorSpace.
Logical flow of the program:
·         File Menu contains clear and quit options
·         Colour menu has blue, red and green options to change stroke colour
·         When the mouse if first pressed, mousePressed event is called which sets the x1 and y1 to the current mouse coords
·         When the mouse is dragged in paint area, mouseDragged event is called which sets x2 and y2 as the coordinates of the mouse pointer and passes it to a function called draw which in turn draws a line between x1,y1 and x2,y2 . Then it sets x1 and y1 as current mouse co-ords. So, the next drag event draws line between these cords and the newly moved co-ords of the mouse. This goes on on every drag event.
·         When the mouse is finally released, mouseReleased event is called which sets x2 and y2 as the coordinates of the mouse pointer and passes it to a function called draw which in turn draws a line between x1,y1 and x2,y2 . Then it sets x1 and y1 as current mouse co-ords



Important notes :
·         Import java.awt.event.* even if you have imported java.awt.*
·         If in the program Frame is inherited then the inheriting class’s constructor initializes the application.
1.      If super() method is called ,it should be the first line inside the constructor.
2.      Frame’s visibility and size must be set.

0 comments:

Copyright © 2012 OpenTechZone | Kesari Technologies |