Home Java Java-tips Summaries Summary: GUI Graphics

Ask Questions?

View Latest Questions


 
 

Summary: GUI Graphics
Posted on: July 27, 2006 at 12:00 AM
This is a summary of simple graphics methods in the java.awt.Graphics class.

Java: Summary: GUI Graphics

This is a summary of simple graphics methods in the java.awt.Graphics class for drawing with shapes, colors, text, ... Other classes define more advanced graphics, eg, javax.swing.Graphics2D.

java.awt.Graphics Class

Draw on a JPanel. Assume g is a Graphics object, and all variables are type int unless otherwise declared. Angles (int startAngle, arcAngle) are in degrees counterclockwise from 3 o'clock. These methods use (x,y) at the top, left, corner and a width and height of the bounding box (except drawString, drawLine).

void g.drawLine(x1, y1, x2, y2);
void g.drawRect(x, y, width, height);  // (x,y) is upper left corner 
void g.fillRect(x, y, width, height);
void g.drawOval(x, y, width, height);
void g.fillOval(x, y, width, height); 
void g.drawArc( x, y, width, height, startAngle, arcAngle);
void g.fillArc( x, y, width, height, startAngle, arcAngle); 
void g.setFont(Font f);     // all drawing after this uses the Font f.
void g.drawString(String s, x, y); // draws s with the left base at (x,y)

void g.drawPolyline(int[] xPoints, int[] yPoints, nPoints); // draws line 
void g.drawPolygon( int[] xPoints, int[] yPoints, nPoints); // draws polygon
void g.drawPolygon( poly); // draws polygon, same for fillPolygon.
void g.fillPolygon( int[] xPoints, int[] yPoints, nPoints); // fills polygon
void g.setColor(Color c); // all drawing after this uses the Color c.

Extend (subclass) JPanel for drawing

Drawing is usually done by defining a subclass of JPanel and overriding the paintComponent method.
class MyDrawing extends JPanel {
   public MyDrawing() {  // constructor
       setBackground(Color.WHITE);
   }
   
   public void paintComponent(Graphics g) {
       super.paintComponent(g);    // paint background, borders
       g.drawOval(0,0, 100, 100);  // do your drawing here.
   }
}

java.awt.Polygon

Straight-sided shapes (eg, triangles) can be created with Polygon class and the Graphics drawPolygon or fillPolygon methods.
  Polygon poly = new Polygon();  // declare and create
  poly.add(x, y);  // add points to polygon
  g.drawPolygon(poly);
The polygon coordinates can be translated with:
  poly.translate(deltaX, deltaY);
There is also a Polygon constructor which takes arrays of points:
 Polygon p = new Polygon(int[] xPoints, int[] yPoints, int nPoints);

JPanel methods

These can be used in the constructor or init() to set the initial values
  this.setBackground(Color c);  // Sets the background color
  this.setForeground(Color c);  // Sets the initial pen color.
Use the following to get the size of the panel, if necessary.
  int w = this.getWidth();   // Width of the JPanel drawing area
  int h = this.getHeight();  // Height ...

java.awt.Color Class

Predefined colors (lowercase without underscores for pre-Java 1.4 Color.BLACK, Color.WHITE, Color.DARK_GRAY, Color.GRAY, Color.LIGHT_GRAY, Color.BLUE, Color.CYAN, Color.GREEN, Color.RED, Color.MAGENTA, Color.PINK, Color.ORANGE, Color.YELLOW, Color.BLUE, Color.CYAN
Creating a color c = new Color(int r, int g, int b); // creates a new color with RGB values (each 0-255)
Example: Color medblu = new Color(128, 128, 255);

java.awt.Font Class to create generic fonts

In addition to the generic fonts below, you can use any of the fonts installed on the system. Get the list of available fonts using (GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames() which returns a String[]). Graphics getFontMetrics() returns info about font measurements.
Font f;
f = new Font(String name, int style, int size);	// creates a new font
name is "Serif", "SansSerif", or "Monospaced", or a font on the system. style is Font.PLAIN. Font.BOLD, Font.ITALIC, or Font.BOLD+Font.ITALIC. size is the point size, usually in the range 8-48.
Example:
  Font big = new Font("SansSerif", Font.Bold, 48);
  . . .
  g.setFont(big);
  g.drawString("Greetings Earthling");
Copyright 2004 Fred Swartz

Related Tags for Summary: GUI Graphics:
javacstringswingclassgraphicstextjpavariablesobjectmethodsclassescolorsdrawingcountmethodvariablecolor2dtypegraphheightawtwidthlocksumpanelintlineriathisidtarsimplejpaneldefinestartdegreeadvancedtexforboxwithclocktodrawrawshapessseanglecounterwsshtopexteshapeitaxcanliicsvarusewisepejavaximfromartcedrawstringleftinsummaryshacsasstamnttrcalessjadarccornerclesdeclarewstringallrclocmeobjpansvaxsueeesscolstaratpanekishallmplgraphicgraphics2dgreexceptandarstrrtsimxtwidwingwiswtvassriringrawstringthswclocavboundingstabablapdegreeseclfinjepleplndodsonomolonl