| ★ wanayoo — archive 1999 http://javaboutique.internet.com/tutorials/Java2D-Tutorial_4.html | Nouvelle recherche | Portail wanayoo |
|
![]()
Java2D: An Introduction and Tutorialby Marty HallTable of Contents 3. Paint Styles in Java2D3.1 Paint Styles: OverviewWhen you fill a Shape, the current Paint attribute of the Graphics2D object is used. This can be a Color (solid color), a GradientPaint (gradient fill gradually combining two colors), a TexturePaint (tiled image), or a new version of Paint that you write yourself. Use setPaint and getPaint to change and retrieve the Paint settings. Note that setPaint and getPaint supersede the setColor and getColor methods that were used in Graphics.3.2 Paint ClassesArguments to the Graphics2D setPaint method (and return values of getPaint) must implement the Paint interface. Here are the major built-in Paint classes:
3.3 TransparencyTransparency is not set in the Paint object, rather separately via an AlphaComposite object that is applied via setComposite. See Section 4 for details.3.4 Gradient Fills: Example CodeDownload the source: GradientPaintExample.java (plus ShapeExample.java, WindowUtilities.java, and ExitListener.java if you don't have them from the previous examples).
import java.awt.*;
/** An example of gradient fills with Java2D in Java 1.2.
* 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class GradientPaintExample extends ShapeExample {
// Red at (0,0), yellow at (175,175), changes gradually between.
private GradientPaint gradient =
new GradientPaint(0, 0, Color.red, 175, 175, Color.yellow,
true); // true means to repeat pattern
public void paintComponent(Graphics g) {
clear(g);
Graphics2D g2d = (Graphics2D)g;
drawGradientCircle(g2d);
}
protected void drawGradientCircle(Graphics2D g2d) {
g2d.setPaint(gradient);
g2d.fill(getCircle());
g2d.setPaint(Color.black);
g2d.draw(getCircle());
}
public static void main(String[] args) {
WindowUtilities.openInJFrame(new GradientPaintExample(),
380, 400);
}
}
3.5 Gradient Fills: Example Output
3.6 Tiled Images as Fill Patterns -- OverviewTo use tiled images, you create a TexturePaint object and specify its use via the setPaint method of Graphics2D, just as with solid colors and gradient fills. The TexturePaint constructor takes a BufferedImage and a Rectangle2D as arguments. The BufferedImage specifies what to draw, and the Rectangle2D specifies where the tiling starts. Creating a BufferedImage to hold custom drawing is relatively straightforward: call the BufferedImage constructor with a width, a height, and a type of BufferedImage.TYPE_INT_RGB, then call createGraphics on that to get a Graphics2D with which to draw. It is a bit harder to create one from an image file. First load an Image from an image file, then use MediaTracker to be sure it is done loading, then create an empty BufferedImage using the Image width and height, then get the Graphics2D via createGraphics, then draw the Image onto the BufferedImage. This process has been wrapped up in the getBufferedImage method of my ImageUtilitiesities class.Note, however, that as of JDK1.2beta4, tiled images fail when used in conjunction with rotation transformations. 3.7 Tiled Images as Fill Patterns: Example CodeDownload the source: TiledImages.java and ImageUtilities.java (plus WindowUtilities.java and ExitListener.java if you don't have them from the previous examples).TiledImages.java
import com.sun.java.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
/** An example of using TexturePaint to fill objects with tiled
* images. Uses the getBufferedImage method of ImageUtilitiesities to
* load an Image from a file and turn that into a BufferedImage.
* 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class TiledImages extends JPanel {
private String dir = System.getProperty("user.dir");
private String imageFile1 = dir + "/images/marty.jpg";
private TexturePaint imagePaint1;
private Rectangle imageRect;
private String imageFile2 = dir + "/images/bluedrop.gif";
private TexturePaint imagePaint2;
private int[] xPoints = { 30, 700, 400 };
private int[] yPoints = { 30, 30, 600 };
private Polygon imageTriangle = new Polygon(xPoints, yPoints, 3);
public TiledImages() {
BufferedImage image =
ImageUtilitiesities.getBufferedImage(imageFile1, this);
imageRect =
new Rectangle(235, 70, image.getWidth(), image.getHeight());
imagePaint1 =
new TexturePaint(image, imageRect);
image = ImageUtilitiesities.getBufferedImage(imageFile2, this);
imagePaint2 =
new TexturePaint(image, new Rectangle(0, 0, 32, 32));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(imagePaint2);
g2d.fill(imageTriangle);
g2d.setPaint(Color.blue);
g2d.setStroke(new BasicStroke(5));
g2d.draw(imageTriangle);
g2d.setPaint(imagePaint1);
g2d.fill(imageRect);
g2d.setPaint(Color.black);
g2d.draw(imageRect);
}
public static void main(String[] args) {
WindowUtilities.openInJFrame(new TiledImages(), 750, 650);
}
}
ImageUtilities.java
import java.awt.*;
import java.awt.image.*;
/** A class that simplifies a few common image operations, in
* particular creating a BufferedImage from an image file, and
* using MediaTracker to wait until an image or several images are
* done loading. 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class ImageUtilities {
/** Create Image from a file, then turn that into a BufferedImage.
*/
public static BufferedImage getBufferedImage(String imageFile,
Component c) {
Image image = c.getToolkit().getImage(imageFile);
waitForImage(image, c);
BufferedImage bufferedImage =
new BufferedImage(image.getWidth(c), image.getHeight(c),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.drawImage(image, 0, 0, c);
return(bufferedImage);
}
/** Take an Image associated with a file, and wait until it is
* done loading. Just a simple application of MediaTracker.
* If you are loading multiple images, don't use this
* consecutive times; instead use the version that takes
* an array of images.
*/
public static boolean waitForImage(Image image, Component c) {
MediaTracker tracker = new MediaTracker(c);
tracker.addImage(image, 0);
try {
tracker.waitForAll();
} catch(InterruptedException ie) {}
return(!tracker.isErrorAny());
}
/** Take some Images associated with files, and wait until they
* are done loading. Just a simple application of MediaTracker.
*/
public static boolean waitForImages(Image[] images, Component c) {
MediaTracker tracker = new MediaTracker(c);
for(int i=0; i<images.length; i++)
tracker.addImage(images[i], 0);
try {
tracker.waitForAll();
} catch(InterruptedException ie) {}
return(!tracker.isErrorAny());
}
}
3.8 Tiled Images as Fill Patterns: Example Output
[Move on to the next part of the article] This article first appeared in November, 1998
Copyright 1998 Mecklermedia Corporation. |