MathBits.com
Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use
 

Beginning Graphics
(using BreezyGUI)

BreezyGUI authors, Kenneth Lambert and Martin Osborne, state that graphics produced under BreezyGUI are similar to those produced under Java's AWT (Abstract Windowing Toolkit).  The major difference is that when using BreezyGUI, graphics are done directly in the interface window, making the programming more accessible to beginners.

Coordinate Axes:
When working with graphics, a coordinate system is essential for locating object.  You probably remember your mathematical coordinate axes system which utilizes an x-axis and a y-axis.  The axes for Java graphics, however, are somewhat different.

 
   (0,0)              Window The coordinate system for the Java graphing window positions (0,0) in the upper left hand corner.  The grid is then numbered in a positive direction on the x-axis (horizontally to the right) and in a positive direction on the y-axis (vertically going down).

We will now be using the methods contained within the Graphics Class in the package java.awt.

 


 

   Basic Graphing Methods:  

Line g.drawLine(35, 45, 75, 95);
drawLine(int x1, int y1, int x2, int y2)

Used to draw a straight line from point (x1,y1) to (x2,y2).

Rectangle

 

g.drawRect(35, 45, 25, 35);
drawRect(int x, int y, int width, int length)

Used to draw a rectangle with the upper left corner at (x,y) and with the specified width and length.

Round Edge Rectangle g.drawRoundRect(35,45,25,35,10,10);
drawRoundRect(int x, int y, int width, int
           length, int arcWidth, int arcHeight)

Used to draw a rounded edged rectangle.  The amount of rounding is controlled by arcWidth and arcHeight.

Oval / Circle

 

g.drawOval(25, 35, 25, 35);
g.drawOval(25, 35, 25, 25);
circle

drawOval(int x, int y, int width, int length)

Used to draw an oval inside an imaginary rectangle whose upper left corner is at (x,y).  To draw a circle keep the width and length the same.

Arc g.drawArc(35, 45, 75, 95, 0, 90);
drawArc(int x, int y, int width, int length,
                       int startAngle, int arcAngle)

Used to draw an arc inside an imaginary rectangle whose upper left corner is at (x,y).  The arc is drawn from the startAngle to startAngle + arcAngle and is measured in degrees. 
     A startAngle of 0º points horizontally to the right (like the unit circle in math).  Positive is a counterclockwise rotation starting at 0º.

Polygon int [ ] x = {20, 35, 50, 65, 80, 95};
int [ ] y = {60, 105, 105, 110, 95, 95};
g.drawPolygon(x, y, 6);

drawPolygon(int x[ ], int y[ ], int n)

Used to draw a polygon created by n line segments.  The command will close the polygon.  (x-coordinates go in one array with accompanying y-coordinates in the other)

String
(text)
g.drawString("Java is cool!", 40, 70);
drawString(String str, int x, int y);

Draws a string starting at the point indicated by (x,y).  Be sure you leave enough room from the top of the screen for the size of the font.


 


Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use