I'd say to begin with, you have two different ways of making shapes (and gui's):
1) AWT / applet
2) Swing
In java, there are thousands of pieces of code that provide functionality. If every one of those pieces of code had to be loaded everytime an application was run, the application would take forever to launch! To avoid this, these code chunks are placed in packages, and a java developer can then import only the packages containing the functionality she needs.
I would recommend starting by creating a java applet, and using the graphics class. Here is a very basic example, documented. (I'm gonna type it up really quick...
Note: the "coordinate" stuff is based on the "java coordinate system." It states that the upper-left corner of the display surface (applet, window, whatever) is coordinate 0,0 (x,y). everything to the right of that corner is an "X" and everything below the top line (0) is a "Y".... a graph:
Code:
<0,0>
*---------------------x----------> X axis
| |
y |--------------------* <x,y>
|
|
y axis
And now the code, 2 parts. The first is just the code (with minimal documentation), the 2nd is the code LADEN with documentation.
Code:
import java.applet.Applet;
import java.awt.*;
public class DrawTestNoDoc extends Applet {
public void paint (Graphics page) {
/* Color Setup */
setBackground(Color.lightGray); //sets the applet background to lightGray
page.setColor(Color.blue); //sets the default paint color to blue
/* Draw Ovals */
page.drawOval(20, 30, 20, 20); //this draws a perfect circle
page.fillOval(60, 30, 20, 20); //this draws an opaque oval
/* Switch Colors */
Color myColor = new Color(255,200,0); //create a new RGB-defined color.
page.setColor(myColor); //set the foreground to the new color.
/* Draw Rectangles */
page.fillRect(10, 70, 30, 60); //draw an opaque rectangle
page.fill3DRect(50, 70, 30, 30, true); //draw a 3d rectangle.
} // method paint
} // class DrawTest
and the documented one:
Code:
/* Import the code to draw pictures & shapes */
import java.awt.*;
/* Import the code to display as an applet */
import java.applet.Applet;
/*
* our main class. by 'extending' the 'Applet' package,
* we have access to all of the Applet's methods and
* functionality. Good Object-Oriented design means that you
* create "sub classes" of main classes. It helps when reading
* the code.
*/
public class DrawTest extends Applet {
/*
* the main method for an applet, paint(). Here it accepts a single parameter,
* a "Graphics" object, called page. It could just as easily have
* accepted a "String" object, but we're painting here. So we need
* graphics functionality, not string functionality on the object.
*/
public void paint (Graphics page) {
/*
* The setBackground method accepts a value for a color.
* This value can either be one of the static (accessible & predefined)
* colors already set by the "Color" class (like "Color.white" or
* "Color.magenta", or by defining a new "Color" object, using RGB
* values.
*/
setBackground(Color.lightGray); //sets the applet background to lightGray
//setForeground(Color.white);
/*
* The setColor() method of the "Graphics" class accepts a "Color" object
* as it's parameter. It will set the "painting" color.
*/
page.setColor(Color.blue);
/*
* the drawOval method takes 4 parameters:
* int x, int y, int width, int height
* where:
* x is the starting "X" coordinate, (how far to the right)
* y is the starting "Y" coordinate, (how far up & down)
* width is the width of the oval ~
* height is the height of the oval ~
*
* ~not actually the width & height of the oval. there is an
* invisible rectangle in which the oval is drawn. these are
* technically the dimensions of the invisible rectangle
*/
page.drawOval(20, 30, 20, 20); //this draws a perfect circle
/*
* the fillOval() method is the same as drawOval, except the shape
* is filled with the current "setColor"
*/
page.fillOval(60, 30, 20, 20); //this draws an opaque oval
// Lets create a new, custom color, besides the predefined ones!
/*
* The "Color" class can be used to create new color objects.
* When creating the object instance, we pass in the RGB value of the
* color we want to create. Here we create an "orange" color object.
* **Please note that we 'could' have used Color.orange (it's a predefined
* color), but for demo purposes, this works well.**
*/
Color myColor = new Color(255,10,0); //create a new RGB-defined color.
/*
* Here we use the "setColor()" method to change the color we "paint" in
*/
page.setColor(Color.orange); //set the foreground color the color object we created
/*
* Here we use the "fillRect()" Graphics method. It works similarly to the
* "fillOval()" method, and accepts 4 parameters:
* int x, int y, int width, int height
* where:
* x is the distance across the applet (the "X" coordinate)
* y is the distance "vertically" down the applet (the "Y" coordinate)
* width is the width of the rectangle
* height is the height of the rectangle
*/
page.fillRect(10, 70, 30, 60); //draw an opaque rectangle
/*
* Call the "fill3DRect()" method, which creates an opaque, 3D rectangle.
* The first 4 params are the same as the "fillRect()" method, but adds a 5th
* parameter: boolean raised
* if this is set to true, the object appears "raised." otherwise, it will
* appear to be "sunken" into the page.
*/
page.fill3DRect (50, 70, 30, 30, true); //draw a 3d rectangle.
} // method paint
} // class DrawTest
I called it by creating an html page with the following:
Code:
<html>
<body>
<applet
code="DrawTest.class"
height="400" width="300"
alt="Your browser is not displaying Java"
>
Your browser does not support Java</applet>
</body>
</html>
and placed it in the same directory as my compiled DrawTest.class.
Let me know how those files work for you.
-Celt
*note that I didn't use any non-standard libraries (code packages), just what comes bundled with the standard java sdk. *