Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Drawing Basic Rectangles, Using Java!
DarkDivinity
post Jan 11 2008, 08:22 PM
Post #1


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 9
Joined: 11-January 08
Member No.: 27,543



I posted on another forums... But not allowed to post links wink.gif

In this tut ill show you how to draw two basic rectangles with Java.

First we start with out imports:

CODE
import java.awt.*;
import java.applet.Applet;//tells the browser its an applet lol
Now we start with the class:
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
              {
              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);
                    }
          }
}
Now you need the HTML code to run it in your browser
HTML
<HTML>
<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>
Now make sure you name the java file Rectangles.java!
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!
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Sorting(0)


 



- Lo-Fi Version Time is now: 6th September 2008 - 07:31 AM