• Log in or sign up for an account here: https://www.sythe.org/login

[TUT] Making a Basic Java Applet

crapkiller

Forum Addict
Joined
Jul 19, 2006
Sythe Gold
0
Messages
426
Ok, lets get started.

1) Imports

These are the most commonly used imports for java applets

Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

The first and second one are mandatory.

In this tutorial, we will not do anything that uses the last one, but it is very common. Some things the last one is used for is making buttons that do things when clicked, listening for mouse clicks, listening for mouse motion etc.

2) Class

The next line of business is the class.

Code:
public class name extends Applet

- The first word public is simple, all it means is that other classes can access the information in our class.

- The second word just tells the compiler that this is a class.

- The third word is the name of the java applet. I named mine name.

- The fourth word means that this class is what ever follows (In this case it says this class is an Applet.)

- The fifth word is what tells the compiler that this should be an applet.

3) paint

Code:
public void paint (Graphics g)

- This is a function that paints things onto our applet. (Words, lines, boxes, etc.)

- The word void means that it doesn't return a value.

- The word paint is the name of the function

- This function takes one parameter. This parameter is a type called Graphics.

- We are naming it g

4) Things to draw

Code:
g.drawString("The text you want to draw", x, y);

- This draws words on our applet.

- The first parameter is what ever words you want to draw on the form(In quotes)

EX:
Code:
g.drawString("Hello World", 20, 20);
-----------------------------------------------------------------
Code:
g.drawLine(x1, y1, x2, y2);

- This draws a line on our applet.

- x1 is the x coordinate of where the line starts.
- y1 is the y coordinate of where the line starts.

- x2 is the x coordinate of where the line ends.
- y2 is the y coordinate of where the line ends.

EX:
Code:
g.drawLine(10, 10, 40, 40);
-----------------------------------------------------------------
Code:
g.drawRect(x, y, width, height);

- This draws a rectangle on our applet

- x is the x coordinate of the upper left corner of the box
- y is the y coordinate of the upper left corner of the box

- width is the width of the box
- height is the height of the box;

EX:
Code:
g.drawRect(20, 20, 40,40);
-----------------------------------------------------------------
Code:
g.fillRect(x, y, width, height);

- This fills a rectangle.

- x is the x coordinate of where to start filling
- y is the y coordinate of where to start filling

- width is the width of what to fill
- height is the height of what to fill

EX:
Code:
g.fillRect(20, 20, 40, 40);

5) Compiling

This is what you should have so far. NOTE: The str, x, y, x1, x2, y1, y2, width, height etc. should all be substituted for a number (With the exception of str, substitute that with something in quotes.

Code:
import java.awt.event.*;
import java.awt.*;
import java.applet.*;

public class name extends Applet{

	public void paint (Graphics g){
		g.drawString(str, x, y)
		
		g.drawLine(x1, y1, x2, y2);
		
		g.drawRect(x, y, width, height);
		
		g.fillRect(x, y, width, height);
	}

}

NOTE: If you are using a program like Eclipse then skip this and just hit run java applet.

Now save that as name.java

NOTE: substitute name for the what ever you named your applet(Keep the .java)

Open up command prompt

In command prompt navigate to the folder where your .java is.

type javac name.java

as usual, substitute name for the name of your applet

Wait a few seconds and you should get a .class file

6) Running

NOTE: if you are using a program like Eclipse then just skip this and hit run java applet

First, open up notepad and before you type anything, save it as whatever.html

Second, put this in notepad

Code:
<html>
<body>
<applet code = "name.class" width = 500 height = 500></applet> // NOTE: you can change the width and the height if you want.
</body>
</html>

NOTE: Substitute name for what ever you named your applet (Keep the .class part)

Save it.

Now you can close notepad, and then click on the file you just made.

It should open up your browser and your applet should load up.

VIOLA!

Some potential problems:

Q: My web browser opens but my applet doesnt load.

A: Make sure your applet and your html are in the same folder. Also make sure the code you have in your applet matches mine. (Again excluding the variables)

Enjoy!
 
Why use Graphics to draw text when you can use an AWT Label?
 
Cheers it was very useful.
 
Thanks very usefull :)
 
Thanks, think you could expand this to a Java basics tutorial? I'm starting to learn it and a list of commands etc would be great :D
 
Wow thx this is really cool.
 
Nice, easy to follow tutorial
 
Nice, very easy to follow ^ Sorry for the duplicate comment ^
Worth reading.
 
very nice... even i learned somthing new..... and im a pro at this lol
 
Thanks for the tutorial, even if it's long to read, it still is very useful!

Thanks alot
 
nice tutorial im glad i read
 
Runescape Gamblingrunescape gambling
Back
Top