|
||||||||||||||
. . . . . |
Java Applets Tutorial
| Intro |
Begin |
if/else |
Loops |
Arrays |
Graphics |
Animation |
Mouse |
Game |
Real |
Methods |
Class |
Class 2 |
Applet |
MouseClick |
Java Applets are run inside Web pages and are used on the internet. They dont have a main method so they are not applications, they are web applets. Different.
Well let's start by doing an Applet and then explaining what goes on. Type this into your text editor:
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet
{
// init method is used to initialise any
// variables, objects
public void init()
{
// empty for now
}
// paint does what it says
// needed
public void paint(Graphics g)
{
g.drawString("Hello World", 20, 20);
}
}
Save as HelloWorld.java and then compile. BUT, Java applets must be compiled to Java 1.1,
so we need to compile like this:
javac -target 1.1 HelloWorld.java"-target 1.1" tells the compiler to compile as a Java 1.1 format, file. Note : If you are using Java 1.5 then you dont need to use the target flag. Do it like this : javac HelloWorld.java
How are we going to look at this? We need to make a html file to load our applet. soooo Create a new text file,
save it as hello.htm and put this text in it : <html> <body> <applet code=HelloWorld width=300 height=200> </applet> </body> </html> The bit that loads the java applet into the html page is of course the applet tag. Just put the name of the class after the attribute, "code". Height and width attributes can be used as well if you need. Now open the HTML file and hey presto, the applet is in the web page. Shut down the HTML page before you do any recoding and then re-open.
Now , back to explaining the applet code.
Flash Tutorials in Video Format -
Watch them now at LearnFlash.com Java Colors
Colors for drawing are selected by using the java setColor method. These are the predefined colours.
Usage :
Java Custom Colors
If you want to create your own custom colour, then you can use the Color constructor that takes red, green , and blue
parameters. Like so : Java drawRect and fillRect
Similar to the judo drawing methods, these methods can be used to draw unfilled or filled Rectangles.
Try this new applet :
import java.applet.Applet;
import java.awt.*;
public class RectPaint extends Applet{
// initialise
public void init(){
}
// paint
public void paint(Graphics g){
g.setColor(Color.red);
g.fillRect(20,20,200,100);
g.setColor(Color.yellow);
g.drawRect(40, 40, 160, 60);
}
}
Compile it.And then create your html file to load it.. <html> <body> <applet code=RectPaint width=300 height=200> </applet> </body> </html> Next we will do some more work on the Graphics class. Java Programming Resources |
|
||||||||||||
|
. | Home | Flash MX | Actionscript 2.0 | Flash 3D | Flash 8 | Flash Database | Flash Mobile | Flash CS3 | Java For Kids | Video Course | General Video | Photoshop | Web Design | Digital Photography | Games | free backgrounds | Resume | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | . . |
||||||||||||||