-
October 7th, 2003, 01:58 AM
#1
Java Applet tutorial
A Java Applet tutorial
first of all, you are going to need either an sdk or jdk to make these programs. I recommend
Jbuilder9 from borland. You can get it here http://www.borland.com/products/downloads/. Registration is required though.
For this tut I will be using jcreator and the latest sdk from http://java.sun.com. You can get jcreator from http://www.jcreator.com under downloads. Remember to get the freeware version not the
shareware.
Now that that's out of the way we can get down to the programming aspect of this tutorial.
This tutorial will focus on graphics in java through applets. Let me give you an example:
Code:
import java.awt.*;
public class picture extends java.applet.Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawRect(50,50,150,100);
}
}
drawRect() is built into the import that we use. The first number is the x coordinate of the top
left corner, and the second number is the y coordinate. The third number is the horizontal length
of the sides and the fourth is the vertical length. You could also do fillRect() to fill in the
rectangle.
the setColor() method does what it sounds like it does, it sets the color. you should be able
to figure out on your own how to get other built in colors(Color.blue, etc.). If you want to
customize a color you could type:
g.setColor(new Color(254,235,234));
Everything I put in the parenthesis below should be numbers(ints), I am just describing them with
words.
Other methods are:
(note: you always need the g. before the method. Although it could be anything else as long as
it matches the value in the head of the paint method.
ex. public void paint(Graphics g))
g.drawOval(X,Y,width,height);
g.drawArc(X,Y,width,height,start(ex. 0 as in 0 degrees),degrees to rotate);
g.drawLine(X1,Y1,X2,Y2);
g.drawRoundRect(X,Y,width,height,cornerwidth,cornerheight);
As you can see, with an applet, no main method is required. The paint method kindof acts as the
main method for applets. Save this file as picture.java. To run this applet we will need to make
an html file containing the following text:
<applet code="picture.class" width=400 height=400>
</applet>
You can adjust the width and height as needed.
Save that file as anything as long as it is in the same directory as the java program
above and is saved as a .html. To run this you must first compile picture.java which creates the
picture.class file you see above, which is used to run the applet. You run the program with the
html page not through jcreator. If you double click your .html file it should open up in your
web browser and you will see the applet you just made.
That should get you started with applets, also I wanted to include a little bouncing ball program
I wrote:
Code:
import java.awt.*;
public class bounce extends java.applet.Applet
{
public void paint(Graphics g)
{
int x=0;
int y=0;
int X=0;
int Y=0;
for(int b=1;b<=100;b++)
{
for(int i=1;i<=500;i++)
{
g.setColor(Color.red);
g.fillOval(x,y,50,50);
for(int j=1;j<=150000;j++)
{}
g.setColor(Color.white);
g.fillOval(x,y,50,50);
x++;
y++;
}
for(int a=1;a<=250;a++)
{
g.setColor(Color.red);
g.fillOval(x,y,50,50);
for(int j=1;j<=150000;j++)
{}
g.setColor(Color.white);
g.fillOval(x,y,50,50);
x++;
y-=2;
}
for(int a=1;a<=500;a++)
{
g.setColor(Color.red);
g.fillOval(x,y,50,50);
for(int j=1;j<=150000;j++)
{}
g.setColor(Color.white);
g.fillOval(x,y,50,50);
x--;
y++;
}
for(int a=1;a<=250;a++)
{
g.setColor(Color.red);
g.fillOval(x,y,50,50);
for(int j=1;j<=150000;j++)
{}
g.setColor(Color.white);
g.fillOval(x,y,50,50);
x--;
y-=2;
}
}
}
}
Save this file as bounce.java and compile it.
Here is the .html file:
<applet code="bounce.class" width=800 height=600>
</applet>
I'm not going to explain it but you can fool around with it for fun.
And one more thing, the coordinates are messed up in java. The top left of the applet is (0,0)
and the bottom right is (800,600), therefore making down positive in the applet.
I know this tut isn't perfect, but it is a start for anyone wanting to jump into the world
of java applets.
Hope you all enjoyed and learned from this.
-
October 7th, 2003, 02:13 AM
#2
[gloworange]And then it happened... a door opened to a world... rushing through the phone line like heroin through an addict\'s veins, an electronic pulse is sent out, a refuge from the day-to-day incompetencies is sought... a board is found. \"This is it... this is where I belong...\" I know everyone here... even if I\'ve never met them, never talked to them, may never hear from them again... I know you all...[/gloworange]
-
October 7th, 2003, 02:21 AM
#3
-
October 7th, 2003, 03:54 AM
#4
I would just like to add that you can put 4 of your lines onto one line...
int x=0;
int y=0;
int X=0;
int Y=0;
can be turned into
int x,y,X,Y;
leaving it at that, will initialize the four integers with a value of zero so no typing needed.
for(int j=1;j<=150000;j++)
{}
Not quite sure why you have that for loop there it doesnt seem to do anything but possibly take up time and slow the applet down...perhaps you could give me an explanation on it?
Otherwise nicely done.
<chsh> I've read more interesting technical discussion on the wall of a public bathroom than I have at AO at times
-
October 7th, 2003, 04:48 AM
#5
Yes the loop is just a delay. Otherwise the applet would just be a blur. you have to adjust the count depending on how fast your computer is.
also this doesn't work
int x,y,X,Y;
you get the error variable may not have been initialized.
however, I believe that if you used the variable outside all the for loops, you wouldn't get an error.
you could do this though
int x=0,y=0,X=0,Y=0;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|