KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Brush.java
3  *
4  * Created on 4 June 2003, 11:47
5  */

6
7 package com.thoughtriver.open.vectorvisuals;
8
9 import java.awt.*;
10
11 /**
12  * The <CODE>Brush</CODE> class is a grouping of some graphics attributes
13  * related to the act of painting. Specifically, a <CODE>Brush</CODE>
14  * represents a specific set of one each of the following: a <CODE>Paint</CODE>,
15  * a <CODE>Composite</CODE>, and a <CODE>Stroke</CODE>. Because different
16  * <CODE>VisualObject</CODE>s can use the same <CODE>Brush</CODE> instance,
17  * a change to a single <CODE>Brush</CODE> can be used to modify the painting
18  * styles of several objects at once.
19  *
20  * @author Brandon Franklin
21  * @version $Date: 2006/11/25 09:18:36 $
22  */

23 public class Brush {
24
25     /** The <CODE>Paint</CODE> used by this <CODE>Brush</CODE> */
26     private Paint paint = null;
27
28     /** The <CODE>Composite</CODE> used by this <CODE>Brush</CODE> */
29     private Composite composite = null;
30
31     /** The <CODE>Stroke</CODE> used by this <CODE>Brush</CODE> */
32     private Stroke stroke = null;
33
34     /**
35      * Creates a new instance of <CODE>Brush</CODE> with the specified
36      * attributes. Any parameters that are set to null will be configured to
37      * suitable default values.
38      *
39      * @param paint the <CODE>Paint</CODE> attribute for this instance to use
40      * @param composite the <CODE>Composite</CODE> attribute for this instance
41      * to use
42      * @param stroke the <CODE>Stroke</CODE> attribute for this instance to
43      * use
44      */

45     public Brush(final Paint paint, final Composite composite, final Stroke stroke) {
46         setPaint(paint);
47         setComposite(composite);
48         setStroke(stroke);
49     }
50
51     /**
52      * Returns the <CODE>Paint</CODE> used by this <CODE>Brush</CODE>.
53      *
54      * @return the <CODE>Paint</CODE> used by this <CODE>Brush</CODE>
55      */

56     public Paint getPaint() {
57         return paint;
58     }
59
60     /**
61      * Returns the <CODE>Stroke</CODE> used by this <CODE>Brush</CODE>.
62      *
63      * @return the <CODE>Stroke</CODE> used by this <CODE>Brush</CODE>
64      */

65     public Stroke getStroke() {
66         return stroke;
67     }
68
69     /**
70      * Returns the <CODE>Composite</CODE> used by this <CODE>Brush</CODE>.
71      *
72      * @return the <CODE>Composite</CODE> used by this <CODE>Brush</CODE>
73      */

74     public Composite getComposite() {
75         return composite;
76     }
77
78     /**
79      * Sets the <CODE>Paint</CODE> used by this <CODE>Brush</CODE>. If the
80      * supplied parameter is null, a suitable default value will be assigned.
81      *
82      * @param paint the <CODE>Paint</CODE> to be used by this <CODE>Brush</CODE>
83      */

84     public void setPaint(final Paint paint) {
85         if (paint == null) {
86             this.paint = Color.BLACK;
87         }
88         else {
89             this.paint = paint;
90         }
91     }
92
93     /**
94      * Sets the <CODE>Stroke</CODE> used by this <CODE>Brush</CODE>. If the
95      * supplied parameter is null, a suitable default value will be assigned.
96      *
97      * @param stroke the <CODE>Stroke</CODE> to be used by this <CODE>Brush</CODE>
98      */

99     public void setStroke(final Stroke stroke) {
100         if (stroke == null) {
101             this.stroke = new BasicStroke();
102         }
103         else {
104             this.stroke = stroke;
105         }
106     }
107
108     /**
109      * Sets the <CODE>Composite</CODE> used by this <CODE>Brush</CODE>. If
110      * the supplied parameter is null, a suitable default value will be
111      * assigned.
112      *
113      * @param composite the <CODE>Composite</CODE> to be used by this <CODE>Brush</CODE>
114      */

115     public void setComposite(final Composite composite) {
116         if (composite == null) {
117             this.composite = AlphaComposite.Src;
118         }
119         else {
120             this.composite = composite;
121         }
122     }
123
124     /**
125      * Configures the supplied <CODE>Graphics2D</CODE> instance to use this
126      * <CODE>Brush</CODE>.
127      *
128      * @param g the <CODE>Graphics2D</CODE> to be configured
129      */

130     public void useOn(final Graphics2D g) {
131         g.setPaint(paint);
132         g.setStroke(stroke);
133         g.setComposite(composite);
134     }
135
136     /**
137      * Indicates whether some other object is "equal to" this one.
138      *
139      * @param obj the reference object with which to compare
140      * @return true if this <CODE>Brush</CODE> is the same as the obj
141      * argument, false otherwise
142      * @see #hashCode()
143      */

144     @Override JavaDoc
145     public boolean equals(final Object JavaDoc obj) {
146         Brush otherBrush = (Brush) obj;
147
148         if (!otherBrush.getPaint().equals(paint)) {
149             return false;
150         }
151
152         if (!otherBrush.getStroke().equals(stroke)) {
153             return false;
154         }
155
156         if (!otherBrush.getComposite().equals(composite)) {
157             return false;
158         }
159
160         return true;
161     }
162
163     /**
164      * Returns a hash code value for the object. This method is supported for
165      * the benefit of hashtables such as those provided by <CODE>Hashtable</CODE>.
166      *
167      * @return the hash code value of this <CODE>Brush</CODE>
168      * @see Object#equals(Object)
169      */

170     @Override JavaDoc
171     public int hashCode() {
172         int hash = paint.hashCode() + stroke.hashCode() + composite.hashCode();
173
174         return hash;
175     }
176
177 }
178
Popular Tags