JApplets

 

Graphics (drawing on the screen and mouse interaction) will involve a slightly different set of enclosing Java code. 
Graphical programs will run as a
 Java applet.

import javax.swing.JApplet;Happy Face applet output

import java.awt.Graphics;

public class HappyFace extends JApplet

{

    public void paint (Graphics canvas)

    {

        canvas.drawOval (100, 50, 200, 200);

        canvas.fillOval (155, 100, 10, 20);

        canvas.fillOval (230, 100, 10, 20);

        canvas.drawArc (150, 160, 100, 50, 180, 180);

    }

}

The blue text show different code from an application. The variables in red are your choice.

The paint method is provided instead of, or in addition to, the main method. It is called ("invoked" or "executed") to draw the graphic. It can be called repeatedly.

The canvas object represents a drawable area on the screen.

 

 

import javax.swing.JApplet;

import java.awt.Graphics;

public class HappyFace extends JApplet

{

    public void paint (Graphics canvas)

    {

        canvas.drawOval (100, 50, 200, 200);

        canvas.fillOval (155, 100, 10, 20);

        canvas.fillOval (230, 100, 10, 20);

        canvas.drawArc (150, 160, 100, 50, 180, 180);

    }

}

 

Sample output:

 

The screen coordinate system

Screen Coordinate SystemNot quite the same as the mathematical x-y coordinate system. The origin (0,0) is in the upper left corner.

x is the horizontal position, increasing left to right.

y is the vertical position, increasing top to bottom--not from the bottom like an x-y graph.

Positions are in terms of pixels. A medium resolution full screen is typically 1024x768 pixels.


Drawing methods

These "draw" are void methods in that they return no value. You just use them as a stand alone statement.

They take several parameters. The parameters can be constants OR variables, or even calculations--as long as you provide the expected type of number or data type.

canvas.drawRect(50, 75, 40, 80); draws a rectangle whose upper left corner is 50 pixels in from the left and 75 pixels from the top and is sized as 40 pixels wide and 80 pixels hight. So the general form is canvas.drawRect(x,y,width,height); // all integer parameters

canvas.drawOval(100, 50, 90, 50); draws an oval inside a "bounding" rectangle whose upper left corner is 100,50 and is 90 pixels wide and 50 pixels high. So the general form iscanvas.drawOval(x,y,width,height);

drawOval details

canvas.fillOval(155, 100, 10, 20); draws a filled-in oval inside a bounding rectangle exactly like drawOval.

canvas.fillRect(x,y,width,height);

canvas.drawLine(x1,y1, x2,y2); // draw a line from point x1,y1 to point x2,y2

canvas.drawLine(x1,y1, x1+100,y2-100); // draw a line from point x1,y1 to a point 100 pixels further to the right and 100 pixels up

canvas.drawArc(150, 160, 100, 50, 180, 180); draws a unclosed arc, where the first four arguments are the same as drawOval and the last two arguments indicate where the arc starts and number of degrees it sweeps through. 0 degrees is horizontal and to the right.

drawArc details

more drawArc details

import javax.swing.JApplet;

import java.awt.Graphics;

/**

Applet that displays a happy face.

Author: Jane Q. Programmer.

Revision of Listing 1.2.

*/

public class HappyFace extends JApplet

{

    public static final int FACE_DIAMETER = 200;

    public static final int X_FACE = 100;

    public static final int Y_FACE = 50;

    public static final int EYE_WIDTH = 10;

    public static final int EYE_HEIGHT = EYE_WIDTH*2;

    public static final int X_RIGHT_EYE = 155;

    public static final int Y_RIGHT_EYE = 95;

    public static final int X_LEFT_EYE = 230;

    public static final int Y_LEFT_EYE = Y_RIGHT_EYE;

    public static final int MOUTH_WIDTH = 100;

    public static final int MOUTH_HEIGHT = 50;

    public static final int X_MOUTH = 150;

    public static final int Y_MOUTH = 175;

    public static final int MOUTH_START_ANGLE = 180;

    public static final int MOUTH_EXTENT_ANGLE = 180;

    public void paint (Graphics canvas)

    {

            //Draw face outline:

        canvas.drawOval (X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);

            //Draw eyes:

        canvas.fillOval (X_RIGHT_EYE, Y_RIGHT_EYE, EYE_WIDTH, EYE_HEIGHT);

        canvas.fillOval (X_LEFT_EYE, Y_LEFT_EYE, EYE_WIDTH, EYE_HEIGHT);

            //Draw mouth:

        canvas.drawArc (X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT,

                MOUTH_START_ANGLE, MOUTH_EXTENT_ANGLE);

    }

}

 

Useful Links for further reading

http://chortle.ccsu.edu/java5/index.html#36

 

http://software.it168.com/manual/java/html/ch29lev1sec3.html

 

http://saturn.cs.unp.ac.za/~robd/javaslides/ch06/ch06.01.ho.pdf

 

Link for very good java tutorial:

http://www.tutorialspoint.com/java/java_applet_basics.htm