Java Programming
Results 1 to 3 of 3

Thread: Java Programming

  1. #1
    Member
    Join Date
    Jan 2003
    Posts
    42

    Java Programming

    What are layouts in java and how does java handle handle mouse movements. can any body please provide some code example about the two topics and suggest some good books on java programming

  2. #2
    Google Google Amazon. Ok. Google for 'Java Programming Tutorial' Go to the SUN website because Java is theirs and they have the tools (compilers) and a good tutorials.

    -Cheers-

  3. #3
    Senior Member
    Join Date
    Nov 2003
    Posts
    285
    I see u are new to java

    Following are the layout managers available with JAVA 2 language:-

    I. Flow Layout:-
    Flow Layout is the default layout manager. Flow Layout implements a simple layout style. Components are laid out from upper left corner, left to right & top to bottom. When no components can fit on a line, next component appears in the next line. A small space is left between each component above and below & on the sides. Flow Layout has following constructors :-

    FlowLayout()
    FlowLayout(int how)
    FlowLayout(int how, int horz, int vert)

    The first form creates the default layout, which centers all the components and leaves a spaces of five pixels between each component. The second one allows you to specify how the components would be aligned. The value for the how variable are:-

    FlowLayout.LEFT
    FlowLayout.CENTER
    FlowLayout.RIGHT

    The third form allows one to specify the alignment as well as the vertical (vert) and the horizontal (horz) spaces between each component.

    Number of components:- There is no limit to the number of components added to the Flow Layout manager.

    How to add components:- The sample program given below shows how to add component. Components are added using add() meyhod.

    Public class FlowLayoutTest extends Applet{


    II. Border Layout:-
    The BorderLayout implements a common layout style for top-level window. It has four narrow, fixed-width component at the edges and one large component in the central area. The four sides are referred to as north, south, east, west & the middle area is called the center. Here are the constructors defined by BorderLayout:-

    BorderLayout()
    BorderLayout(int horz, int vert)

    The first one creates a default layout. The second allows one to specify the vertical and horizontal space between each component in vert & horz respectively.

    III. Grid Layout:-
    GridLayout lays out all the components in a two-dimensional grid. When you instantiate a GridLayout, you define the number of rows and columns. The constructors supported by GridLayout are shown here :

    GridLayout()
    GridLayout(int numRows, int numCols)
    GridLayout(int numRows, int numCols, int horz, int vert)

    The first form creates a single-column grid layout. The second form creates a grid layout with the specified number of rows & columns. The third form allows you to specify the horizontal and vertical space left between components in horz, & vert respectively. Either numRows or numCols can be zero. Specifying numRows as zero allows for unlimited-length columns. Specifying numCols as zero allows for unlimited-length rows.

    IV. Card Layout:-

    The CardLayout class is unique from other classes in the way that it stores several different layouts. Each layout can be thought of as a separate index card in the deck that can be shuffled so that any card is no the top at any given time. This can be useful for user interfaces with optional components that can be dynamically enabled and disabled upon user input. You can prepare the other layout and have them hidden, ready to be activated when needed.

    Card Layout has following constructors :-

    CardLayout()
    CardLayout(int horz, int vert)

    The first form creates a default layout. The second form allows you to specify the horizontal and vertical space left between each component in horz and vert, respectively.
    *******


    Question 2: Describe the following :
     Describe the drawing methods for lines, rectangles, ovals, arcs & polygons.
     Describe the methods to be used to detect mouse movement.


    Answer: The various shapes are drawn using the Graphics class. The various methods used to draw the shapes are as follows:-
     Line:- Lines are drawn using the drawLine( ) method, shown here:

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

    drawLine( ) displays a line in the current drawing color that begins at startX, startY & ends at endX, endY.

    Example:- Graphics g;
    g.drawLine(10,10, 100,200);

     Rectangle:- The methods drawRect( ) & fillRect( ) are used to draw outlined or filled rectangles. The methods drawRoundRect( ) & fillRoundRect( ) can be used to draw outlined & filled rectangles with rounded corners. The constructors for all the 4 methods are as follow:

    void drawRect (int top, int left, int width, int height)
    void fillRect (int top, int left, int width, int height)
    void drawRoundRect (int top, int left, int width, int height, int xDiam, int yDiam)
    void fillRoundRect (int top, int left, int width, int height, int xDiam, int yDiam)

    The upper-left corner of the rectangle is at top, left. The dimensions are given by width & height. XDiam, yDiam specify the diameter of the rounding arc along the x-axis & y-axis respectively.

     Ovals:- To draw a outlined oval we use drawOval( ) method and to draw a filled oval we use fillOval( ) method. The constructors of the both is given below:

    void drawOval(int top, int left, int width, int height)
    void fillOval(int top, int left, int width, int height)

    The oval is drawn within the boundaries of a rectangle whose upper-left corner is defined by top, left and whose dimensions are specified by width, height.



     Arcs:- Arcs can be drawn with drawArc( ) method and filled arcs can be drawn using fillArc( ) method.

    void drawArc(int top, int left, int width, int height, int startAngle, int sweepAngle)

    void fillArc(int top, int left, int width, int height, int startAngle, int sweepAngle)

    The arc is bounded by the rectangle whose upper-left corner is specified by the top, left and whose width & height is specified by width, height. The arc is drawn starting from the startAngle through the angular distance specified by the sweepAngle. Angles specified are in degrees. Zero degree is on the horizontal line on the three o’clock position. The arc is drawn counter-clockwise if the sweepAngle is positive and clockwise if the sweepAngle is negative. Therefore, to draw from 12 o’clock to 9 o’clock the startAngle would be 90 ad sweepAngle would be 180.

     Polygons:- Polygons can be drawn using drawPolygon( ) and fillPolygon( ) methods.

    void drawPolygon(int x[], int y[], int numPoints)
    void fillPolygon(int x[], int y[], int numPoints)

    The polygon’s end points are specified as coordinate pairs in the arrays x, y. the number of points defined by x & y are defined in numPoints. There are alternative forms of these methods in which the polygon is specified by a Polygon object.

    Example:-

    public class HourGlass extends Applet{
    public void paint (Graphics g){
    int xpoints[] = {30, 300, 30, 200,30};
    int ypoints[] = {30, 30, 200, 200, 30};
    int nums = 5;

    g.drawPolygon(xpoints, ypoints, nums);
    }
    }

    -----------------------------------------------------------------------------------------------

    mouse events are generated when the user licks a mouse button or moves a mouse. There are seven mouse event types represented by the constants in the Mouse Event class. These constants are the possible values for a mouse event’s id field. Below is the definition of the MouseEvent class:

    Public class MouseEvents extends InputEvent{

    Public static final int MOUSE_CLICKED;
    Public static final int MOUSE_DRAGED;
    Public static final int MOUSE_ENTERED;
    Public static final int MOUSE_EXITED;
    Public static final int MOUSE_FIRST;
    Public static final int MOUSE_LAST;
    Public static final int MOUSE_MOVED;
    Public static final int MOUSE_PRESED;
    Public static final int MOUSE_RELEASED;

    Public MouseEvent(Component source, int id, long when, int modifiers, int x, int y, int ClickCout, boolean popupTrigger);

    Pulic int getClickCount();
    Public Point getPoint();
    Public int getX();
    Public int getY();
    Pubic boolean isPopupTrigger();
    Public string paramString();
    Public synchronized void translatePoint(int x, int y);
    };


    i am also including a calander program which i made in java 2

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •