日期:2014-05-20 浏览次数:21017 次
/* <HTML> <HEAD> <TITLE>A Simple Program</TITLE> </HEAD> <BODY bgcolor=gray> Here is the output of my program:<BR> <APPLET CODE="HelloAppletWorld_Mouse.class" WIDTH=650 HEIGHT=550 > </APPLET> </BODY> </HTML> */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class HelloAppletWorld_Mouse extends Applet implements MouseListener,MouseMotionListener { ArrayList<_2DPoint> m_listPoint = new ArrayList<_2DPoint>(4); _2DPoint m_obj5thPoint = null; _2DPoint m_objMovingPoint = null; ArrayList<_2DPoint> m_listTopoWhen3P = null; String m_strPosDesc = null; int[] m_nNo4Pos = new int[3]; ArrayList<_2DPoint> m_listAux4P = new ArrayList<_2DPoint>(); _2DPoint m_objCrossPoint = null; Triangle T1= null; Triangle T2= null; boolean m_b5InArea = false; public void init() { resize(800,800); addMouseListener(this); addMouseMotionListener(this); } public void paint(Graphics g) { if(m_listPoint.size()<1) return; drawer painter = new drawer((Graphics2D)g); if(T1!=null&&T2!=null) { painter.draw(T1,Color.red); painter.draw(T2,Color.blue); } _2DPoint lastP = null; if(m_listPoint.size()>0) { lastP = m_listPoint.get(0); painter.draw(lastP,Color.red); } if(m_listPoint.size()>1) { lastP = m_listPoint.get(1); painter.draw(lastP,Color.red); painter.draw(lastP,m_listPoint.get(0),Color.green); } if(m_listPoint.size()>2) { lastP = m_listPoint.get(2); painter.draw(lastP,Color.red); painter.draw(lastP,m_listPoint.get(1),Color.green); painter.draw(m_listPoint.get(0),m_listPoint.get(1),Color.green); } if(m_listPoint.size()>3) { lastP = m_listPoint.get(3); painter.draw(lastP,Color.red); painter.draw(lastP,m_listPoint.get(2),Color.green); painter.draw(m_listPoint.get(2),m_listPoint.get(1),Color.green); painter.draw(m_listPoint.get(1),m_listPoint.get(0),Color.green); painter.draw(m_listPoint.get(0),lastP,Color.green); } if(m_listPoint.size()<4) { painter.draw(m_objMovingPoint,lastP,Color.blue); } if(m_b5InArea) painter.draw2(m_objMovingPoint,Color.green); else painter.draw(m_objMovingPoint,Color.red); if(m_listTopoWhen3P!=null) { for(int i=0;i<m_listTopoWhen3P.size();i=i+2) { painter.draw(m_listTopoWhen3P.get(i),m_listTopoWhen3P.get(i+1),Color.pink); } } if(m_strPosDesc!=null) g.drawString(m_strPosDesc,10,10); if(m_listAux4P.size()>0) { painter.draw(m_listAux4P.get(0),m_listAux4P.get(1),Color.blue); } if(m_objCrossPoint!=null) { painter.draw(m_objCrossPoint,Color.yellow); } } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e){} public void mousePressed(MouseEvent e){} public void mouseClicked(MouseEvent e) { if(e.getButton()==1 && m_listPoint.size()<4){ _2DPoint p = new _2DPoint(e.getX(),e.getY()); m_listPoint.add(p); p.setIndex(m_listPoint.size()); if(m_listPoint.size()==3) { m_listTopoWhen3P = _2DPoint.load123Topo(m_listPoint.get(0),m_listPoint.get(1),m_listPoint.get(2)); } if(m_listPoint.size()==4) { int _4_1_2 = m_nNo4Pos[0]; int _4_2_3 = m_nNo4Pos[1]; int _4_3_1 = m_nNo4Pos[2]; if(_4_1_2==1&&_4_2_3==1&&_4_3_1==1) //三点内部 { m_listAux4P.add(m_listPoint.get(1)); m_listAux4P.add(m_listPoint.get(3)); T1 = new Triangle(m_listPoint.get(0),m_listPoint.get(1),m_listPoint.get(3)); T2 = new Triangle(m_listPoint.get(1),m_listPoint.get(2),m_listPoint.get(3)); }else if((_4_1_2*_4_2_3)*_4_3_1==1) //三个对角区域 { if(_4_2_3==1){ m_listAux4P.add(m_listPoint.get(0)); m_listAux4P.add(m_listPoint.get(2)); T1 = new Triangle(m_listPoint.get(0),m_listPoint.get(2),m_listPoint.get(3)); T2 = new Triangle(m_listPoint.get(1),m_listPoint.get(2),m_listPoint.get(0)); } if(_4_3_1==1){ m_listAux4P.add(m_listPoint.get(1)); m_listAux4P.add(m_listPoint.get(3)); T1 = new Triangle(m_listPoint.get(0),m_listPoint.get(1),m_listPoint.get(3)); T2 = new Triangle(m_listPoint.get(1),m_listPoint.get(2),m_listPoint.get(3)); } if(_4_1_2==1){ m_listAux4P.add(m_listPoint.get(0)); m_listAux4P.add(m_listPoint.get(2)); T1 = new Triangle(m_listPoint.get(0),m_listPoint.get(2),m_listPoint.get(3)); T2 = new Triangle(m_listPoint.get(1),m_listPoint.get(2),m_listPoint.get(0)); } }else //三个边外区域 { if(_4_3_1==-1) { m_listAux4P.add(m_listPoint.get(1)); m_listAux4P.add(m_listPoint.get(3)); T1 = new Triangle(m_listPoint.get(0),m_listPoint.get(1),m_listPoint.get(3)); T2 = new Triangle(m_listPoint.get(1),m_listPoint.get(2),m_listPoint.get(3)); } if(_4_1_2==-1) { //34 12求交 Line l1 = new Line(m_listPoint.get(2),m_listPoint.get(3)); Line l2 = new Line(m_listPoint.get(0),m_listPoint.get(1)); m_objCrossPoint = Line.getCrossPoint(l1,l2); m_objCrossPoint.setIndex(6); T1 = new Triangle(m_listPoint.get(0),m_listPoint.get(3),m_objCrossPoint); T2 = new Triangle(m_listPoint.get(1),m_listPoint.get(2),m_objCrossPoint); } if(_4_2_3==-1) { //14 23求交 Line l1 = new Line(m_listPoint.get(3),m_listPoint.get(0)); Line l2 = new Line(m_listPoint.get(2),m_listPoint.get(1)); m_objCrossPoint = Line.getCrossPoint(l1,l2); m_objCrossPoint.setIndex(6); T1 = new Triangle(m_listPoint.get(0),m_listPoint.get(1),m_objCrossPoint); T2 = new Triangle(m_listPoint.get(3),m_listPoint.get(2),m_objCrossPoint); } } } repaint(); }else if(e.getButton()==3 && m_listPoint.size()==4) { m_listPoint.remove(3); m_listAux4P.clear(); m_objCrossPoint=null; T1=null; T2=null; repaint(); }else if(e.getButton()==3 && m_listPoint.size()==3) { m_listPoint.remove(2); m_listTopoWhen3P.clear(); m_listTopoWhen3P=null; repaint(); } } public void mouseDragged(MouseEvent e){} public void mouseMoved(MouseEvent e) { m_objMovingPoint = new _2DPoint(e.getX(),e.getY()); m_objMovingPoint.setIndex(m_listPoint.size()+1); if(m_listPoint.size()==3) { _2DVector.locateP4(m_nNo4Pos,m_listPoint.get(0),m_listPoint.get(1),m_listPoint.get(2),m_objMovingPoint); m_strPosDesc="4-1-2:"+m_nNo4Pos[0]+"; 4-2-3:"+m_nNo4Pos[1]+";4-3-1:"+m_nNo4Pos[2]; } if(T1!=null&&T2!=null) { m_b5InArea = T1.contains(m_objMovingPoint)||T2.contains(m_objMovingPoint); } repaint(); } }