KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tracing > server > viewer > ScaleManager


1 package org.coach.tracing.server.viewer;
2
3 import java.awt.*;
4 import java.awt.geom.*;
5 import java.awt.font.*;
6
7 public class ScaleManager {
8     public final static float DRAW_LINE_WIDTH = 1;
9     private float scale = (float)1.0;
10     private float scaleDelta = (float)0.25;
11     private float minScale = (float)0.25;
12     private Font font = null;
13     private RenderingHints rh = null;
14     private BasicStroke bs = null;
15     private BasicStroke bs_dashed = null;
16     private BasicStroke bs_circle = null;
17     private BasicStroke bs_circle_dashed = null;
18     private float dash_size = 5;
19     private int defaultIndex = 3;
20         
21     private static ScaleManager singleton = new ScaleManager();
22     
23     private ScaleManager() {
24         setScale((float)1.0);
25     }
26     
27     public static ScaleManager getScaleManager() {
28         return singleton;
29     }
30
31     //access methods
32
private synchronized void setScale(float s) {
33         scale = s;
34         font = new Font("Arial", Font.BOLD, (int)(10 * scale));
35         rh = new RenderingHints(null);
36         rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
37         rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
38         float[] da1 = { (float)scale * dash_size, (float)scale * dash_size };
39         float[] da2 = { (float)scale * 3, (float)scale * 3 };
40         bs = new BasicStroke(DRAW_LINE_WIDTH * scale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
41         bs_dashed = new BasicStroke(DRAW_LINE_WIDTH * scale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 1.0f, da1, 0);
42         bs_circle = new BasicStroke(DRAW_LINE_WIDTH * scale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
43         bs_circle_dashed = new BasicStroke(DRAW_LINE_WIDTH * scale, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 1.0f, da2, 0);
44     }
45         
46     public void scaleUp() {
47         setScale(scale + scaleDelta);
48     }
49     
50     public void scaleDown() {
51         if (scale > minScale) {
52             setScale(scale - scaleDelta);
53         }
54     }
55        
56     public synchronized float getScale() {
57         return scale;
58     }
59
60     public synchronized Font getFont() {
61         return font;
62     }
63
64     public Dimension getStringDimension(String JavaDoc s, Component c) {
65         FontMetrics fm = c.getFontMetrics(font);
66         // return new Dimension(fm.stringWidth(s), fm.getHeight());
67
// return new Dimension((int)fm.getStringBounds(s, c.getGraphics()).getWidth(), fm.getHeight());
68
return new Dimension((int)fm.getStringBounds(s, c.getGraphics()).getWidth(), (int)fm.getStringBounds(s, c.getGraphics()).getHeight());
69     }
70
71     public Dimension getStringDimension(String JavaDoc s, Graphics2D c) {
72         FontRenderContext frc = c.getFontRenderContext();
73         Font f = c.getFont();
74         GlyphVector gv = f.createGlyphVector(frc, s);
75         Rectangle2D r2d = gv.getLogicalBounds();
76         // return new Dimension(fm.stringWidth(s), fm.getHeight());
77
// return new Dimension((int)fm.getStringBounds(s, c.getGraphics()).getWidth(), fm.getHeight());
78
return new Dimension((int)r2d.getWidth(), (int)r2d.getHeight());
79     }
80
81     public RenderingHints getHints() {
82         return rh;
83     }
84
85     public BasicStroke getStroke() {
86         return bs;
87     }
88
89     public BasicStroke getStrokeDashed() {
90         return bs_dashed;
91     }
92
93     public BasicStroke getStrokeCircle() {
94         return bs_circle;
95     }
96
97     public BasicStroke getStrokeCircleDashed() {
98         return bs_circle_dashed;
99     }
100 }
101
Popular Tags