package applettest;
import java.awt.event.*;
import java.util.Vector;
/**
* <p>Title:center画布的类 </p>
* <p>Description: 画法</p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.awt.*;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ArcCanvas extends Canvas implements MouseListener,MouseMotionListener{ //类ArcCanvas
boolean filled = false;
private Node node;//当前画的节点
//private Node SelectNode;//选中的节点
private Arc arc;//当前画的弧段
//private int Type;//根据工具栏选择花的类别,1代表矩形,2代表折线
private Graphics g;
//节点向量
private Vector NodeObject;
private int NodeOBLenght;
private String CurrentNodeName = "";
//弧段向量
private Vector ArcObject;
private int ArcOBLength;
private boolean IfDrag;//拖拉矩形框大小
private boolean SelectNodeDrag = false;//选中后拖拉节点
private boolean SelectArcDrag = false;//选中后托放弧段
private boolean haveSelectedNode = false;//是否有选中的矩形
private boolean haveSelectedArc = false;//是否有选中的弧段
private boolean ArcIfinit = true;//弧段是否是第一次初始化
private boolean NodeIFinit = true;//节点是否第一次初始化
private int status;//当前状态 1为节点图,2为弧段图,3为空闲状态
private int nodeindex;//选中矩形的位置下标
private int arcindex;//选中弧段的位置下标
private int arcNodeindex = -1;//选中弧线上那一个节点
private int ox,oy;//弧线平移时使用,记录当鼠标按下第一次的位置坐标
private Vector endNode = new Vector();
private Vector arcList = new Vector();
public ArcCanvas()
{
//this.setBackground(new Color(123,145,226));
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.NodeObject = new Vector();
this.ArcObject = new Vector();
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void SetNodeName(String name){
CurrentNodeName = name;
}
public void SetType() {
}
//画对象函数,从向量中取出对象,画出来
public void paint(Graphics g) {
NodeOBLenght = this.NodeObject.size();
ArcOBLength = this.ArcObject.size();
System.out.println("Node Vector size: "+NodeOBLenght);
System.out.println("Arc Vector size: "+ArcOBLength);
//画节点框图
if (NodeOBLenght>0){
for(int i=0;i<NodeOBLenght;i++){
Node ob = (Node)this.NodeObject.elementAt(i); // elementAt(i)返回指定索引处的组件。
ob.DrawMyself(g);
ob.SetNodeColor(Color.BLACK);
}
}
//画弧段折线图
if(ArcOBLength>0){
for(int i=0;i<ArcOBLength;i++){
Arc ob = (Arc)this.ArcObject.elementAt(i);
ob.DrawMyself(g);
ob.SetArcColor(Color.BLACK);
}
}
g.translate(2,2);
}
public void redraw() { //重画方法
repaint();
//通过调用repaint()方法,从而最终调用paint方法完成重画
}
public void mousePressed(MouseEvent e) {//鼠标按下
switch (status) {
case 1:
break;
case 2:
break;
case 3:
ox = e.getX();
oy = e.getY();
// System.out.println(" press ox is : " + ox);
// System.out.println("press oy is : " + oy);
break;
}
}
public void mouseClicked(MouseEvent e) {//鼠标点
int button = e.getButton();
int x = e.getX();
int y = e.getY();
switch (status) {
case 1:
switch (button) {
case 1:
System.out.println("search node");
Node tempnode = SearchNode(x, y);
if (tempnode != null) {
tempnode.SetNodeColor(Color.RED);
node = tempnode;
repaint();
}
break;
case 2:
break;
case 3:
this.getGraphics().drawString("测试字符串", e.getX(), e.getY());
break;
}
this.IfDrag = false;//拖动假
break;
case 2:
switch (button) {
case 1:
if (this.ArcIfinit == true) {
arc = new Arc(e.getX(), e.getY());
this.ArcObject.addElement(arc);
this.ArcIfinit = false;
}
else {
//System.out.print(ArcIfinit);
int[] tep = new int[2];
tep[0] = e.getX();
tep[1] = e.getY();
arc.AddPoint(tep);
}
repaint();
break;
case 2:
break;
case 3:
if (arc != null) {
arc.SetArcEnd();
int[] tep = new int[2];
tep[0] = e.getX();
tep[1] = e.getY();
arc.SetArcEndCoor(tep[0],tep[1]);
//arc.AddPoint(tep);
}
this.status = 3;//绘制弧段已经结束,设置为空闲状态;
repaint();
break;
}
break;
case 3://搜索弧段的状态
switch (button) {
case 1:
// Arc temparc = this.SearchArc(x,y);
Arc temparc = this.SearchArcTow(x,y);
if(temparc==null){
System.out.println("aearch arc is null");
}else{
arc = temparc;//付给当前的arc对象
temparc.SetArcColor(Color.RED);
repaint();
}
break;
case 2:
break;
case 3:
this.getGraphics().drawString("测试字符串", e.getX(), e.getY());
break;
}
break;
}
}
public void mouseReleased(MouseEvent e) {//鼠标松开
switch (status) {
case 1:
this.NodeIFinit = false;
this.SelectNodeDrag = false;
this.endNode.add(node);
break;
case 2:
//this.ArcIfinit = false;
case 3:
SelectArcDrag = false;//松开鼠标后将拖放弧段设置假,要再次拖动必须先选
// this.arcList.add(o);
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseDragged(MouseEvent e) { //鼠标拖动
int x = e.getX();
int y = e.getY();
switch(status){
case 1:
if (this.NodeIFinit == true) {
node = new Node(e.getX(), e.getY(),CurrentNodeName);
this.NodeObject.add(node);
this.IfDrag = true;
this.NodeIFinit = false;
repaint();
}
else {
if (IfDrag == true) {
if (node != null) {
node.SetHeightAndWidth(x, y);
repaint();
}
}
else {
if (SelectNodeDrag == true) {
node.SetNodeMoved(x, y);
node.DrawMyself(this.getGraphics());
repaint();
}
}
}
break;
case 2:
break;
case 3:
if (SelectArcDrag == true) {
if ( (arc != null) && (arcNodeindex != -1)) {
arc.SetSelectedNode(arcNodeindex, x, y);
repaint();
}else if((arc != null)&&(arcNodeindex == -1)){//选中的是最后一个不再Vetor里面的点
arc.SetSelectedNode(x, y);
repaint();
}
}
break;
}
}
public void mouseMoved(MouseEvent e) {
switch (status) {
case 1:
break;
case 2:
if ( (arc != null) && (arc.GetArcIFEnd() == false)) {
arc.SetCurrentCursor(e.getX(), e.getY());
//System.out.println("Current point: x is " + e.getX() + " y is " +
// e.getY());
repaint();
}
break;
}
}
public Node SearchNode(int x,int y){
Node tempNode = null;
if(NodeOBLenght!=0){
for(int i=0;i<NodeOBLenght;i+