Unformatted text preview:

A Simple Applet 1 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run modern Java applets old browsers need an up to date Java plugin appletviewer is a program that can run applets When you download the Java SDK appletviewer comes with it appletviewer is always up to date with your Java system An application is a Java program that runs all by itself 2 Packages and classes Java supplies a huge library of pre written code ready for you to use in your programs Code is organized into classes Classes are grouped into packages One way to use this code is to import it You can import a single class or all the classes in a package 3 AWT and Swing There are two ways to build Graphical User Interfaces GUIs AWT the Abstract Window Toolkit is the original way Swing is the new way since Java 1 2 Swing is built on top of the AWT When you build an AWT GUI you use only AWT methods When you build a Swing GUI you typically use both AWT methods and Swing methods However there are some incompatibilities you will need to learn about later 4 The Applet and JApplet classes To create an applet you must import either the Applet class to write an AWT applet or the JApplet class to write a Swing applet The Applet class in in the java awt package The JApplet class is in the javax swing package Both Applet and JApplet contain code that works with a browser to create a display window We will use Swing hence JApplet in this example Capitalization matters Japplet and JApplet are different names 5 Importing the JApplet class Here is the directive that you need import javax swing JApplet import is a keyword javax swing is the name of the package A dot separates the package from the class JApplet is the name of the class There is a semicolon at the end This directive should be before your class definitions Only the package directive if you have one goes before the import directives 6 The java awt package awt stands for Abstract Window Toolkit The java awt package includes classes for Drawing lines and shapes Drawing letters Setting colors Choosing fonts Swing builds on the AWT it doesn t replace it If it s drawn on the screen then java awt is probably involved 7 Importing the java awt package Since you may want to use many classes from the java awt package simply import them all import java awt The asterisk or star means all classes The import directives can go in any order but must be the first lines in your program unless you have a package directive which must precede your import directives 8 C and C programmers only C and C have an include directive that copies a library function into your program This makes your program bigger Java s import gives you access to the library It does not make your program bigger It s OK to use lots of import directives 9 The applet so far import javax swing JApplet import java awt 10 Comments A comment adds information for the reader Java ignores everything inside comments There are three kinds of comments starts a comment that goes to the end of the line starts a comment that can extend over many lines and ends at is a javadoc comment that can be extracted from your program and used in documentation 11 Classes In Java all code occurs in classes Except for the package and import directives We will talk about package some day The code that you import is in classes Your code will also be in classes For now a class is a bundle of code We will talk about what it really is very soon 12 Your first class public class Drawing extends JApplet the code for your class goes in here public says your class is not hidden This makes your class visible to the whole world We will talk later about why we hide code class says we are making a class Duh 13 Your first class part 2 public class Drawing extends JApplet Drawing is the name of your class extends JApplet says that our Drawing is a kind of JApplet but with added capabilities Class names should always be capitalized Java s JApplet just makes an empty window We are going to draw in that window The only way to make an applet is to extend Applet or JApplet 14 Your first class part 3 public class Drawing extends JApplet the code for your class goes in here The braces mark the beginning and ending of your code 15 The applet so far import javax swing JApplet import java awt CIT 591 example public class Drawing extends JApplet we still need to put some code in here 16 Methods A method is a group of commands that tell the computer to do something C programmers methods are similar to functions A method takes information in does something with it and returns a result The input information is called the method s parameters or arguments The result is just called a result 17 The paint method Our applet is going to have a method to paint some colored rectangles on the screen This method must be named paint paint needs to be told where on the screen it can draw This will be the only parameter it needs paint doesn t return any result 18 The paint method part 2 public void paint Graphics g public says that anyone can use this method void says that it does not return a result paint is the name of the method Method names should begin with a lowercase letter The argument or parameter there s only one is inside parentheses The method s commands are inside braces 19 By the way names are parentheses are braces are brackets 20 The paint method part 3 public void paint Graphics g A Graphics short for Graphics context is an object that holds information about a painting It remembers what color you are using It remembers what font you are using You can paint on it but it doesn t remember what you have painted In this program The type of the parameter is Graphics The name of the parameter is g 21 The applet so far import javax swing JApplet import java awt CIT 591 example public class Drawing extends JApplet public void paint Graphics g we still need to put some code in here 22 Colors The java awt package defines a class named Color There are 13 predefined colors here are their fully qualified names Color BLACK Color DARK GRAY Color GRAY Color BLUE Color LIGHT GRAY Color WHITE Color PINK Color RED Color ORANGE Color GREEN Color CYAN Color YELLOW Color MAGENTA For compatibility with older programs before the naming conventions were established Java also allows color names in lowercase Color black Color darkGray etc 23 New colors Every color is a mix of red green and …


View Full Document

Penn CIT 591 - A Simple Applet

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Loading Unlocking...
Login

Join to view A Simple Applet and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view A Simple Applet and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?