[原创]画一个美观的箭头
/** * @(#) ArraowLinePanel.java * * Copyright 2004 Opensource Develop Team. All rights reserved. */
// package pakcage com.opensource.arrow;
// import classes import java.awt.*; import java.swing.*; import java.awt.geom.*:
/** * 在Panel上画一个箭头 * * @author ODT * @version 1.0 22/04/2004 * @see JPanel * @since 1.3 */ class ArrowLinePanel extends JPanel { // confirm the line position Line2D line = new Line2D.Float(100f, 100f, 300f, 300f);
ArrowLinePanel() { setBackground(Color.white); }
public void paintComponent(Graphics g) { super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g; g2D.setColor(Color.red); g2D.draw(line); // draw line without arrow.
double x1 = line.getX1(); double x2 = line.getX2(); double y1 = line.getY1(); double y2 = line.getY2(); // compute the line's length double length = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); g2D.translate(x1, y1); // compute the angle between line and X axis. double roate = Math.atan((y2-y1)/(x2-x1)); // draw arrow GeneralPath path = new GeneralPath(); path.moveTo((float)length, 0); path.lineTo((float)length - 10, -5); path.lineTo((float)length - 7, 0); path.lineTo((float)length - 10, 5); path.lineTo((float)length, 0); path.closePath();
Area area = new Area(path); g2D.fill(area); } } 
|