⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linkpathdemo.java

📁 全面实现ilog地功能,没有使用第三方lib.
💻 JAVA
字号:
/*
 * This source code is part of TWaver 1.3.1
 *
 * SERVA Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2000-2005 SERVA Software, Inc. All rights reserved.
 */

package demo.network;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;
import java.awt.geom.GeneralPath;

import javax.swing.SwingUtilities;

import twaver.Link;
import twaver.Node;
import twaver.TWaverConst;
import twaver.network.LinkLayouter;
import twaver.network.TNetwork;
import twaver.network.ui.LinkUI;
import demo.DemoPane;

public class LinkPathDemo extends DemoPane {
	
	// custom link ui for listen client property "camber" change event
	public static class CustomLinkUI extends LinkUI{
		GeneralPath customPath = null;
		public CustomLinkUI(TNetwork network, Link link) {
			super(network, link);
		}
	    public void updateClientProperty(String propertyName) {
	    	super.updateClientProperty(propertyName);
	    	if(propertyName.equals("camber")){
	    		this.invalidateLinkPath();
	    	}
	    }
	}
	
    public LinkPathDemo() {
        final TNetwork network = new TNetwork();
        this.add(network, BorderLayout.CENTER);
        
        // custom link layout
        network.setLinkLayout(new LinkLayouter(){
			public void layout() {
				// do nothing;
			}
			public GeneralPath getLinkPath(LinkUI linkUI) {
				GeneralPath customPath;
	            Point p1 = linkUI.getFromPoint();
	            Point p2 = linkUI.getToPoint();
		    	if(linkUI.isBundleAgent()){
		    		customPath = new GeneralPath();
		    		customPath.moveTo(p1.x, p1.y);
		    		customPath.lineTo(p2.x, p2.y);
		    	}else{
			    	Integer value = (Integer)linkUI.getElement().getClientProperty("camber");
			    	int camber = 0;
			    	if(value != null){
			    		camber = value.intValue();
			    	}
			    	customPath = new GeneralPath();
			    	customPath.moveTo(p1.x, p1.y);
			    	customPath.quadTo(200, camber, p2.x, p2.y);
		    	}
		    	return customPath;
			}
        });
        
        final Node node1 = new Node();
        node1.setLocation(100, 100);
        network.getDataBox().addElement(node1);
        final Node node2 = new Node();
        node2.setLocation(300, 100);
        network.getDataBox().addElement(node2);
        final Node node3 = new Node();
        node3.setLocation(500, 100);
        network.getDataBox().addElement(node3);
        
        final Link link1 = new Link(node1, node2){
            public String getUIClassID() {
                return CustomLinkUI.class.getName();
            }
        };
        link1.putClientProperty(TWaverConst.PROPERTYNAME_LINK_WIDTH, new Integer(8));
        link1.putClientProperty(TWaverConst.PROPERTYNAME_LINK_COLOR, Color.cyan.darker());
        network.getDataBox().addElement(link1);
        
        final Link link2 = new Link(node2, node1){
            public String getUIClassID() {
                return CustomLinkUI.class.getName();
            }
        };
        link2.putClientProperty(TWaverConst.PROPERTYNAME_LINK_WIDTH, new Integer(8));
        link2.putClientProperty(TWaverConst.PROPERTYNAME_LINK_COLOR, Color.blue);
        network.getDataBox().addElement(link2);
        
        final Link link3 = new Link(node2, node3){
            public String getUIClassID() {
                return CustomLinkUI.class.getName();
            }
        };
        link3.putClientProperty(TWaverConst.PROPERTYNAME_LINK_WIDTH, new Integer(8));
        link3.putClientProperty(TWaverConst.PROPERTYNAME_LINK_COLOR, Color.red);
        link3.putClientProperty("camber", new Integer(200));
        network.getDataBox().addElement(link3);
        
        final Link link4 = new Link(node2, node3){
            public String getUIClassID() {
                return CustomLinkUI.class.getName();
            }
        };
        link4.putClientProperty(TWaverConst.PROPERTYNAME_LINK_WIDTH, new Integer(8));
        link4.putClientProperty(TWaverConst.PROPERTYNAME_LINK_COLOR, Color.yellow);
        link4.putClientProperty("camber", new Integer(400));
        network.getDataBox().addElement(link4);
        
        
        LinkUI linkUI = (LinkUI)network.getRenderer().getElementUI(link1);
        linkUI.setBundleExpand(true, true);
        
        Thread thread = new Thread() {
            public void run() {
                boolean flag = false;
                while (true) {
                    flag = !flag;
                    
                    final boolean expend = flag;
                    // change expand state 
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            LinkUI linkUI = (LinkUI)network.getRenderer().getElementUI(link3);
                            linkUI.setBundleExpand(expend, true);
                        }
                    });
                    
                    // change camber value
                    for (int i = 0; i < 300; i+=2) {
                        int camber = i;
                        if (flag) {
                        	camber = 300 - i;
                        }
                        final Integer value = new Integer(camber);
                        SwingUtilities.invokeLater(new Runnable() {
                            public void run() {
                                link1.putClientProperty("camber", value);
                                link2.putClientProperty("camber", new Integer(300-value.intValue()));
                            }
                        });
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException ex) {
                        }
                    }
                }
            }
        };
        thread.start();
    }
    
	public String getTitle() {
		return "Link Path Demo";
	}

	public String getHelp() {
		return "This demo shows how to customize link path.";
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -