1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
package test;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
/** Displays a radar.
*/
public class RadarComponent extends JComponent {
/** Default width.
*/
public static int DEFAULT_WIDTH = 800;
/** Default height.
*/
public static int DEFAULT_HEIGHT = 800;
/** Back base paint.
* @todo Make this a configurable property.
*/
private static final Color BACK_RADAR_PAINT = Color.BLACK;
/** Front base paint.
* @todo Make this a configurable property.
*/
private static final Color FRONT_RADAR_PAINT = Color.GREEN;
/** Paint of the passive area.
* @todo Make this a configurable property.
*/
private static final Color FRONT_PASSIVE_RADAR_PAINT = FRONT_RADAR_PAINT.darker().darker();
/** Paint of the active line.
* @todo Make this a configurable property.
*/
private static final Color FRONT_ACTIVE_RADAR_PAINT = FRONT_RADAR_PAINT;
/** Paint of the echo area.
* @todo Make this a configurable property.
*/
private static final Color FRONT_ACTIVE_RADAR_TRANSPARENT_PAINT = new Color(FRONT_RADAR_PAINT.getRed(), FRONT_RADAR_PAINT.getGreen(), FRONT_RADAR_PAINT.getBlue(), 0);
/** Stroke of the passive line.
* @todo Make this a configurable property.
*/
private static final Stroke PASSIVE_STROKE = new BasicStroke(0.8f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
/** Stroke of the active line.
* @todo Make this a configurable property.
*/
private static final Stroke ACTIVE_STROKE = new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
/** Default sleep time.
* @todo Make this a configurable property.
*/
private int SLEEP_TIME = 16;
/** Listener for timer events and component resizing events.
*/
private InnerListener innerListener = new InnerListener();
/** Timer for display refresh.
*/
private Timer timer = new Timer(SLEEP_TIME, innerListener);
/** Re-usable line.
*/
private Line2D.Float line = new Line2D.Float();
/** Re-usable pie slice shape.
*/
private Arc2D.Float arc = new Arc2D.Float();
private Rectangle2D arcRect = new Rectangle2D.Float();
/** Re-usable circular frame.
*/
private Ellipse2D.Float ellipse = new Ellipse2D.Float();
/** Current paint for the echo area.
*/
private GradientPaint gradient;
/** Current radius in pixels.
*/
private float radius;
/** Current diameter in pixels.
*/
private float diameter;
/** Current X coordinate of the center in pixels.
*/
private float centerX;
/** Current Y coordinate of the center in pixels.
*/
private float centerY;
/** Current X coordinate of the top left corner of the radar area in pixels.
*/
private float radarX;
/** Current Y coordinate of the top left corner of the radar area in pixels.
*/
private float radarY;
/** Current angle in radians.
*/
double angle = 0;
/** Angle offset in radians.
* @todo Make this a configurable property.
*/
double da = 2 * Math.PI / 1000;
/** Creates a new instance.
*/
public RadarComponent() {
super();
setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_WIDTH));
precalculate();
addComponentListener(innerListener);
timer.start();
}
/** Precalculates and sets all necessary measures and objects.
*/
private void precalculate() {
int width = getWidth();
int height = getHeight();
radius = Math.min(width, height) / 2f;
if (radius != 0) {
diameter = 2 * radius;
centerX = width / 2f;
centerY = height / 2f;
radarX = centerX - radius;
radarY = centerY - radius;
// Note arc intialization angles are in degrees.
arc.setArc(new Rectangle2D.Double(radarX, radarY, diameter, diameter), 0f, 20f, Arc2D.PIE);
arcRect.setRect(centerX, centerY - radius / 8f, radius, radius / 8f);
// Rescale gradient accordingly.
// Note : this is a "should look OK" approximation. Could be better.
gradient = new GradientPaint(centerX, centerY - radius / 8f, FRONT_ACTIVE_RADAR_TRANSPARENT_PAINT, centerX, centerY, FRONT_RADAR_PAINT, false);
}
}
/** {@inheritDoc}
*/
@Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
if ((radius > 0) && isVisible() && isShowing()) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// Paint back circle.
g2d.setPaint(BACK_RADAR_PAINT);
ellipse.setFrame(radarX, radarY, diameter, diameter);
g2d.fill(ellipse);
// Paint axes.
g2d.setStroke(PASSIVE_STROKE);
g2d.setPaint(FRONT_PASSIVE_RADAR_PAINT);
float dx = radius / 4f;
for (int i = 0; i < 5; i++) {
ellipse.setFrame(radarX + i * dx, radarY + i * dx, diameter - 2 * i * dx, diameter - 2 * i * dx);
g2d.draw(ellipse);
}
line.setLine(radarX, centerY, radarX + diameter, centerY);
g2d.draw(line);
line.setLine(centerX, radarY, centerX, radarY + diameter);
g2d.draw(line);
// Backup current transformation.
AffineTransform backTransform = g2d.getTransform();
// Rotate to proper angle.
g2d.rotate(angle, centerX, centerY);
//Paint echo zone.
g2d.setPaint(gradient);
g2d.fill(arc);
//g2d.fill(arcRect);
// Paint active line.
g2d.setStroke(ACTIVE_STROKE);
g2d.setPaint(FRONT_ACTIVE_RADAR_PAINT);
line.setLine(centerX, centerY, centerX + radius, centerY);
g2d.draw(line);
//g2d.rotate(-angle, centerX, centerY);
// Restore transformation, avoid possible rounding error.
g2d.setTransform(backTransform);
}
}
/** Program entry point.
* @param args Arguments from the command line.
*/
public static void main(String ...args) {
Runnable runnable = new Runnable() {
/** {@inheritDoc}
*/
public void run() {
JFrame myFrame = new JFrame("A radar");
myFrame.add(new RadarComponent());
myFrame.pack();
//myFrame.setResizable(false);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
}
};
SwingUtilities.invokeLater(runnable);
}
/** Inner listener for this component
*/
private class InnerListener implements ActionListener, ComponentListener {
/** Creates a new instanc.
*/
public InnerListener() {
}
/** {@inheritDoc}
* <br>Reacts to timer events.
*/
public void actionPerformed(ActionEvent event) {
angle += da;
if (isVisible() && (isShowing())) {
repaint();
}
}
/** {@inheritDoc}
* <br>Rescale graphical objects according to new size.
*/
public void componentResized(ComponentEvent event) {
precalculate();
if (isVisible() && (isShowing())) {
repaint();
}
}
/** {@inheritDoc}
*/
public void componentMoved(ComponentEvent event) {
}
/** {@inheritDoc}
*/
public void componentShown(ComponentEvent event) {
}
/** {@inheritDoc}
*/
public void componentHidden(ComponentEvent e) {
}
}
} |
Partager