{ super(title); this.setSize(500, 580); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.getContentPane().setLayout(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.show=new MyPanel(); this.show.setBounds(0,0,this.getWidth(),this.getHeight()-80); this.begin=new JButton(\开始绘制\ this.begin.addActionListener(new MyButtonMonitor()); this.begin.setBounds(this.getWidth()/2-70, this.getHeight()-75,100,30); this.getContentPane().add(show); this.getContentPane().add(begin); //this.pack(); this.setVisible(true); }
public class MyPanel extends JPanel { public HashMap
g.setColor(Color.LIGHT_GRAY); g.fillRect(0, 0, 500, 500); g.setColor(Color.red); for(int i=1;i<=this.pointSet.size();i++) { g.fillOval(this.pointSet.get(i).x-2,this.pointSet.get(i).y-2,5,5); } if(!edgeSet.isEmpty()) { for(int i=1;i<=this.edgeSet.size();i++) { this.edgeSet.get(i).draw(g, pointSet); } } }
class MyMouseMonitor implements MouseListener { public void mouseClicked(MouseEvent e) { Point temp=e.getPoint(); MyPoint p=new MyPoint(temp.x,temp.y); pointSet.put(p.id,p); repaint(); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) {
} } }
class MyButtonMonitor implements ActionListener { HashMap
* 点击“开始绘制”按钮后就会开始绘制
* 这里假定以点集中的第一个点为初始点进行绘制 */
MyPoint firstPoint=pointSet.get(1);
MyPoint secondPoint=firstPoint.getMinDistancePoint(pointSet);//找距初始点最近的点 MyEdge firstEdge=new MyEdge(firstPoint,secondPoint);//生成的第一个边 edgeSet.put(firstEdge.id, firstEdge);//将第一个边加入到边集中 MyTin.this.show.repaint(); int i=1; while(!edgeSet.isEmpty()) { MyEdge activeEdge=null; for( ;i<=edgeSet.size();i++) { activeEdge=edgeSet.get(i); if(activeEdge.getUseCount()<2) break; } if(i>edgeSet.size())break; //这是采用外接圆的方法找最优点 // MyPoint goodPoint=activeEdge.findGoodPointByCircle(pointSet, edgeSet, triangleSet); //这是采用余弦值的方法找最优点 MyPoint goodPoint=activeEdge.findGoodPointByAngle(pointSet, edgeSet,
triangleSet); if(goodPoint==null) activeEdge.addUseCount(); } MyTin.this.show.repaint(); } }
}import java.util.*; import java.awt.*;
public class MyTriangle { public static int count=0; public int id;
private int[] pID;//记录三角形中的点,按顺时针顺序排列 private int[] eID;//记录三角形中的边,按顺时针顺序排列 /*
* 下面这个构造方法是简单三角形构造法,其中没有边的信息 * 可以用它来做判断
* 建议最后生成的三角形不要用此构造方法 */
public MyTriangle(int p1ID,int p2ID,int p3ID) { this.pID=new int[3]; this.pID[0]=p1ID; this.pID[1]=p2ID; this.pID[2]=p3ID; } /*
* 注意此构造方法中的三点要满足顺时针的顺序 */
public MyTriangle(MyPoint p1,MyPoint p2,MyPoint p3,Map
*此构造方法也是根据点来确定三角形
*但是传入的点是按照顺时针的顺序传入点的ID号 */
public MyTriangle(int p1ID,int p2ID,int p3ID,Map
* 下面这个构造方法是按照边对象来构造三角形的 * 注意,边的顺序也要是按照顺时针的顺序进入 */ public MyTriangle(MyEdge e1,MyEdge e2,MyEdge e3,Map
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库基于狄洛尼三角网生成算法的源代码(3)在线全文阅读。
相关推荐: