A Simple Applet Applets and applications An applet

  • Slides: 34
Download presentation
A Simple Applet

A Simple Applet

Applets and applications • An applet is a Java program that runs on a

Applets and applications • An applet is a Java program that runs on a web page – Applets can be run from: • Internet Explorer • Netscape Navigator (sometimes) • appletviewer • An application is a Java program that runs all by itself

Packages and classes • Java supplies a huge library of pre-written “code, ” ready

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

The Applet class • To create an applet, you must import the Applet class

The Applet class • To create an applet, you must import the Applet class – This class is in the java. applet package • The Applet class contains code that works with a browser to create a display window • Capitalization matters! – applet and Applet are different names

Importing the Applet class • Here is the directive that you need: import java.

Importing the Applet class • Here is the directive that you need: import java. applet. Applet; • • • import is a keyword java. applet is the name of the package A dot (. ) separates the package from the class Applet is the name of the class There is a semicolon ( ; ) at the end

The java. awt package • “awt” stands for “Abstract Window Toolkit” • The java.

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 • If it’s drawn on the screen, then java. awt is probably involved!

Importing the java. awt package • Since you may want to use many classes

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

C and C++ programmers only • C and C++ have an #include directive that

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 include directives!

The applet so far import java. applet. Applet; import java. awt. *;

The applet so far import java. applet. Applet; import java. awt. *;

Comments • A comment adds information for the reader • Java ignores everything inside

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 */

Classes • In Java, all code occurs in classes – Except for the package

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

Your first class public class Drawing extends Applet { …the code for your class

Your first class public class Drawing extends Applet { …the code for your class goes in here… } • public says your class is not hidden – This makes your class visible to Blue. J – We will talk later about why we hide code • class says we are making a class (Duh!)

Your first class, part 2 public class Drawing extends Applet { … } •

Your first class, part 2 public class Drawing extends Applet { … } • Drawing is the name of your class – Class names should always be capitalized • extends Applet says that our Drawing is a kind of Applet, but with added capabilities – Java’s Applet just makes an empty window – We are going to draw in that window

Your first class, part 3 public class Drawing extends Applet { …the code for

Your first class, part 3 public class Drawing extends Applet { …the code for your class goes in here… } • The braces, { }, mark the beginning and ending of your code

The applet so far import java. applet. Applet; import java. awt. *; // CIT

The applet so far import java. applet. Applet; import java. awt. *; // CIT 591 example public class Drawing extends Applet { …we still need to put some code in here. . . }

Methods • A method is a group of commands that tell the computer to

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

The paint method • Our applet is going to have a method to paint

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

The paint method, part 2 public void paint(Graphics g) { … } • public

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 • The argument (there’s only one) is inside parentheses • The method’s commands are inside braces

By the way…names • ( ) are parentheses • { } are braces •

By the way…names • ( ) are parentheses • { } are braces • [ ] are brackets

The paint method, part 3 public void paint(Graphics g) { … } • A

The paint method, part 3 public void paint(Graphics g) { … } • A Graphics is something 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, and it remembers what you have painted

Classes and objects • A class is a description of some objects • An

Classes and objects • A class is a description of some objects • An object is a member of a class – The type of an object is the class it belongs to – Classes are more abstract than objects • If I have a dog named Fido, I can’t pet “dog, ” but I can pet “Fido” – Fido is an object of type Dog

The paint method, part 4 public void paint(Graphics g) { … } • g

The paint method, part 4 public void paint(Graphics g) { … } • g is the parameter, or argument – we could use any name we wanted for it – but it should not begin with a capital letter • The type of a name tells what kind of thing the name refers to • Graphics g says g is an object of type Graphics • We can paint on g

The applet so far import java. applet. Applet; import java. awt. *; // CIT

The applet so far import java. applet. Applet; import java. awt. *; // CIT 591 example public class Drawing extends Applet { public void paint(Graphics g) { …we still need to put some code in here… } }

Colors • The java. awt package defines a class named Color • There are

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. LIGHT_GRAY Color. WHITE Color. PINK Color. RED Color. ORANGE Color. YELLOW Color. MAGENTA Color. GREEN Color. CYAN Color. BLUE • For compatibility with older programs (before the naming conventions were established), Java also allows color names in lowercase: Color. black, Color. dark. Gray, etc.

New colors • Every color is a mix of red, green, and blue •

New colors • Every color is a mix of red, green, and blue • You can make your own colors: new Color( red , green , blue ) • Amounts range from 0 to 255 • Black is (0, 0, 0), white is (255, 255) • We are mixing lights, not pigments • Yellow is red + green, or (255, 0)

Setting a color • To use a color, we tell our Graphics g what

Setting a color • To use a color, we tell our Graphics g what color we want: g. set. Color(Color. RED); • g will remember this color and use it for everything until we tell it some different color

The paint method so far public void paint(Graphics g) { g. set. Color(Color. BLUE);

The paint method so far public void paint(Graphics g) { g. set. Color(Color. BLUE); …draw a rectangle… g. set. Color(Color. RED); …draw another rectangle… } }

Pixels • A pixel is a picture (pix) element – one pixel is one

Pixels • A pixel is a picture (pix) element – one pixel is one dot on your screen – there are typically 72 to 90 pixels per inch • java. awt measures everything in pixels

Java’s coordinate system (0, 0) (50, 0) (0, 20) (50, 20) (w-1, h-1) •

Java’s coordinate system (0, 0) (50, 0) (0, 20) (50, 20) (w-1, h-1) • • • Java uses an (x, y) coordinate system (0, 0) is the top left corner (50, 0) is 50 pixels to the right of (0, 0) (0, 20) is 20 pixels down from (0, 0) (w - 1, h - 1) is just inside the bottom right corner, where w is the width of the window and h is its height

Drawing rectangles • There are two ways to draw rectangles: • g. draw. Rect(

Drawing rectangles • There are two ways to draw rectangles: • g. draw. Rect( left , top , width , height ); • g. fill. Rect(left , top , width , height );

The complete applet import java. applet. Applet; import java. awt. *; // CIT 591

The complete applet import java. applet. Applet; import java. awt. *; // CIT 591 example public class Drawing extends Applet { public void paint(Graphics g) { g. set. Color(Color. BLUE); g. fill. Rect(20, 50, 30); g. set. Color(Color. RED); g. fill. Rect(50, 30, 50, 30); } }

Some more java. awt methods • • • g. draw. Line( x 1 ,

Some more java. awt methods • • • g. draw. Line( x 1 , y 1 , x 2 , y 2 ); g. draw. Oval( left , top , width , height ); g. fill. Oval( left , top , width , height ); g. draw. Round. Rect( left , top , width , height ); g. fill. Round. Rect( left , top , width , height ); g. draw. Arc( left , top , width , height , start. Angle , arc. Angle );

Drawing Strings • A String is a sequence of characters enclosed in double quote

Drawing Strings • A String is a sequence of characters enclosed in double quote marks – "Hello, World!" • A double quote mark in a String must be preceded by a backslash ( ) – "He said, "Please don't go. "" • g. draw. String( string , x , y );

The End

The End