First
Intro to Java: We'll start with Java Applet
Before writing code please take a look at the Java
Style Page and JavaDoc pages.
Java is fun to learn and play and here is the proof.
Your next step is creating a Java Applet. Java Applet is a different (from
application) type of program. Java Applet is a part of a web page, pretty much
like an image on this page. A web browser is the main program that reads web
page, renders images, and also can use its Java library to play Java Applet
code. This is why Java Applet does not have the main()
method. The browser starts applet with the init()
method. In the example below a very simple Java Applet paints its part of a web
page with an image and several lines of Java Poetry. The paint()
method is also called by the browser automatically whenever there is a need to
refresh the screen. You can see that all programmer needs to do for this simple
applet is to fill the init() and paint() methods with
several lines of code. You can find more about Java Applet life and death
habits at Overview of Applets, but you can actually start right here
and explore more applet details later.
You compile Java code with your IDE or with javac
compiler using the command line:
javac AppletExample.java
The result of the compilation (If no errors found) is
the AppletExample.class file. You can place this file
on the web server in the same folder where you have your web page. You can also
put the web page as well as your applet class on your local machine for
testing.
Your web page must have the applet tag, in a very similar way as you would do
this for an image.
<applet code="AppletExample.class"
width=200 height=300> </applet>
Java Applet will be loaded and running in the web browser as soon as a user
will point the browser to your web page. To see HTML source - use right mouse
click View-Source and look for APPLET CODE Html tag. Copy/Paste two lines with
this code into a new file. Save this file as MyApplet.html in the same project/javasource folder. Here is the example of living applet
with its code. Try to modify this code and create another applet.
Create a new Java project in Eclipse, create a new class: AppletExample, then Copy/Paste the code below in the newly created class, make sure you have no red errors
|
// AppletExample
@ http://JavaSchool.com/school/public/awt/
import java.applet.Applet;
import java.awt.*;
// Jeff.Zhuk@JavaSchool.com
public class AppletExample
extends Applet {
private
Image image;
private
String[] javaPoetry = {
"I placed
my JAR", " in
|
Email your questions to your instructor: Jeff.Zhuk@JavaSchool.com