资源简介
java中画带箭头的线
代码片段和文件信息
package com.swing.line;
import java.awt.Color;
import java.awt.frame;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
import java.util.Vector;
public class DrawLine extends frame {
//装载画线容器
Vector Lines = new Vector();
//选中的画线对象
PaintLine checkline = null;
int orgX;
int orgY;
/**
*箭头的绘画范围
*/
transient protected Polygon polygon;
public void paint(Graphics g) {
g.setColor(Color.red);
Enumeration e = Lines.elements();
while (e.hasMoreElements()) {
PaintLine In = (PaintLine) e.nextElement();
In.drawLine(g);
}
}
public void init() {
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
((Window) e.getSource()).dispose();
System.exit(0);
}
});
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
orgX = e.getX();
orgY = e.getY();
PaintLine f = new PaintLine();
checkline = f.isInLine(Lines orgX orgY);
if (checkline != null) {
Graphics g = e.getComponent().getGraphics();
//paintk(g);
// g.setColor(Color.blue);
// g.drawLine(checkline.x1 checkline.y1 checkline.x2 checkline.y2);
}
}
public void mouseReleased(MouseEvent e) {
Graphics g = e.getComponent().getGraphics();
g.setColor(Color.red);
if (orgX != e.getX() || orgY != e.getY()) {
//g.drawLine(orgX orgY e.getX() e.getY());
paintk(g orgX orgY e.getX() e.getY());
Lines.add(new PaintLine(orgX orgY e.getX() e.getY()));
} else {
//删除选择中的画线
if (checkline != null) {
Lines.remove(checkline);
repaint();
}
}
}
});
this.setSize(300 300);
setVisible(true);
}
public static void main(String[] args) {
DrawLine f = new DrawLine();
f.init();
}
public void paintk(Graphics g int x1 int y1 int x2 int y2) {
double H = 10; //箭头高度
double L = 7;//底边的一半
int x3 = 0;
int y3 = 0;
int x4 = 0;
int y4 = 0;
double awrad = Math.atan(L / H); //箭头角度
double arraow_len = Math.sqrt(L * L + H * H);//箭头的长度
double[] arrXY_1 = rotateVec(x2 - x1 y2 - y1 awrad true arraow_len);
double[] arrXY_2 = rotateVec(x2 - x1 y2 - y1 -awrad true arraow_len);
double x_3 = x2 - arrXY_1[0]; //(x3y3)是第一端点
double y_3 = y2 - arrXY_1[1];
double x_4 = x2 - arrXY_2[0];//(x4y4)是第二端点
double y_4 = y2 - arrXY_2[1];
Double X3 = new Double(x_3);
x3 = X3.intValue();
Double Y3 = new Double(y_3);
y3 = Y3.intValue();
Double X4 = new Double(x_4);
x4 = X4.intValue();
Double Y4 = new Double(y_4);
y4 = Y4.intValue();
g.setColor(Color.blue);
g.drawLine(x1 y1 x2 y2);
g.drawLine(x2 y2 x3 y3);
g.drawLine(x2 y2 x
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3585 2007-09-14 18:12 画线\DrawLine.java
文件 2077 2007-08-30 15:13 画线\PaintLine.java
目录 0 2007-09-14 18:15 画线
----------- --------- ---------- ----- ----
5662 3
评论
共有 条评论