First we start with out imports:
Code: Select all
import java.awt.*;
import java.applet.Applet;//tells the browser its an applet lol
Code: Select all
public class Rectangles extends Applet
{
Code: Select all
public void paint(Graphics g) //you will always need Graphics g so the applet knows its a drawing
{
int left = 5;
int top = 5;
int width = (this.size().width) / 2 - 10; //the width of the rectangles
int height = this.size().height - 10; //the height of the rectangles
drawRects(g, left, top, width, height) ; //obvious what it does
left = width + 15;
drawFilledRects(g, left ,top, width, height); //filles the recangles
}
public void drawRects(Graphics g, int l, int t, int w, int h)
{
while (h > 0)
{
g.drawRect(l, t, w, h);
l += 10; //length
t += 10; //idk lol
w -= 20; //width
h -= 20; //height
}
}
public void drawFilledRects(Graphics g, int l, int t, int w, int h)
{
g.setColor(Color.black); //the colour it sets - try green etc
while (h > 0)
{
g.fillRect(l, t, w, h);
l += 10; //length
t += 10;
w -= 20; //width
h -= 20; //height
if (g.getColor() == Color.white) //the other colour etc
g.setColor(Color.black);
else
g.setColor(Color.white);
}
}
}
Code: Select all
<HTML>
<HEAD>
<TITLE>Rectangles</TITLE>
</HEAD>
<BODY>
<H1>A test page for my Java applet
</H1>
<P>
This is the output from a Java applet:
<APPLET CODE="Rectangles.class" WIDTH=150 HEIGHT=50>
</APPLET>
</BODY>
</HTML>
This is what I learned from my java book (with JDK v 1.1)
Note: I posted this on other forums too