Most DoodlePad Shape subclasses may be filled with one color and stroked (outlined) with a separate color. It is also possible to disable Shape fill or stroke, drawing empty shapes or shapes without an outline. Colors in DoodlePad are constructed from color components: red, green, and blue, and one transparency component called alpha. The value of each of the four components may be an integer in the range [0, 255].
Use the interactive widget on the right to explore how these four values combine to achieve a desired fill or stroke color. Drag the colored slider knobs up and down with the mouse to change a component's value. The color that results from combining the four components is shown in the rectangular swatch on the right. To illustrate transparency (alpha), behind the swatch there are three colored lines. Transparency is increased by decreasing the alpha component. When alpha is 0, the resulting color is completely transparent. When alpha is 255, the color is completely opaque. Drag the alpha slider up and down to see the impact on color.
Pure red, green or blue is achieved by setting the corresponding color component value to its maximum (255) and leaving the other two color color values at 0. Other colors are constructed from different combinations of the color components. All shades of gray, from black to white, are formed by setting the values of red, green, and blue to equal values between 0 and 255.
Color | Red | Green | Blue | Sample |
---|---|---|---|---|
Yellow | 255 | 255 | 0 | |
Orange | 255 | 127 | 0 | |
Cyan | 0 | 255 | 255 | |
Magenta | 255 | 0 | 255 | |
Black | 0 | 0 | 0 | |
Gray | 127 | 127 | 127 | |
White | 255 | 255 | 255 |
Each Shape object's fill color and stroke color may be specified using color component values. There are two mutator methods for setting Shape color: setFillColor(...) and getStrokeColor(...). These setter methods each have three overloads. The base method signature has four parameters, one for each of the four color components: red, green, blue and alpha. A second overloaded method has only three parameters, which specify red, green and blue components. In this case the alpha component is assumed to have a value of 255, meaning that the Shape color has no transparency. The third overloaded method has only one parameter, often called gray. In this case the parameter value is repeated for all three color components, and alpha once again is set to 255.
The following example program creates five shapes with various fill and stroke colors. The resulting Shape objects are shown in the image on the right. Note that the Oval is unfilled, so the underlying red Rectangle is visible. Also note that the filled Arc has a semi-transparent yellow fill, so the underlying Line is partially visible.
import doodlepad.*;
public class Colors {
private Oval myOval;
private Rectangle myRect;
private Line myLine;
private Arc myArc;
private Polygon myPoly;
public Colors() {
// Red Rectangle with default stroke style
myRect = new Rectangle( 10, 55, 50, 40);
myRect.setFillColor(255, 0, 0);
// Unfilled Oval with thick light gray stroke
myOval = new Oval(40, 50, 50, 50);
myOval.setFilled(false);
myOval.setStrokeColor(200);
myOval.setStrokeWidth(5);
// Gray Line with thick stroke
myLine = new Line(80, 50, 120, 90);
myLine.setStrokeColor(127);
myLine.setStrokeWidth(10);
// Semi-transparent yellow filled Arc with default stroke style
myArc = new Arc(100, 50, 50, 50, 30, 290);
myArc.setFillColor(255, 255, 0, 127);
// Green Polygon with no stroke
double[] xs = {130, 150, 170};
double[] ys = {50, 100, 50};
myPoly = new Polygon(xs, ys);
myPoly.setFillColor(0, 255, 0);
myPoly.setStroked(false);
}
public static void main(String[] args) {
Colors myColors = new Colors();
}
}
The following table lists all color-related Shape methods and their overloads with descriptions. Both setter and getter methods are listed.
Method | Description |
---|---|
public void setFillColor(double red, double green, double blue, double alpha) | Set the color used to fill a Shape by specifying all four color components. |
public void setFillColor(double red, double green, double blue) | Set the color used to fill a Shape. The alpha color component is set to 255, meaning that the Shape fill is not transparent. |
public void setFillColor(double gray) | Set the fill color of a Shape to a shade of gray. The single parameter value is used for red, green and blue. The alpha component is set to 255, meaning that the Shape fill is not transparent. |
public int getFillRed() | Return the red fill color component. |
public int getFillBlue() | Return the blue fill color component. |
public int getFillGreen() | Retrun the green fill color component. |
public int getFillAlpha() | Return the alpha fill color component indicating the degree to which the shape fill is transparent. |
public java.awt.Color getFillColor() | Return the color used to fill a Shape as a java.awt.Color object. |
public void setFilled(boolean filled) | Specify whether or not a Shape is to be filled with a color. If set to false the Shape will not be filled. |
public boolean getFilled() | Return a boolean indicating whether or not the Shape is filled. |
public void setStrokeColor(double red, double green, double blue, double alpha) | Set the color used to stroke (outline) a Shape by specifying al four color components |
public void setStrokeColor(double red, double green, double blue) | Set the color used to outline a Shape. The alpha color component is set to 255, meaning that the Shape outline is not transparent. |
public void setStrokeColor(double gray) | Set the color used to outline a Shape to a shade of gray. The single parameter value is used for red, green and blue. The alpha component is set to 255, meaning that the Shape outline is not transparent. |
public int getStrokeRed() | Return the red outline color component. |
public int getStrokeBlue() | Return the blue outline color component. |
public int getStrokeGreen() | Return the green outline color component. |
public int getStrokeAlpha() | Return the alpha outline color component indicating the degree to which the shape outline is transparent. |
public java.awt.Color getStrokeColor() | Return the color used to outline a Shape as a java.awt.Color object. |
public void setStroked(boolean filled) | Specify whether or not a Shape is to be outline with a color. If set to false the Shape will not be outlined. |
public boolean getStroked() | Return a boolean indicating whether or not the Shape is outlined. |
The Pad object also has a few color-related methods. These are used only for setting and getting the Pad's background color. The Pad has only two overloads for its method of setting background color. Because a Pad's color may not be transparent, no overload is available that includes an alpha parameter.
Method | Description |
---|---|
public void setBackground(double red, double green, double blue) | Specify the color to use to fill the background of a Pad object. Only red, green and blue color components are specified because Pad objects may not be transparent. |
public void setBackground(double gray) | Specify the shade of gray to use to fill the background of a Pad object. The single parameter value is used for red, green and blue. |
public java.awt.Color getBackground() | Return the color used to fill the background of a Pad object as a java.awt.Color object. |