001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.graphic.configuration.shape;
051:
052: import java.awt.Color;
053: import java.awt.Graphics2D;
054: import java.awt.Rectangle;
055: import java.awt.TexturePaint;
056: import java.awt.image.BufferedImage;
057: import java.util.HashMap;
058:
059: /**
060: *
061: */
062: public class PredefinedPaint extends TexturePaint {
063: private Color foreground;
064: private Color background;
065: private int width;
066: private int height;
067: private int[] points;
068: private float alphaValue = 1.0f;
069:
070: public PredefinedPaint(int width, int height, int[] array) {
071: this (width, height, array, Color.black, Color.white);
072: }
073:
074: public PredefinedPaint(PredefinedPaint pattern, Color foreground,
075: Color background) {
076: this (pattern.width, pattern.height, pattern.points, foreground,
077: background);
078: }
079:
080: private PredefinedPaint(int width, int height, int[] points,
081: Color foreground, Color background) {
082: super (createTexture(width, height, points, foreground,
083: background), new Rectangle(0, 0, width, height));
084: this .width = width;
085: this .height = height;
086: this .foreground = foreground;
087: this .background = background;
088: this .points = points;
089: float sum = 0;
090: for (int i = 0; i < points.length; i++) {
091: sum += points[i];
092: }
093: alphaValue = (float) Math.pow(sum / points.length, 1.5); //modify brightness
094: }
095:
096: private static BufferedImage createTexture(int width, int height,
097: int[] array, Color foreground, Color background) {
098: BufferedImage bufferedImage = new BufferedImage(width, height,
099: BufferedImage.TYPE_INT_ARGB);
100: for (int w = 0; w < width; w++) {
101: for (int h = 0; h < height; h++) {
102: bufferedImage.setRGB(w, h,
103: array[w + h * width] > 0 ? foreground.getRGB()
104: : background.getRGB());
105: }
106: }
107: return bufferedImage;
108: }
109:
110: public static final PredefinedPaint TRANSPARENT = new PredefinedPaint(
111: 4, 4, new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112: 0, 0 });
113: public static final PredefinedPaint SOLID = new PredefinedPaint(4,
114: 4, new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
115: 1 });
116: public static final PredefinedPaint DEFAULT = new PredefinedPaint(
117: 4, 4, new int[] { 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1,
118: 0, 1 });
119: public static final PredefinedPaint SPACED_DOTS = new PredefinedPaint(
120: 4, 4, new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
121: 0, 0 });
122: public static final PredefinedPaint DIAGONAL = new PredefinedPaint(
123: 4, 4, new int[] { 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
124: 0, 0 });
125: public static final PredefinedPaint DOT_LINE = new PredefinedPaint(
126: 4, 4, new int[] { 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
127: 1, 0 });
128: public static final PredefinedPaint DASH_LINE = new PredefinedPaint(
129: 1, 4, new int[] { 1, 1, 0, 0 });
130: public static final PredefinedPaint DOT_LINE2 = new PredefinedPaint(
131: 1, 2, new int[] { 1, 0 });
132: public static final PredefinedPaint VERY_SPACED_DOTS = new PredefinedPaint(
133: 8, 8, new int[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
134: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
135: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
136: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
137:
138: private static Object[][] data = { { "TRANSPARENT", TRANSPARENT },
139: { "SOLID", SOLID }, { "DEFAULT", DEFAULT },
140: { "SPACED_DOTS", SPACED_DOTS },
141: { "VERY_SPACED_DOTS", VERY_SPACED_DOTS },
142: { "DASH_LINE", DASH_LINE }, { "DIAGONAL", DIAGONAL },
143: { "DOT_LINE2", DOT_LINE2 }, { "DOT_LINE", DOT_LINE } };
144: private static HashMap shapePaintMap = null;
145:
146: public static HashMap getShapePaints() {
147: if (shapePaintMap == null) {
148: shapePaintMap = new HashMap();
149: for (int i = 0; i < data.length; i++) {
150: Object row[] = data[i];
151: shapePaintMap.put(row[0], row[1]);
152: }
153: }
154: return shapePaintMap;
155: }
156:
157: public static PredefinedPaint find(String key) {
158: return (PredefinedPaint) getShapePaints().get(key);
159: }
160:
161: public void applyPaint(Graphics2D g2, boolean texture) {
162: //if ("SVGGraphics2D".equals(g2.getClass().getSimpleName()))
163: if (texture) {
164: g2.setPaint(this ); // the paint already has the color set
165: } else {
166: g2.setColor(new Color(bar(foreground.getRed(), background
167: .getRed(), alphaValue), bar(foreground.getGreen(),
168: background.getGreen(), alphaValue), bar(foreground
169: .getBlue(), background.getBlue(), alphaValue)));
170: }
171:
172: }
173:
174: private int bar(float a, float b, float w) {
175: return Math.round(a * w + (1.0f - w) * b);
176: }
177:
178: }
|