package demo.helicopter; import java.awt.*; import java.awt.geom.*; import java.awt.geom.Arc2D.Double; import java.awt.event.*; import javax.swing.*; /** * * @author Thomas Miller * @version 1.0 */ public class Compass extends JPanel { /** Creates new Compass */ /** * @param int compassSetting - the compass setting */ private int compassSetting; /** * @param final int RADIUS - set to 20 */ private final int RADIUS = 20; /** * @param final int OFFSET - set to 90 */ private final int OFFSET = 90; /** * @param final int START_POSITION - set to 10 */ private final int START_POSITION = 10; /** * @param final double LINE-WIDTH - set to 1.0 */ private final double LINE_WIDTH = 1.0; /** * Compass constructor * @param degree is the current entity heading in degrees * @param idNum is the entities ID Number */ public Compass ( int degree, short idNum ) { compassSetting = OFFSET - degree; if ( idNum < 30 ) { setBackground ( Color.red ); } // end if else { setBackground ( Color.blue ); } // end else JLabel northLabel = new JLabel (); // northLabel.setText ( "N" ); // northLabel.setForeground ( Color.yellow ); // northLabel.setBounds(new Rectangle( 28, 5, 5, 5)); setPreferredSize ( new Dimension ( 60, 60 )); } // end argumented constructor /** * paintComponent paints the panel holding the compass * @param Graphics g is the graphics context * @return void */ public void paintComponent ( Graphics g ) { super.paintComponent ( g ); Graphics2D g2 = ( Graphics2D ) g; int diameter = 2 * RADIUS; g.setColor ( Color.yellow ); g.drawOval ( START_POSITION, START_POSITION, diameter, diameter ); g.fillOval ( START_POSITION, START_POSITION, diameter, diameter ); g2.drawString ( "N", 26, 9 ); g.setColor ( Color.black ); Arc2D arrow = new Arc2D.Double (( double ) START_POSITION, ( double ) START_POSITION, ( double ) diameter, ( double ) diameter, ( double ) compassSetting, LINE_WIDTH, Arc2D.PIE ); g2.draw ( arrow ); g2.fill ( arrow ); } // end paintComponent /** * setCompassSetting sets the new direction as a compass position * @param int newSetting is a new heading angle in degrees * @return void */ public void setCompassSetting ( int newSetting ) { compassSetting = OFFSET - newSetting; } // end setCompassSetting } // end Compass