import
java.applet.Applet;
import
java.awt.*;
public
class rect extends Applet
{
public void paint (Graphics g)
{
//Program code goes here
g.setColor (Color.blue);
g.drawRect (200, 200, 100, 100);
}
}
g.setColor(Color.red);
Changes
the current color to whatever is specified.
Writes
Hello World in the current colour at position 75, 100 on the screen.
Draw
a rectangle starting at position 1, 1. It goes on for 200 pixels right and
400 pixels down. It is not filled and it is drawn in the current colour.
As
above but it fills in the rectangle with the current colour.
Draws
an oval that in a box that starts at 50,50 and that has goes in the x direction
20 pixels and the y direction 20 pixels. It is drawn in the current colour.
As
above but it fills in the oval with the current colour.
g.fillPolygon(Xarray,
Yarray, NumPoints);
Makes a polygon based on the co-ordinates in Xarray and Yarray. See triangle example below.
You already know this
mode, we used it in unit 1:
import java.io* when you are using it to pull in the methods that handle input and output
Use System.out.print(“hi”) or System.out.println(“hello”) to print. The first one stays on the line to add more, the second line prints its message and moves to a new line.
Put the Keyin class in the same folder as the file that you are working with. It has code written to handle input. We use the Keyin class instead of writing it ourselves because Java’s input is a little awkward. Whenever you input something, it can throw an IOException and that is still something we aren’t good at handling.
To Input Strings (prompt is in the brackets):
String c = IBIO.inputString(“Please enter your name”);
To Input Integers (prompt is in the brackets):
int j = IBIO.inputInt(“Please enter the year”);
There is only text in this mode. We cannot clear the screen, we cannot draw pictures.
Applets are ‘little applications’.
You can post them on your webpage.
To
get one:
(Blue
Jay) Click on the New Class Button and choose the Applet class type radio
button
(Ready
to Program) File -> New -> Applet Boilerplate
import java.awt.* to get widgets (buttons, textfields, etc)
import java.swing.* to get the code for applets.
Applets
need extends Applet on the end of the class declaration line.
public void init() gets the interface set up and ready to draw. Init is called once when the program starts running.
public void paint (Graphics g) handles the drawing of pictures and shapes.
public void actionPerformed (Event e) is called once every time the user does something, like click on a button.