1 package net.sf.openrocket.gui.components.compass;
4 import java.awt.Dimension;
6 import java.awt.Graphics;
7 import java.awt.Graphics2D;
8 import java.awt.RenderingHints;
9 import java.awt.font.GlyphVector;
10 import java.awt.geom.Rectangle2D;
12 import javax.swing.JComponent;
14 import net.sf.openrocket.l10n.Translator;
15 import net.sf.openrocket.startup.Application;
18 * A component that draws a compass rose. This class has no other functionality, but superclasses
19 * may add functionality to it.
21 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
23 public class CompassRose extends JComponent {
24 private static final Translator trans = Application.getTranslator();
27 private static final Color MAIN_COLOR = new Color(0.4f, 0.4f, 1.0f);
28 private static final float MAIN_LENGTH = 0.95f;
29 private static final float MAIN_WIDTH = 0.15f;
31 private static final int CIRCLE_BORDER = 2;
32 private static final Color CIRCLE_HIGHLIGHT = new Color(1.0f, 1.0f, 1.0f, 0.7f);
33 private static final Color CIRCLE_SHADE = new Color(0.0f, 0.0f, 0.0f, 0.2f);
35 private static final Color MARKER_COLOR = Color.BLACK;
38 private double scaler;
40 private double markerRadius;
41 private Font markerFont;
45 * Construct a compass rose with the default settings.
47 public CompassRose() {
48 this(0.8, 1.1, Font.decode("Serif-PLAIN-16"));
53 * Construct a compass rose with the specified settings.
55 * @param scaler The scaler of the rose. The bordering circle will we this portion of the component dimensions.
56 * @param markerRadius The radius for the marker positions (N/E/S/W), or NaN for no markers. A value greater than one
57 * will position the markers outside of the bordering circle.
58 * @param markerFont The font used for the markers.
60 public CompassRose(double scaler, double markerRadius, Font markerFont) {
62 this.markerRadius = markerRadius;
63 this.markerFont = markerFont;
69 public void paintComponent(Graphics g) {
71 Graphics2D g2 = (Graphics2D) g;
75 Dimension dimension = this.getSize();
77 int width = Math.min(dimension.width, dimension.height);
79 width = (int) (scaler * width);
81 int mainLength = (int) (width * MAIN_LENGTH / 2);
82 int mainWidth = (int) (width * MAIN_WIDTH / 2);
85 g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
86 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
88 g2.setColor(MAIN_COLOR);
94 y[1] = mid - mainLength;
95 x[2] = mid - mainWidth;
96 y[2] = mid - mainWidth;
97 g2.fillPolygon(x, y, 3);
99 x[2] = mid + mainWidth;
100 g2.drawPolygon(x, y, 3);
105 x[1] = mid + mainLength;
107 x[2] = mid + mainWidth;
108 y[2] = mid - mainWidth;
109 g2.fillPolygon(x, y, 3);
111 y[2] = mid + mainWidth;
112 g2.drawPolygon(x, y, 3);
118 y[1] = mid + mainLength;
119 x[2] = mid + mainWidth;
120 y[2] = mid + mainWidth;
121 g2.fillPolygon(x, y, 3);
123 x[2] = mid - mainWidth;
124 g2.drawPolygon(x, y, 3);
129 x[1] = mid - mainLength;
131 x[2] = mid - mainWidth;
132 y[2] = mid + mainWidth;
133 g2.fillPolygon(x, y, 3);
135 y[2] = mid - mainWidth;
136 g2.drawPolygon(x, y, 3);
140 g2.setColor(CIRCLE_SHADE);
141 g2.drawArc(mid - width / 2 + CIRCLE_BORDER, mid - width / 2 + CIRCLE_BORDER,
142 width - 2 * CIRCLE_BORDER, width - 2 * CIRCLE_BORDER, 45, 180);
143 g2.setColor(CIRCLE_HIGHLIGHT);
144 g2.drawArc(mid - width / 2 + CIRCLE_BORDER, mid - width / 2 + CIRCLE_BORDER,
145 width - 2 * CIRCLE_BORDER, width - 2 * CIRCLE_BORDER, 180 + 45, 180);
148 // Draw direction markers
149 if (!Double.isNaN(markerRadius) && markerFont != null) {
151 int pos = (int) (width * markerRadius / 2);
153 g2.setColor(MARKER_COLOR);
154 drawMarker(g2, mid, mid - pos, trans.get("lbl.north"));
155 drawMarker(g2, mid + pos, mid, trans.get("lbl.east"));
156 drawMarker(g2, mid, mid + pos, trans.get("lbl.south"));
157 drawMarker(g2, mid - pos, mid, trans.get("lbl.west"));
165 private void drawMarker(Graphics2D g2, float x, float y, String str) {
166 GlyphVector gv = markerFont.createGlyphVector(g2.getFontRenderContext(), str);
167 Rectangle2D rect = gv.getVisualBounds();
169 x -= rect.getWidth() / 2;
170 y += rect.getHeight() / 2;
172 g2.drawGlyphVector(gv, x, y);
180 public double getScaler() {
185 public void setScaler(double scaler) {
186 this.scaler = scaler;
191 public double getMarkerRadius() {
196 public void setMarkerRadius(double markerRadius) {
197 this.markerRadius = markerRadius;
202 public Font getMarkerFont() {
207 public void setMarkerFont(Font markerFont) {
208 this.markerFont = markerFont;
213 public Dimension getPreferredSize() {
214 Dimension dim = super.getPreferredSize();
215 int min = Math.min(dim.width, dim.height);
216 dim.setSize(min, min);