1
2
3
4
5
6
7
8
9
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
26
27
28
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
68
69
70
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
113
114
115
116
117
118
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
162
163
164
165
166
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 }