IntroToApplets

Download Report

Transcript IntroToApplets

IT11 Agenda for Feb 10
1. Golden Rule Reminder.
2. PowerPoint demonstration on the structure of Java
Applet programs.
•
Source Files
•
HTML Files
•
Class Files
3. Quick demo on using Project Builder.
4. Unit 2 Exercises (on page 13). Due 10 minutes after the
beginning of the next class.
Three Files To Consider When Creating
& Running Java Applets
Java Applets source files have extension (end in) “.java”
Example of a Java source file: HelloWorld.java
Java Applet executable files have extension “.class”
Example of a Java executable file: HelloWorld.class
Applets need an applet viewer or browser to run. To place
an Applet on a web page use the Applet HTML Tag.
Example of a Java Applet Source Files
import java.awt.*;
import java.applet.*;
public class program_name extends Applet
{
public void init()
{
Java instructions for setting up the applet will go
here.
}public void paint (Graphics screen)
{
}
}
Java instructions for painting will go here
The init method
public void init()
{
}
The instructions in this method get executed before
any other instructions in your program. For the time
being we will not be placing any instructions inside
this method.
The paint method
public void paint (Graphics screen)
{
Your java paint instructions will go here.
}
The paint method is executed every time your window
needs to be re-drawn. Can you think of a situation
where the paint method would need to be executed?
HTML Files
Use the Applet Tag to insert your Applet into your web-page.
When working from home make sure you place your class file in
the same folder as your HTML page. Below is an example of an
HTML file that contains an Applet that is 300 pixels wide and
200 pixels in height.
<HTML>
<BODY>
<APPLET code=“myApplet.class” width= 300 height=200>
</APPLET>
</BODY>
</HTML>
Pitfall Alert!
The name of your source file must exactly match the name
of your program. For example if you call your Java Project
HelloWorld, Project Builder will automatically call your Java
source file HelloWorld.java. That means your source file
must look like this:
import java.awt.*;
import java.applet.*;
public class HelloWorld extends Applet
{…}
A syntax error will result if you replace the line above with
something like:
public class helloworld extends Applet { … }
Quiz #1 on Friday
Things To Do
1. Read Unit 2 carefully & then answer the Unit Exercises (on
page 13). Don’t forget to fully answer question #3 at the
very bottom of page 13 before handing it in.
2. Read chapter 3 carefully (silent reading).
3. If you finish all of 1 & 2 try Section 3.1 Mastery Questions
on page 18.