/** * Created by Jerome Caffaro on 06.06.05. * * A simple Java applet that shows how the simplest way to use MagLib. * This example uses the default provided classes of MagMap. The easiest * way to customize them is to subclass them, or write completely new classes * that implements the interfaces required by MagLib. * Put MagLib.jar in same folder as this file, then type * * javac -classpath MagLib.jar MagLight.java * * to compile, and this: * * java -cp .:MagLib.jar MagLight * * to run */ import java.awt.*; import javax.swing.*; import java.io.*; import java.util.*; import ch.epfl.craft.maglib.*; import ch.epfl.craft.maglib.magnet.*; public class MagLight extends JFrame { public MagLight() { //Create Magnet View MagnetView view = new MagnetView(); //Add it to frame this.getContentPane().setLayout(new BorderLayout()); this.getContentPane().add(view, BorderLayout.CENTER); //Create Magnet Model DefaultMagnetModel model = new DefaultMagnetModel(); //Load data into model. Here we choose a text format //where the first line is the title nodes. If a title //begins with MAGNET_, then the node is considered as magnet. //There can be no space in a title (we use space as separator) //Other lines is the similarity matrix (row1 is similarity //between column 1 node and other, row2 is column 2 node'similarity //with others, etc.) //We use a StringTokenizer to read the data and hope the file is in same //format, but we don't test String aRow; int rowNumber = 0; int columnNumber = 0; //Save nodes in ordered list, for setting similarities ArrayList nodes = new ArrayList(); try{ BufferedReader data = new BufferedReader(new FileReader("./SampleData.txt")); while ( (aRow = data.readLine()) != null) {//while there are rows StringTokenizer st = new StringTokenizer(aRow); columnNumber = 0; while (st.hasMoreTokens()) { //while the row has other columns if (rowNumber==0){ //First line. Need to add nodes String title = st.nextToken(); //read column value boolean isMagnetic = false; //node title //remove "MAGNET_" at the beginning of title if it appears if (title.startsWith("MAGNET_")){ title = title.replaceFirst("MAGNET_",""); isMagnetic = true; } //create a new DefaultMagnetNode instance and add it to model DefaultMagnetNode node = new DefaultMagnetNode(title); model.addMagnetNode(node); nodes.add(node); //if node is magnetic, let the model know if (isMagnetic){ model.setIsMagneticNode(node, true); } } else { //Not first line. Need to set similarities //read column value float similarity = Float.parseFloat(st.nextToken()); //fetch row node DefaultMagnetNode rowNode = (DefaultMagnetNode)nodes.get(rowNumber-1); //fetch column node DefaultMagnetNode columnNode = (DefaultMagnetNode)nodes.get(columnNumber); //set similarity rowNode.setSimilarityWith(columnNode, similarity); } columnNumber++; } rowNumber++; } } catch (Exception e){ e.printStackTrace(); } //Add model to the view view.setMagnetModel(model); //It is almost done. MagnetView uses a DefaultMagnetLayout instance //and a DefaultMagnetRenderer. The layout algorithm and the rendering //of the magnets can be customized using //view.setMagnetLayout(...) and view.setMagnetCellRenderer(...). //We only need to tell the layouter how much node we want to display //Here, we want all of them to be visible view.getMagnetLayout().setNumberOfVisibleNodes(columnNumber+1); view.setBackground(Color.WHITE); } public static void main(String args[]) { JFrame frame = new MagLight(); frame.setSize(new Dimension(800, 600)); frame.setVisible(true); } }