DOC PREVIEW
Penn CIT 591 - A Simple Applet

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 34 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

A Simple AppletApplets and applicationsPackages and classesAWT and SwingThe Applet and JApplet classesImporting the JApplet classThe java.awt packageImporting the java.awt packageC and C++ programmers onlyThe applet so farCommentsClassesYour first classYour first class, part 2Your first class, part 3Slide 16MethodsThe paint methodThe paint method, part 2By the way…namesThe paint method, part 3Slide 22ColorsNew colorsSetting a colorThe paint method so farPixelsJava’s coordinate systemDrawing rectanglesDrawing stringsThe complete appletSome more java.awt methodsThe HTML pageThe End1A Simple Applet2Applets 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 itself3Packages 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 package4AWT 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)5The 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 names6Importing 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 directives7The 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!8Importing 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 directives9C 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!10The applet so farimport javax.swing.JApplet;import java.awt.*;11Comments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 extractedfrom your program and used in documentation */12Classes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 soon13Your 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!)14Your first class, part 2 public class Drawing extends JApplet { …}Drawing is the name of your classClass names should always be capitalizedextends JApplet says that our Drawing is a kind of JApplet, but with added capabilities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 JApplet15Your 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 code16The applet so farimport javax.swing.JApplet;import java.awt.*;// CIT 591 examplepublic class Drawing extends JApplet { …we still need to put some code in here...}17Methods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 result18The 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 result19The 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 braces20By the way…names ( ) are parentheses { } are braces [ ] are brackets21The paint method, part 3public void paint(Graphics g) { … }A Graphics (short for “Graphics


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
Download A Simple Applet
Our administrator received your request to download this document. We will send you the file to your email shortly.
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 2 2 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?