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 applicationsAn applet is a Java program that runs on a web pageApplets can be run within any modern browserTo run modern Java applets, old browsers need an up-to-date Java pluginappletviewer is a program that can run appletsWhen you download the Java SDK, appletviewer comes with itappletviewer is always up-to-date with your Java systemAn application is a Java program that runs all by itself3Packages and classesJava supplies a huge library of pre-written “code,” ready for you to use in your programsCode is organized into classesClasses are grouped into packagesOne way to use this code is to import itYou can import a single class, or all the classes in a package4AWT and SwingThere are two ways to build Graphical User Interfaces (GUIs):AWT, the “Abstract Window Toolkit,” is the original waySwing is the new way (since Java 1.2)Swing is built “on top of” the AWTWhen you build an AWT GUI, you use only AWT methodsWhen you build a Swing GUI, you typically use both AWT methods and Swing methodsHowever, there are some incompatibilities you will need to learn about (later)5The Applet and JApplet classesTo 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 packageThe JApplet class is in the javax.swing packageBoth Applet and JApplet contain code that works with a browser to create a display windowWe will use Swing (hence, JApplet) in this exampleCapitalization matters! Japplet and JApplet are different names6Importing the JApplet classHere is the directive that you need: import javax.swing.JApplet;import is a keywordjavax.swing is the name of the packageA dot ( . ) separates the package from the classJApplet is the name of the classThere is a semicolon ( ; ) at the endThis directive should be before your class definitionsOnly 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 shapesDrawing lettersSetting colorsChoosing fontsSwing builds on the AWT, it doesn’t replace itIf it’s drawn on the screen, then java.awt is probably involved!8Importing the java.awt packageSince 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 onlyC and C++ have an #include directive that copies a library function into your programThis makes your program biggerJava’s import gives you access to the libraryIt does not make your program biggerIt’s OK to use lots of import directives!10The applet so farimport javax.swing.JApplet;import java.awt.*;11CommentsA comment adds information for the readerJava ignores everything inside commentsThere 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 */12ClassesIn Java, all code occurs in classesExcept for the package and import directivesWe will talk about package some dayThe code that you import is in classesYour code will also be in classesFor now, a class is a bundle of codeWe 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 hiddenThis makes your class visible to “the whole world”We will talk later about why we hide codeclass says we are making a class (Duh!)14Your first class, part 2 public class Drawing extends JApplet { …}Drawing is the name of your classClass names should always be capitalizedextends JApplet says that our Drawing is a kind of JApplet, but with added capabilitiesJava’s JApplet just makes an empty windowWe are going to draw in that windowThe 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...}17MethodsA method is a group of commands that tell the computer to do somethingC programmers: methods are similar to functionsA method takes information in, does something with it, and returns a resultThe input information is called the method’s parameters, or argumentsThe result is just called a result18The paint methodOur applet is going to have a method to paint some colored rectangles on the screenThis method must be named paintpaint needs to be told where on the screen it can drawThis will be the only parameter it needspaint doesn’t return any result19The paint method, part 2public void paint(Graphics g) { … }public says that anyone can use this methodvoid says that it does not return a resultpaint is the name of the methodMethod names should begin with a lowercase letterThe argument, or parameter (there’s only one) is inside parenthesesThe method’s commands are inside braces20By the way…names ( ) are parentheses { } are braces [ ] are brackets21The paint method, part 3public void paint(Graphics g) { … }A Graphics (short for “Graphics
View Full Document