| | | | Browse by category |
Question
How to create a network component with two nodes and a link?
Answer
This basic sample shows a network component in a window. The network contains two elements with a link between them.
import ilog.cpl.IlpNetwork;
import ilog.cpl.graphic.IlpPoint;
import ilog.tgo.datasource.IltDefaultDataSource;
import ilog.tgo.model.IltLink;
import ilog.tgo.model.IltNetworkElement;
import ilog.tgo.model.IltOSIObjectState;
import ilog.tgo.model.IltObject;
import javax.swing.JFrame;
public class Main {
{
// This sample uses JViews TGO features. When deploying an
// application that includes this code, you need to be in possession
// of a JViews TGO Deployment license.
// unComment the following line of code if using JViews TGO 8.8 or earlier.
// IlvProductUtil.DeploymentLicenseRequired( IlvProductUtil.IBM_ILOG_JViews_TGO_Deployment);
}
public static void main(String[] args) {
IlpNetwork network = new IlpNetwork();
network.setDataSource(createDataSource());
createFrame("JTGO Basic Network Sample", network);
}
static IltDefaultDataSource createDataSource() {
IltDefaultDataSource dataSource = new IltDefaultDataSource();
IltNetworkElement element1 = createElement("First", 50, 50);
IltNetworkElement element2 = createElement("Second", 150, 150);
IltLink link = createLink("Link", element1, element2);
dataSource.addObject(element1);
dataSource.addObject(element2);
dataSource.addObject(link);
return dataSource;
}
static IltNetworkElement createElement(String name, float x, float y) {
IltNetworkElement element =
new IltNetworkElement(
name,
IltNetworkElement.Type.NE,
new IltOSIObjectState());
element.setPosition(new IlpPoint((float) x, (float) y));
return element;
}
static IltLink createLink(String name, IltObject from, IltObject to) {
IltLink link =
new IltLink(new IltOSIObjectState(), name, IltLink.Media.Fiber);
link.setFrom(from);
link.setTo(to);
return link;
}
static JFrame createFrame(String title, IlpNetwork network) {
JFrame frame = new JFrame(title);
frame.getContentPane().add(network);
frame.setLocation(50, 50);
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return frame;
}
}