View Javadoc

1   /*
2    *  File: GraphicsHelper.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
10   */
11  package de.jaret.util.swing;
12  
13  import java.awt.Color;
14  import java.awt.Graphics;
15  import java.awt.Image;
16  import java.awt.Rectangle;
17  import java.awt.geom.Rectangle2D;
18  import java.awt.image.BufferedImage;
19  import java.awt.image.ImageObserver;
20  
21  import org.eclipse.swt.graphics.GC;
22  import org.eclipse.swt.graphics.Point;
23  
24  /**
25   * A simple class containing several static methods for convenient painting with a grpahics object.
26   * 
27   * @author Peter Kliem
28   * @version $Id: GraphicsHelper.java 881 2009-09-22 21:25:47Z kliem $
29   */
30  public class GraphicsHelper {
31  
32      public static void drawStringCentered(Graphics graphics, String string, int left, int right, int y) {
33          Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics);
34          int width = right - left;
35          int xx = (int) ((width - rect.getWidth()) / 2);
36          graphics.drawString(string, xx, y);
37  
38      }
39  
40      public static void drawStringCenteredVCenter(Graphics graphics, String string, int left, int right, int yCenter) {
41          Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics);
42          int descent = graphics.getFontMetrics().getDescent();
43          int width = right - left;
44          int xx = (int) ((width - rect.getWidth()) / 2);
45          int y = yCenter + (int) (rect.getHeight() / 2 - descent);
46          graphics.drawString(string, xx, y);
47  
48      }
49  
50      public static void drawStringRightAlignedVCenter(Graphics graphics, String string, int x, int y) {
51          Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics);
52  
53          int xx = (int) (x - rect.getWidth());
54          int yy = (int) (y + (rect.getHeight() / 2));
55          graphics.drawString(string, xx, yy);
56      }
57  
58      public static void drawStringLeftAlignedVCenter(Graphics graphics, String string, int x, int y) {
59          Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics);
60  
61          int xx = x;
62          int yy = (int) (y + (rect.getHeight() / 2));
63          graphics.drawString(string, xx, yy);
64      }
65  
66      /**
67       * @param graphics graphics to use
68       * @param string string to render
69       * @param x center x
70       * @param y basey
71       */
72      public static void drawStringCentered(Graphics graphics, String string, int x, int y) {
73          Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics);
74          int xx = x - (int) ((rect.getWidth()) / 2);
75          graphics.drawString(string, xx, y);
76      }
77  
78      public static void drawStringCenteredPoint(Graphics graphics, String string, int x, int y) {
79          Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics);
80          int xx = x - (int) ((rect.getWidth()) / 2);
81          int yy = (int) (y + (rect.getHeight() / 2));
82          graphics.drawString(string, xx, yy);
83      }
84  
85      public static void drawStringCentered(Graphics graphics, String string, Rectangle area) {
86          Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics);
87          int xx = area.x + area.width / 2 - (int) ((rect.getWidth()) / 2);
88          int yy = (int) (area.y + area.height / 2 + (rect.getHeight() / 2));
89          graphics.drawString(string, xx, yy);
90      }
91  
92      public static int getStringDrawingWidth(Graphics graphics, String string) {
93          Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics);
94          return (int) rect.getWidth();
95      }
96  
97      public static int getStringDrawingHeight(Graphics graphics, String string) {
98          Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics);
99          return (int) rect.getHeight() - graphics.getFontMetrics().getDescent();
100     }
101 
102     public static void drawStringRightAlignedVTop(Graphics graphics, String string, int x, int yTop) {
103         Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics);
104 
105         int xx = (int) (x - rect.getWidth());
106         int yy = (int) (yTop + rect.getHeight());
107         graphics.drawString(string, xx, yy);
108 
109     }
110     
111     /**
112      * Draw a string vertically centered between upper and lower y left aligned to x.
113      * 
114      * @param graphics Graphics to paint with
115      * @param label label to draw
116      * @param x left x
117      * @param upperY upper y bound
118      * @param lowerY lower y bound
119      */
120     public static void drawStringVCentered(Graphics graphics, String string, int x, int upperY, int lowerY) {
121         Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics);
122 
123         int yy = (int) upperY + (lowerY - upperY)/2 + (int)rect.getHeight() / 2;
124         graphics.drawString(string, x, yy);
125     }
126 
127 
128     public static void drawArrowLine(Graphics g, int x1, int y1, int x2, int y2, int dist, boolean arrowLeft,
129             boolean arrowRight) {
130         int off = 3;
131         g.drawLine(x1 + off + 1, y1, x2 - off - 1, y2);
132         if (arrowLeft) {
133             g.drawLine(x1, y1, x1 + dist, y1 - off);
134             g.drawLine(x1, y1, x1 + dist, y1 + off);
135             g.drawLine(x1 + dist, y1 - off, x1 + dist, y1 + off);
136         }
137         if (arrowRight) {
138             g.drawLine(x2, y2, x2 - dist, y2 - off);
139             g.drawLine(x2, y2, x2 - dist, y2 + off);
140             g.drawLine(x2 - dist, y2 - off, x2 - dist, y2 + off);
141         }
142     }
143 
144     public static void drawArrowLineVertical(Graphics g, int x1, int y1, int x2, int y2, int dist, int height,
145             boolean arrowUp, boolean arrowDown) {
146         int off = height;
147         g.drawLine(x1, y1 + off + 1, x2, y2 - off - 1);
148         if (arrowUp) {
149             g.drawLine(x1, y1, x1 - off, y1 + dist);
150             g.drawLine(x1, y1, x1 + off, y1 + dist);
151             g.drawLine(x1 - off, y1 + dist, x1 + off, y1 + dist);
152         }
153         if (arrowDown) {
154             g.drawLine(x2, y2, x2 - off, y2 - dist);
155             g.drawLine(x2, y2, x2 + off, y2 - dist);
156             g.drawLine(x2 - off, y2 - dist, x2 + off, y2 - dist);
157         }
158     }
159 
160     /**
161      * Draw a String vertical. This method might be quite costly since it uses an image to buffer.
162      * 
163      * @param graphics graphics to paint with
164      * @param string string to to draw
165      * @param x upper left x
166      * @param y upper left y
167      */
168     public static void drawStringVertical(Graphics graphics, String string, int x, int y) {
169         Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics);
170         BufferedImage img = new BufferedImage((int) rect.getWidth(), (int) rect.getHeight(),
171                 BufferedImage.TYPE_3BYTE_BGR);
172         
173         Graphics imageGraphics = img.getGraphics();
174         imageGraphics.setColor(Color.WHITE);
175         imageGraphics.fillRect(0, 0, img.getWidth(), img.getHeight());
176         imageGraphics.setColor(Color.BLACK);
177         
178         imageGraphics.drawString(string, 0, (int) rect.getHeight());
179 
180         BufferedImage vertImg = new BufferedImage((int) rect.getHeight(), (int) rect.getWidth(),
181                 BufferedImage.TYPE_3BYTE_BGR);
182 
183         for (int xx = 0; xx < img.getWidth(); xx++) {
184             for (int yy = 0; yy < img.getHeight(); yy++) {
185                 vertImg.setRGB(yy, img.getWidth()-xx-1, img.getRGB(xx, yy));
186             }
187         }
188 
189         graphics.drawImage(vertImg, x, y, new ImageObserver() {
190 
191             public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
192                 return false;
193             }
194 
195         });
196 
197     }
198 
199 }