★ wanayoo — archive 1999 http://javaboutique.internet.com/tutorials/Java2D-Tutorial_2.htmlNouvelle recherche | Portail wanayoo
internet.com logo To JavaBoutique Home

 Java News
 Latest Applets
 Applet Categories
 Applet Index
 Applet Help
 Submit Applets
 Other Resources
 How-To Java
 Java Reviews
 Java FAQs
 Java Forum
 Java IDEs
To Webdeveloper.com
 Mecklermedia
 Internet.com
 Magazines
 Trade Shows
 Advertise
 Corporate
 Information
 Search
 Subscribe

internet.com
BrowserWatch
CyberAtlas
E-Commerce Guide
IntranetDesignMag
InternetNews.com
InternetProductWatch
InternetShopper
Internet World
JavaBoutique
JavaScript Source
JustSMIL
PCWebopaedia.com
ScriptSearch
SearchEngineWatch
ServerWatch
Stroud's CWSApps
The List
The WDVL
WebCompare.com
WebDeveloper.com
WebReference.com

Webopedia


How-To Java

Java2D: An Introduction and Tutorial

by Marty Hall
Table of Contents

1. Introduction to Java2D

1.1 Overview

In Java 1.2, the paintComponent method is supplied with a Graphics2D object (a subclass of Graphics), which contains a much richer set of drawing operations. It includes pen widths, dashed lines, image and gradient color fill patterns, the use of arbitrary local fonts, a floating point coordinate system, and a number of coordinate transformation operations. However, to maintain compatibility with Swing as used in Java 1.1, the declared type of the paintComponent argument is Graphics, so you have to cast it to Graphics2D before using it.

1.2 Main New Features

  • Colors and patterns: gradient fills, fill patterns from tiled images, transparency
  • Local fonts
  • Pen thicknesses, dashing patterns, and segment connection styles
  • Coordinate transformations

1.3 General Approach

  • Cast the Graphics object to a Graphics2D object
    public void paintComponent(Graphics g) {
      super.paintComponent(g);  // Typical Swing approach
      Graphics2D g2d = (Graphics2D)g;
      g2d.doSomeStuff(...);
      ...
    }
    
  • Create a Shape object
    Rectangle2D.Double rect = ...;
    Ellipse2D.Double ellipse = ...;
    Polygon poly = ...;
    GeneralPath path = ...;
    ...
    
  • Draw an outlined or solid version of the Shape
    g2d.draw(someShape);
    g2d.fill(someShape);
    
  • Modify drawing parameters
    g2d.setPaint(fillColorOrPattern);
    g2d.setStroke(penThicknessOrPattern);
    g2d.setComposite(someAlphaComposite);
    g2d.setFont(someFont);
    g2d.translate(...);
    g2d.rotate(...);
    g2d.scale(...);
    g2d.shear(...);
    g2d.setTransform(someAffineTransform);
    


[Move on to the next part of the article]

This article first appeared in October, 1998

Stock Market Analysis

Copyright 1998 Mecklermedia Corporation.
All Rights Reserved. Legal Notices.