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