Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java: Summary: 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. Related classes: Fonts, Images - ImageIcon

java.awt.Graphics Class - Basic drawing methods

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() and drawLine()).

g.drawLine(x1, y1, x2, y2);
g.drawRect(x, y, width, height);  // (x,y) is upper left corner 
g.fillRect(x, y, width, height);
g.drawOval(x, y, width, height);
g.fillOval(x, y, width, height); 
g.drawArc( x, y, width, height, startAngle, arcAngle);
g.fillArc( x, y, width, height, startAngle, arcAngle); 
g.drawString(String s, x, y); // Draws s with the left base at (x,y)

g.setColor(Color c);          // All drawing after this uses the Color c.
g.setFont(Font f);            // All drawing after this uses the Font f.

g.drawPolyline(int[] xPoints, int[] yPoints, nPoints); // Draws line 
g.drawPolygon( int[] xPoints, int[] yPoints, nPoints); // Draws polygon
g.drawPolygon( poly);         // draws polygon, same for fillPolygon.
g.fillPolygon( int[] xPoints, int[] yPoints, nPoints); // Fills polygon

JPanel methods

Use these calls in the constructor to set the initial values.

  setPreferredSize(new Dimension(width, height)); // Set size
  setBackground(Color c);  // Set background color
  setForeground(Color c);  // Set the initial pen color.

Use the following in paintCompenent() to get the size of the panel.

int w = this.getWidth();   // Get width of the JPanel drawing area
int h = this.getHeight();  // Get 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 mediumBlue = new Color(128, 128, 255);

java.awt.Polygon

Straight-sided shapes (eg, triangles) can be created with Polygon class and the Graphics drawPolygon or fillPolygon methods. Add each vertex as an (x, y) pair.

Polygon poly = new Polygon();  // declare and create
poly.addPoint(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);

Extend (subclass) JPanel for drawing

Drawing is usually done by defining a subclass of JPanel and overriding the paintComponent method. The constructor should set the background color and size.

import java.awt.*;
import javax.swing.*;

class MyDrawing extends JPanel {
   public MyDrawing() {  // constructor
       setBackground(Color.WHITE);
       setPreferredSize(new Dimension(200, 100));
   }
   
   public void paintComponent(Graphics g) {
       super.paintComponent(g);    // Paint background, borders
       g.drawOval(0,0, 100, 100);  // Do your drawing here.
   }
}
Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.