KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > VisualImageObject


1 /*
2  * VisualImageObject.java
3  *
4  * Created on 10 June 2003, 11:21
5  */

6
7 package com.thoughtriver.open.vectorvisuals;
8
9 import java.awt.*;
10 import java.awt.geom.*;
11
12 /**
13  * This class can be used to display a simple image with all of the benefits of
14  * being a <CODE>VisualObject</CODE>. Transparency is supported, and clipping
15  * is calculated based on the dimensions of the image, so normal <CODE>Shape</CODE>
16  * definition is not required. However, if custom clipping is desired, a call to
17  * <CODE>setShape</CODE> can be used to specify a region.
18  * <P>
19  * If more sophisticated <CODE>Image</CODE> texturing is desired, a basic
20  * <CODE>VisualObject</CODE> can be used instead, with a <CODE>Brush</CODE>
21  * defined to use an <CODE>Image</CODE>-based <CODE>TexturePaint</CODE>.
22  *
23  * @author Brandon Franklin
24  * @version $Date: 2006/11/25 09:18:36 $
25  */

26 public class VisualImageObject extends VisualObject {
27
28     /** The <CODE>Image</CODE> that this object displays */
29     private Image image = null;
30
31     /**
32      * Creates a new instance of <CODE>VisualImageObject</CODE> with the
33      * specified <CODE>Image</CODE> and <CODE>Shape</CODE> (for clipping).
34      *
35      * @param shape the <CODE>Shape</CODE> of this object
36      * @param image the <CODE>Image</CODE> to base this object on
37      */

38     public VisualImageObject(final Shape shape, final Image image) {
39         super(new Area(), null, null);
40         setImage(image);
41         setShape(shape);
42     }
43
44     /**
45      * Creates a new instance of <CODE>VisualImageObject</CODE> with the
46      * specified image name and <CODE>ImageManager</CODE>. The image name
47      * will be used to retrieve an <CODE>Image</CODE> instance from the
48      * supplied <CODE>ImageManager</CODE>. If no <CODE>ImageManager</CODE>
49      * is provided, the shared instance will be retrieved and used.
50      *
51      * @param imageName the name of the image to display
52      * @param manager the <CODE>ImageManager</CODE> to retrieve the <CODE>Image</CODE>
53      * from
54      */

55     public VisualImageObject(final String JavaDoc imageName, final ImageManager manager) {
56         super(new Area(), null, null);
57
58         ImageManager imgManager = manager;
59         if (imgManager == null) {
60             imgManager = ImageManager.getSharedInstance();
61         }
62
63         setImage(imgManager.getImage(imageName));
64     }
65
66     /**
67      * Sets the <CODE>Image</CODE> that this object displays.
68      *
69      * @param image the new <CODE>Image</CODE> for this object to display
70      */

71     public void setImage(final Image image) {
72         this.image = image;
73
74         // Update the shape based on the image's bounding box
75
Rectangle2D bounds = new Rectangle2D.Double(0, 0, image.getWidth(null), image.getHeight(null));
76         setShape(bounds);
77     }
78
79     /**
80      * Returns the <CODE>Image</CODE> that this object displays.
81      *
82      * @return the <CODE>Image</CODE> that this object displays
83      */

84     public Image getImage() {
85         return image;
86     }
87
88     /**
89      * Renders this object onto the supplied graphics context. Embedded objects
90      * will be rendered later, followed by the object's outline.
91      *
92      * @param g the graphics context on which to render
93      */

94     @Override JavaDoc
95     public void renderObject(final Graphics2D g) {
96         Shape oldClip = g.getClip();
97
98         g.setClip(getShape());
99         g.drawImage(getImage(), 0, 0, null);
100
101         g.setClip(oldClip);
102     }
103
104 }
105
Popular Tags