CS 177 - Simple Java Applets, Object-Oriented Programming, Graphics
(This material last modified
A simple Java applet
Methtest.java...
import java.applet.Applet;
import java.awt.Graphics;
public class Methtest extends Applet
{
public void paint (Graphics g)
{
int x = 50;
g.drawString("The value is " + String.valueOf(increase(x)),5,25);
}
public int increase (int a)
{
return (a+5);
}
}
Compile the above (javac) to get Methtest.class
hello.html
Hello Applet
Hello Applet
Below is my first Java applet and I am really excited about it....
How do you like it?
import classes from Application Programmer's Interface (API)
import java.awt.Graphics;
class Graphics is to be imported from awt (Abstract Windows Toolkit)
package of java
import java.awt.*;
all classes are to be imported from awt (Abstract Windows Toolkit)
package of java
Reuse of software -- API written by someone else
Inheritance --
public class Methtest extends Applet
Methtest is my class that is a subclass of the superclass (base class)
Applet and inherits all Applet capabilities and methods
"public" is required for all subclasses of Applet
file name must be Methtest.java
public void paint (Graphics g)
Applet method paint called automatically if you supply it
Requires parameter of type Graphics (Graphics object)
Graphics method drawString (String, x, y) draws a string x pixels from
left and y pixels from top of applet
A more interesting Java applet
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Eventtest extends Applet implements ActionListener
{
Label prompt;
TextField input;
int number;
public void init ()
{
prompt = new Label ("Enter a number:");
add (prompt);
input = new TextField (10);
add (input);
input.addActionListener (this);
}
public void actionPerformed (ActionEvent e)
{
number = Integer.parseInt (e.getActionCommand ());
number += 12;
input.setText (Integer.toString (number));
}
}
public class Eventtest extends Applet implements ActionListener
can implement one or more interfaces
must define methods for interface in your class
Label prompt;
TextField input;
int number;
...can be used throughout class Eventtest (instance variables)
method init provided to applet to initialize it
every object of class Applet has empty method init(), start(), paint()
get called in that order
prompt = new Label ("Enter a number:");
add (prompt);
input = new TextField (10);
add (input);
...construct and add Label object and TextField object to applet
input.addActionListener (this);
..."this" applet should listen for events from TextField input
if user presses return key in TextField input, calls method
actionPerformed -- event-driven programming
public void actionPerformed (ActionEvent e)
event handling method
e.getActionCommand () returns String in TextField input
Object-Oriented Programming
Inheritance
Inheritance = class (subclass) created from existing class
(superclass) ... acquiring its data and methods ... and adding new
data and methods
subclass could be superclass for some other class
Account -> CheckingAccount -> StudentCheckingAccount
Direct superclass = one level up
Indirect superclass = two or more levels up
Multiple Inheritance = class is subclass of two or more independent
superclasses
Student -> StudentEmployee <- Employee
Java does NOT allow Multiple Inheritance
Java does allow Interfaces, which have advantages of multiple
inheritance without associated problems
Subclass more specific than superclass,
represents smaller group of objects
Each object of subclass is also an object of superclass
public class Subclass extends Superclass {...}
Graphics
Java's Abstract Windows Toolkit (AWT)
All of these are subclasses of Object:
Color = methods, constants for manipulating applet colors
Font = methods, constants for manipulating type fonts
Polygon = methods for creating polygons
Toolkit = methods for getting graphical information from system
Graphics = methods for drawing strings, lines, shapes
In an applet x,y coordinates denote x pixels from left and y pixels from top of
applet
Every Applet object has an associated Graphics object
Some classes in AWT have Class Component as a superclass
Component method paint uses Applet's associated Graphics object as parameter
public void paint (Graphics g)
Remember that paint() is called automatically after init() and start()
You can call paint() as needed by calling:
public void repaint ()
This clears the applet of "painted" items and calls paint() again
public void drawString (
String string, // string to draw
int x, // x pixels from left
int y) // y pixels from top
x,y coordinate is lower left corner of string
Color class
Color class has predefined Color constants
Color.orange
Color.pink
Color.cyan
Color.magenta
Color.yellow
Color.black
Color.white
Color.gray
Color.lightGray
Color.darkGray
Color.red
Color.green
Color.blue
Can create your own Color constant
public Color (
int r, //0-255 red
int g, //0-255 green
int b) //0-255 blue
Can determine current color for Graphics drawing:
public Color getColor ()
Can set current color for Graphics drawing:
public void setColor (Color c)
Example:
import java.awt.Color;
...
Color hold;
Color alert = new Color (100, 255, 100);
hold = getColor();
setColor (alert);
...
setColor (Color.blue);
Font class
Font class has constants
Font.PLAIN
Font.BOLD
Font.ITALIC
public Font (
String s, // font name
int style, // font style
int size) // font size
public void setFont (Font f);
for sure Serif, Monospaced, SanSerif, Dialog, DialogInput
also usually Helvetica, TimesRoman, Courier
default is Dialog, plain, 12-point
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Font;
public class DemoFont extends Applet
{
private Font font1, font2, font3;
private Font font4, font5, font6;
private Font font7, font8;
public void init()
{
font1 = new Font
( "Serif", Font.BOLD, 12 );
font2 = new Font
("Monospaced",Font.ITALIC,24);
font3 = new Font
( "SansSerif", Font.PLAIN, 14 );
font4 = new Font
( "Dialog", Font.BOLD, 18 );
font5 = new Font
("DialogInput",Font.ITALIC,20);
font6 = new Font
( "Helvetica", Font.PLAIN, 16 );
font7 = new Font
( "TimesRoman", Font.BOLD, 22 );
font8 = new Font
( "Courier", Font.ITALIC, 10 );
}
public void paint( Graphics g )
{
g.setFont( font1 );
g.drawString
( "Serif 12 point bold.", 0, 20 );
g.setFont( font2 );
g.drawString
("Monospaced 24 point italic.",
0, 40 );
...
}
}
Drawing Lines
public void drawLine (
int x1, // x first point
int y1, // y first point
int x2, // x second point
int y2) // y second point
import java.applet.Applet;
import java.awt.Graphics;
public class Line extends Applet
{
public void paint( Graphics g )
{
drawThickLine(g,10,10,230,95);
}
public void drawThickLine
(Graphics g, int ax, int ay, int bx, int by)
{
g.drawLine(ax, ay, bx, by);
g.drawLine(ax+1, ay, bx+1, by);
g.drawLine(ax-1, ay, bx-1, by);
}
}
Drawing Rectangles
public void drawRect (
int x, // top-left x
int y, // top-left y
int width,
int height)
public void fillRect (
int x, // top-left x
int y, // top-left y
int width,
int height)
draws solid rectangle
public void clearRect (
int x, // top-left x
int y, // top-left y
int width,
int height)
draws rectangle using
background color
import java.applet.Applet;
import java.awt.Graphics;
public class RectangleTest
extends Applet
{
public void paint( Graphics g )
{
g.drawRect( 10, 15, 100, 100 );
g.fillRect( 150, 15, 100, 100 );
g.clearRect( 175, 50, 30, 40 );
}
}
Stick Man
import java.applet.Applet;
import java.awt.Graphics;
public class Stick extends Applet
{
public void paint( Graphics g )
{
int i;
for (i=0; i < 256; i=i+48)
{
drawStickMan( g, i, i);
}
}
public void drawStickMan
(Graphics g, int x, int y)
{
g.drawOval (x+24, y+0, 16, 16);
g.drawLine(x+32, y+16, x+32,y+40);
g.drawLine(x+32, y+40, x+16,y+64);
g.drawLine(x+32, y+40, x+48,y+64);
g.drawLine(x+16, y+32, x+48,y+32);
}
}
Move Stick Man
import java.applet.Applet;
import java.awt.*;
public class MoveStick extends Applet
{
public void paint( Graphics g )
{
int i;
int j;
for (i=0; i < 256; i=i+1)
{
drawStickMan( g, i, i);
for (j=0; j < 5000000; j++) {}
g.setColor (Color.white);
drawStickMan( g, i, i);
g.setColor (Color.black);
}
}
public void drawStickMan
(Graphics g, int x, int y)
{}
}