[原创]代码范例:画一个美观的箭头

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

[原创]画一个美观的箭头

/**

* @(#) 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);

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航