KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > util > RenderUtil


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.util;
28
29 import java.awt.BasicStroke JavaDoc;
30 import java.awt.Font JavaDoc;
31 import java.awt.FontMetrics JavaDoc;
32 import java.awt.Graphics2D JavaDoc;
33 import java.awt.Point JavaDoc;
34 import java.awt.Rectangle JavaDoc;
35 import java.awt.Toolkit JavaDoc;
36 import java.awt.font.FontRenderContext JavaDoc;
37 import java.awt.font.GlyphVector JavaDoc;
38 import java.awt.font.LineMetrics JavaDoc;
39 import java.awt.geom.Rectangle2D JavaDoc;
40
41 import org.nightlabs.editor2d.render.RenderConstants;
42
43
44 public class RenderUtil
45 {
46
47   public RenderUtil() {
48     super();
49   }
50   
51   public static BasicStroke JavaDoc setStrokeStyle(int strokeWidth, int strokeStyle)
52     {
53     float[] dashArray = new float[] {1f, 1f};
54     int dashPhase = 0;
55     float miterLimit = 10.0f;
56     
57     if (strokeStyle == RenderConstants.STROKE_SOLID) {
58 // dashArray = new float[] {1f, 1f};
59
return new BasicStroke JavaDoc();
60     }
61     else if (strokeStyle == RenderConstants.STROKE_DASHED_1) {
62       dashArray = new float[]{6f, 6f};
63     }
64     else if (strokeStyle == RenderConstants.STROKE_DASHED_2) {
65       dashArray = new float[]{3f, 3f};
66     }
67     else if (strokeStyle == RenderConstants.STROKE_DASHED_3) {
68       dashArray = new float[]{1f, 1f};
69     }
70     else if (strokeStyle == RenderConstants.STROKE_DASHED_4) {
71       dashArray = new float[]{3f, 6f};
72       dashPhase = 1;
73     }
74     return new BasicStroke JavaDoc(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, miterLimit, dashArray, dashPhase);
75   }
76   
77   public static GlyphVector JavaDoc createGlyphVector(String JavaDoc s, Font JavaDoc f, FontRenderContext JavaDoc frc)
78   {
79     return f.createGlyphVector(frc, s);
80   }
81   
82   public static void paintCenterGlyphVector(String JavaDoc s, Graphics2D JavaDoc g2d, Font JavaDoc f, Rectangle JavaDoc bounds)
83   {
84     GlyphVector JavaDoc glyphVector = createGlyphVector(s, f, g2d.getFontRenderContext());
85     Rectangle2D JavaDoc glyphBounds = glyphVector.getVisualBounds();
86     Point JavaDoc glyphLocation = EditorModelUtil.getLeftTopCenterLocation(glyphBounds.getBounds(), bounds);
87     g2d.drawGlyphVector(glyphVector, glyphLocation.x, glyphLocation.y);
88   }
89   
90 // public static int getPixelWidth( Font f, String s )
91
// {
92
// FontMetrics fm = Toolkit.getDefaultToolkit( ).getFontMetrics( f );
93
// return fm.stringWidth( s );
94
// }
95

96 }
97
Popular Tags