|
|
|
|
![]() ![]() |
Jan 11 2008, 08:22 PM
Post
#1
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 9 Joined: 11-January 08 Member No.: 27,543 |
I posted on another forums... But not allowed to post links
In this tut ill show you how to draw two basic rectangles with Java. First we start with out imports: CODE import java.awt.*; Now we start with the class:import java.applet.Applet;//tells the browser its an applet lol CODE public class Rectangles extends Applet And then the voids:{ CODE public void paint(Graphics g) //you will always need Graphics g so the applet knows its a drawing Now you need the HTML code to run it in your browser{ 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); } } } HTML <HTML> Now make sure you name the java file Rectangles.java!<HEAD> <TITLE>Rectangles</TITLE> </HEAD> <BODY> <H1>A test page for my Java applet<BR> </H1> <P> This is the output from a Java applet:<BR> <BR> <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) Also Read the comments I have added to help you out! |
|
|
|
![]() ![]() |
Similar Topics
| Topics | Topics | |
|---|---|---|
|
|
|
|
Lo-Fi Version | Time is now: 6th September 2008 - 07:31 AM |