/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package routegui; import java.awt.Graphics; import javax.swing.JPanel; /** * * @author rick */ public class Compass extends JPanel { private int angle = 0; private int margin = 10; @Override public void paintComponent(Graphics g) { super.paintComponent(g); // paints background int height = this.getHeight(); int width = this.getWidth(); g.drawOval(margin, margin, height - (2 * margin), width - (2 * margin)); /* Make 0 degrees act like north */ double angle_rad = (angle - 90) * 2.0 * Math.PI/360.0; int len = (height / 2) - 2 * margin; int xMiddle = width / 2; int yMiddle = height / 2; g.drawLine(xMiddle, yMiddle, xMiddle + (int)(Math.cos(angle_rad) * len) , yMiddle + (int)(Math.sin(angle_rad) * len)); } public void setAngle(int angleNew) { angle = angleNew; this.repaint(); } public void setMargin(int marginNew) { margin = marginNew; this.repaint(); } }