Thursday, 27 February 2014

METHODS FOR FONTS IN GRAPHICS CLASS

WORKING WITH FONTS

Font Class in java applet with sample program..........

The AWT supports multiple type fonts. Within AWT, a font is specified by its name, style, and size. Each platform that supports Java provides a basic set of fonts. Beginning with Java 2, fonts have a family name, a logical font name, and a face name. The family name is the general name of the font, such as Courier. The logical name specifies a category of font, such as Monospaced. The face name specifies a specific font, such as Courier Italic.
An instance of the Font class represents a specific font to the system.

The Font class defines these variables:-


 Variable                     Meaning


 protected String name                    Name of the font
 protected int size                              Size of the font in points
 protected int style                            Font style


There are four styles for displaying fonts in Java: plain, bold, italic, and bold italic. Three class constants are used to represent font styles:

public static final int BOLD:The BOLD constant represents a boldface font.

public static final int ITALIC:The ITALIC constant represents an italic font.

public static final int PLAIN:The PLAIN constant represents a plain or normal font.

The combination BOLD/ITALIC represents a bold italic font. PLAIN combined with either BOLD or ITALIC represents bold or italic, respectively.

INTRODUCING THE AWT:Working With Color

Java supports color in a portable, device-independent fashion.


Working with Color

Java supports color in a portable, device-independent fashion. The AWT color system allows you to specify any color you want. It then finds the best match for that color, given the limits of the display hardware currently executing your program or applet. Thus, your code does not need to be concerned with the differences in the way color is supported by various hardware devices. Color is encapsulated by the Color class.


color constructors


To create your own colors,using one of the color constructors.the most commly used forms are shown here:

  • Color (int red, int green, int blue)
  • Color (int rgbValue) //int rgbValue = (0xff000000 | (0xc0<<16) | (0x00 <<8) | 0x00);
  • Color (float red, float green, float blue)

The first constructors take three integers that specify the color as the mix of red,green and blue. these values must be between 0 and 255.

example:
new color(255,100,100); // light red.


The second color constructor takes a single integer that contain the mix of red,greenand blue packed into an integer.the integer is organized with red in bits 16 to 23,green in bits 8 to 15,and blue in bits 0 to 7.

BASIC GRAPHICS IN JAVA WITH EXAMPLE

SETTING THE PAINT MODE

set drawing mode to XOR  example..........

PAINTING MODE:


There are two painting or drawing modes for the Graphics class:- paint mode which is the default mode and XOR mode. In paint mode, anything we draw replaces whatever is already on the screen i.e. it overwrites any preexisting contents. If you draw a green circle, you get a green circle, no matter what was underneath.The paint mode determines how objects are drawn in a window. It is possible to have new objects XORed on to the window by using setXORMode().

Syntax

void setXORMode(Color XORColor)

INTRODUCING THE AWT:Working With Graphics

AWT supports a rich assortment of graphics method.All graphics are drawn reative to window..........

Working with Graphics

The AWT supports a rich assortment of graphics method.All graphics are drawn reative to window.This can be a main wi ndow of an applet,a child window of an applet ,or stand alone appliction of window.The origin of each window is at the top-left corner and is 0,0. Coordinates are specified in pixels. All output to a window takes place through a graphics context. 

A graphics context is encapsulated by the Graphics class and is obtained in two ways:

• It is passed to a method, such as paint( ) or update( ), as an argument.

• It is returned by the getGraphics( ) method of Component.

The Graphics class defines a number of drawing functions.Each shape can be drawn edge only or filled .several drawing methods are:

  • Drawing Lines

      Lines are drawn by means of the drawLine() method,shown here:

                               void drawLine(int startX,int startY,int endX,int endY)

simple applet program to drawn a line


import java.applet.*;
import java.awt.*;
public class DrawingLines extends Applet
 {
   int width, height;
   public void init()
 {
      width = getSize().width;
      height = getSize().height;
      setBackground( Color.black );
   }
   public void paint( Graphics g ) 
{
      g.setColor( Color.green );
      for ( int i = 0; i < 10; ++i ) 
{
         g.drawLine( width, height, i * width / 10, 0 );
      }
   }
}

Wednesday, 26 February 2014

CREATING A WINDOWED PROGRAM

How to  Create an AWT-based application.

Creating applets is the most commom use for java's AWT
But,it is possible to create stand-alone AWT based applications to do this

  • simply create an instance of the window or windows you need inside main()
  • e.g. create frame window with in main()

The example program is  shown below:

// Create an AWT-based application.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
// Create a frame window.
public class AppWindow extends Frame 
{
String keymsg = "This is a test.";
String mousemsg = "";
int mouseX=30, mouseY=30;
public AppWindow() 
{
addKeyListener(new MyKeyAdapter(this));
addMouseListener(new MyMouseAdapter(this));
addWindowListener(new MyWindowAdapter());
}
public void paint(Graphics g) {
g.drawString(keymsg, 10, 40);
g.drawString(mousemsg, mouseX, mouseY);
}

CREATING A FRAME WINDOW IN AN APPLET

How to create a child frame window from within an applet...........

Creating a new frame window from within an applet is actually quite easy.
The following steps may be used to do it,
  • Create a subclass of Frame
  • Override any of the standard window methods,such as init(),start(),stop(),and paint().
  • Implement the windowClosing() method of the windowlistener interface,calling setVisible(false)when the window is closed
  • Once you have defined a Frame subclass,you can create an object of that class.But it will note be initially visible
  • When created,the window is given a default height and width
  • You can set the size of the window explicitly by calling the setSize() method
The example program is  shown below:

Tuesday, 25 February 2014

WORKING WITH FRAME WINDOWS

HOW TO MAKE FRAMES(MAIN WINDOW)


How to Creating a frame window in an applet

   Creating a new frame window from within an applet is actually quite easy. First, create a subclass of Frame. Next, override any of the standard applet methods, such as init( ), start( ), and stop( ), to show or hide the frame as needed. Finally, implement the windowClosing( ) method of the WindowListener interface, calling setVisible(false) when the window is closed.After the applet, the type of window you will most often create is derived from Frame.

Frame’s constructors are as follows:

Frame( )
Frame(String title)

    The first form creates a standard window that does not contain a title.The second form creates a window with the title specified by title. We cannot specify the dimensions of the window. We must set the size of the window after it has been created. Some of the methods used when working with windows are as follows:

SETTING THE WINDOW'S DIMENSIONS:

The setSize( ) method is used to set the dimensions of the window.

Its signature is shown here:

                       void setSize(int newWidth, int newHeight)
                       void setSize(Dimension newSize)

EVENT CLASSES:WindowEvent Class

WindowEvent is a subclass of ComponentEvent.....................

The WindowEvent Class


              A low-level event that indicates that a window has changed its status. This low-level event is generated by a Window object when it is opened, closed, activated, deactivated, iconified, or deiconified, or when focus is transfered into or out of the Window.The event is passed to every WindowListener or WindowAdapter object which registered to receive such events using the window's addWindowListener method. (WindowAdapter objects implement the WindowListener interface.) Each such listener object gets this WindowEvent when the event occurs.

TheWindowEvent class: integer constants


  • WINDOW_ACTIVATED           : The window was activated.
  • WINDOW_CLOSED                 :The window has been closed.
  • WINDOW_CLOSING                 : The user requested that the window be closed.
  • WINDOW_DEACTIVATED       :The window was deactivated.
  • WINDOW_DEICONIFIED          :The window was deiconified.
  • WINDOW_GAINED_FOCUS    :The window gained input focus.
  • WINDOW_ICONIFIED                :The window was iconified.
  • WINDOW_LOST_FOCUS         :The window lost input focus.
  • WINDOW_OPENED                   :The window was opened.
  • WINDOW_STATE_CHANGED :The state of the window changed.

 

WindowEvent:- constructors. 


  • WindowEvent(Window src, int type)
  • WindowEvent(Window src, int type, Window other)
  • WindowEvent(Window src, int type, int fromState, int toState)
  • WindowEvent(Window src, int type, Window other, int fromState, int toState)

         Here,src is a reference to the object that generated this event. other specifies the

WINDOW FUNDAMENTALS

SOME JAVA FUNDAMENTALS OF THE AWT

Concept of window and advanced components in java


   The AWT defines windows according to a class hierarchy that adds functionality and specificity with each level. The two most common windows are those derived from Panel, which is used by applets, and those derived from Frame, which creates a standard window. Much of the functionality of these windows is derived from their parent classes.

Component


         At the top of the AWT hierarchy is the Component class. Component is an abstract class that encapsulates all of the attributes of a visual component. All user interface elements that are displayed on the screen and that interact with the user are subclasses of Component. It defines over a hundred public methods that are responsible for managing events, such as mouse and keyboard input, positioning and sizing the window, and repainting. A Component object is responsible for remembering the current foreground and background colors and the currently selected text font.

EVENT CLASS:TextEvent class

Instances of this class describe text events.........

The TextEvent Class


           Instances of this class describe text events. These are generated by text fields and text areas when characters are entered by a user or program.TextEvent defines the integer constant TEXT_VALUE_CHANGED.

TextEvent Class: constructor

         
       TextEvent(Objectsrc, int type)

            Here,src is a reference to the object that generated this event. The type of the event is specified by type.

Monday, 24 February 2014

INTRODUCTION TO THE AWT

WHAT ARE AWT CLASSES IN JAVA


A description of Java's user interface toolkit

The Abstract Window Toolkit(AWT) contains numerous classes and methods that allow you to create and manage windows. Although the main purpose of the AWT is to support applet windows, it can also be used to create stand-alone windows that run in GUI environment, such as windows.The AWT classes are contained in the 'java.awt' package. Fortunately, because it is logically organized in a top-down,hierarchical fashion, it is easier to understand and use.

EVENT CLASSES:MouseWheelEvent class

The MouseWheelEvent class encapsulates a mouse wheel event.........

The MouseWheelEvent Class:

        
The MouseWheelEvent class encapsulates a mouse wheel event. It is a subclass of MouseEvent. Not all mice have wheels. If a mouse has a wheel, it is located between the left and right buttons. Mouse wheels are used for  scrolling.

MouseWheelEvent :- integer constants:

  • WHEEL_BLOCK_SCROLL :      A page-up or page-down scroll event occurred.
  • WHEEL_UNIT_SCROLL      :          A line-up or line-down scroll event occurred.

EVENTLISTENER INTERFACES IN JAVA

THE JAVA LISTENER INTERFACES AND THEIR METHODS

Provides interfaces and classes for dealing with different types of events fired by AWT components.

An event listener registers with an event source to receive notifications about the events of a particular type.
Various event listener interfaces defined in the java.awt.event package are given below :


EVENT CLASSES:MouseEvent Class

 KeyEvent and MouseEvent are subclasses of abstract InputEvent class..................

The MouseEvent Class

                    KeyEvent and MouseEvent are subclasses of abstract InputEvent class. Both these events are generated by objects of type Component class and its subclasses.The MouseEvent is generated when the user presses the mouse or moves the mouse.


The MouseEvent class defines the following integer constants:


  • MOUSE_CLICKED    :     The user clicked the mouse.
  • MOUSE_DRAGGED  :     The user dragged the mouse.
  • MOUSE_ENTERED   :     The mouse entered a component.
  • MOUSE_EXITED        :     The mouse exited from a component.
  • MOUSE_MOVED        :     The mouse moved.
  • MOUSE_PRESSED   :     The mouse was pressed.
  • MOUSE_RELEASED:    The mouse was released.
  • MOUSE_WHEEL        :    The mouse wheel was moved.


 MouseEvent constructors:


MouseEvent(Componentsrc, int type, long when, int modifiers,int x, int y, int clicks, boolean triggersPopup)