Intro to Applets - Brookwood High School

Download Report

Transcript Intro to Applets - Brookwood High School

Intro to Applets
August 19, 2008
Mrs. C. Furman
Java Applets vs. Java Applications
• Java Applet: a program that is
intended for use on the web.
• Java Applications: a stand-alone
program that is executed using an
interpreter.
Java Applets
• The first kind of executable program that could
•
•
•
be retrieved using www software.
It is compiled into bytecode before being used
on the web.
Applets do Not have a main method!!
The browser which executes the applet is
already running and the applet can then be
thought of as part of this larger program.
Import statements
• 2 import statements needed:
– import java.applet.Applet;
– import java.awt.*;
• The class which is defines an applet
extends the applet class. (Inheritance)
• Inheritance – getting code from a related
class.
paint method
• paint is automatically invoked by the
applet
• paint is called when graphic elements of
the applet need to be painted on the
screen.
• We will be overridding the paint method.
Drawing Shapes
• Graphics Class
• Defined in the awt package
• That is why we
– import java.awt.*;
• contains methods for us to draw shapes
such as lines, rectangles and ovals.
• Java API
Create your own colors
Color brookwoodMaroon =
new Color (100, 20, 20);
• Color brookwoodMaroon – declaring side of the
statement. Declares brookwoodMaroon as an
identifier.
• new Color (100, 20, 20) – creates the actual
Color object.
g.setColor (brookwoodMaroon);
• g is a Graphics object
• object.methodName – to call methods.
AppletViewer Code
import javax.swing.*;
import java.awt.Dimension;
public class AppletViewer extends JFrame {
public AppletViewer() {
super("Applet Viewer");
setResizable(false);
setDefaultCloseOperation(1);
<AppletName> applet = new<AppletName>(); // change line to include your applet
//name
setPreferredSize(new Dimension(400,300));
getContentPane().add(applet);
pack();
setVisible(true);
applet.init();
}
}
public static void main(String[] args) {
new AppletViewer();
}
Applet Code
/** author @ Mrs. C. Furman – change name here
* date: 8/19/2008
* This program with draw an autumn scene – change description here.
*/
import java.applet.Applet;
import java.awt.*;
public class AppletName extends Applet //Change the AppletName to something that describes your scene
{
public void init() {
setSize(400,300);
}
public void paint (Graphics g)
{
}
}
//type your methods here!!!