import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class TrigArrow extends JPanel {
Point p1 = new Point(100,100);
Point p2 = new Point(300,300);
double dia = 25.0;
double barb = 15.0;
double phi = Math.toRadians(20);
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.red);
g2.fill(new Ellipse2D.Double(p1.x-1.5, p1.y-1.5, 4, 4));
g2.fill(new Ellipse2D.Double(p2.x-1.5, p2.y-1.5, 4, 4));
g2.setPaint(Color.blue);
g2.draw(new Ellipse2D.Double(p1.x-dia/2, p1.y-dia/2, dia, dia));
g2.draw(new Ellipse2D.Double(p2.x-dia/2, p2.y-dia/2, dia, dia));
double dy = p2.y - p1.y;
double dx = p2.x - p1.x;
double theta = Math.atan2(dy, dx);
double x1 = p1.x + (dia/2)*Math.cos(theta);
double y1 = p1.y + (dia/2)*Math.sin(theta);
theta += Math.PI;
double x2 = p2.x + (dia/2)*Math.cos(theta);
double y2 = p2.y + (dia/2)*Math.sin(theta);
g2.setPaint(Color.green.darker());
g2.draw(new Line2D.Double(x1, y1, x2, y2));
double x = x2 + barb*Math.cos(theta+phi);
double y = y2 + barb*Math.sin(theta+phi);
g2.draw(new Line2D.Double(x2, y2, x, y));
x = x2 + barb*Math.cos(theta-phi);
y = y2 + barb*Math.sin(theta-phi);
g2.draw(new Line2D.Double(x2, y2, x, y));
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new TrigArrow());
f.setSize(400,400);
f.setLocation(100,100);
f.setVisible(true);
}
} |