/* * Title: GraphTheory * Copyright: Waterloo Maple Inc * @author Stephen Forrest * @version 1.0 */ package com.maplesoft.maplenet.demo; import com.maplesoft.maplenet.client.*; import java.awt.*; import java.awt.event.*; import java.net.URL; import java.util.*; import javax.swing.*; import javax.swing.border.*; public class GraphTheory extends JApplet implements MouseListener { boolean isStandalone = false; JButton ComplementButton = new JButton(); Box box1; Box box2; Box box5; Box box6; Box box7; Box box8; Box box9; Box xbox; Box ybox; Box tgbox1; Box tgbox2; Box xtgbox; Box ytgbox; JTextField mapleInput = new JTextField(); GraphPanel graphPanel = new GraphPanel(); //ImagePanel imagePanel1 = new ImagePanel(); JPanel graphProps = new JPanel(); JPanel func = new JPanel(); JPanel displayed = new JPanel(); JPanel derivative = new JPanel(); String sUser; String sPassword; String sHost; int iPort; TitledBorder graphBorder; TitledBorder titledBorder1; TitledBorder titledBorder2; TitledBorder titledBorder3; TitledBorder titledBorder4; JCheckBox xCheckBox = new JCheckBox(); JCheckBox yCheckBox = new JCheckBox(); JTextField xMin = new JTextField(); JTextField xMax = new JTextField(); JTextField yMin = new JTextField(); JTextField yMax = new JTextField(); JCheckBox displayDerivative = new JCheckBox(); JTextField tgEquation = new JTextField(); JCheckBox tgDisplay = new JCheckBox(); JLabel xtgLabel = new JLabel(); JLabel ytgLabel = new JLabel(); JTextField xtgInput = new JTextField(); JTextField ytgOutput = new JTextField(); Component component1; Box isplanar; JCheckBox isplanarCB = new JCheckBox(); JTextField isplanarTF = new JTextField(); Box diameter; JCheckBox diameterCB = new JCheckBox(); JTextField diameterTF = new JTextField(); Box charpoly; JCheckBox charpolyCB = new JCheckBox(); JTextField charpolyTF = new JTextField(); Box numcomp; JCheckBox numcompCB = new JCheckBox(); JTextField numcompTF = new JTextField(); Box box4; JCheckBox spantreeCB = new JCheckBox(); Box graphPropsOuterBox; Box graphPropsBox; Box vertexBox; Box edgeBox; JLabel vertexLabel = new JLabel(); JLabel edgeLabel = new JLabel(); JTextField vertexField = new JTextField(); JTextField edgeField = new JTextField(); //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public GraphTheory() { } //Initialize the applet public void init() { try { String sDocHost; if(isStandalone) { sDocHost = "localhost"; }else{ URL url = getDocumentBase(); sDocHost = url.getHost(); } sHost = this.getParameter( "host", sDocHost ); } catch (Exception e) { e.printStackTrace(); } try { iPort = Integer.parseInt(this.getParameter("port", "14444")); } catch (Exception e) { e.printStackTrace(); } try { sUser = this.getParameter("user", "client"); } catch (Exception e) { e.printStackTrace(); } try { sPassword = this.getParameter("password", "demopass"); } catch (Exception e) { e.printStackTrace(); } try { jbInit(); //myInit(); //sizecheck(); } catch(Exception e) { e.printStackTrace(); } addMouseListener(this); } //Component initialization private void jbInit() throws Exception { // initialize all the components graphPropsOuterBox = Box.createVerticalBox(); graphPropsBox = Box.createHorizontalBox(); vertexBox = Box.createHorizontalBox(); edgeBox = Box.createHorizontalBox(); graphPropsBox = Box.createVerticalBox(); box1 = Box.createHorizontalBox(); box2 = Box.createVerticalBox(); box4 = Box.createVerticalBox(); box5 = Box.createVerticalBox(); box6 = Box.createVerticalBox(); box7 = Box.createVerticalBox(); box8 = Box.createVerticalBox(); box9 = Box.createVerticalBox(); isplanar = Box.createHorizontalBox(); diameter = Box.createHorizontalBox(); charpoly = Box.createHorizontalBox(); numcomp = Box.createHorizontalBox(); xbox = Box.createHorizontalBox(); ybox = Box.createHorizontalBox(); tgbox1 = Box.createHorizontalBox(); tgbox2 = Box.createVerticalBox(); xtgbox = Box.createHorizontalBox(); ytgbox = Box.createHorizontalBox(); graphBorder = new TitledBorder(BorderFactory.createLineBorder(SystemColor.controlText,1),"Graph"); titledBorder4 = new TitledBorder(BorderFactory.createLineBorder(SystemColor.controlText,1),"Graph properties"); titledBorder1 = new TitledBorder(BorderFactory.createLineBorder(SystemColor.controlText,1),"Basic properties"); titledBorder2 = new TitledBorder(BorderFactory.createLineBorder(SystemColor.controlText,1),"Display"); // titledBorder3 = new TitledBorder(BorderFactory.createLineBorder(SystemColor.controlText,1),"Derivative"); component1 = Box.createGlue(); ComplementButton.setText("Take Complement"); ComplementButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { graphPanel.take_complement(); do_something(e); } }); this.setSize(new Dimension(400,400)); graphPanel.setBackground(Color.white); graphPanel.setBorder(graphBorder); // set border on panels graphProps.setBorder(titledBorder1); func.setBorder(titledBorder4); displayed.setBorder(titledBorder2); //derivative.setBorder(titledBorder3); // add to graph properties panel vertexLabel.setText(" Vertices:"); vertexField.setEditable(false); vertexField.setText("0"); vertexField.setPreferredSize( new Dimension( 30, 15) ); vertexBox.add(vertexLabel, null); vertexBox.add(vertexField, null); edgeLabel.setText(" Edges:"); edgeField.setEditable(false); edgeField.setPreferredSize( new Dimension( 30, 15) ); edgeField.setText("0"); edgeBox.add(edgeLabel, null); edgeBox.add(edgeField, null); graphPropsBox.add(vertexBox, null); graphPropsBox.add(edgeBox, null); graphProps.add( graphPropsBox ); graphPropsOuterBox.add( graphProps ); //add to func panel isplanarCB.setSelected(false); isplanarCB.setText("planar?"); isplanarCB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { do_something(e); } }); isplanarTF.setText(""); isplanarTF.setMinimumSize(new Dimension(160, 21)); isplanarTF.setPreferredSize(new Dimension(160, 21)); isplanarTF.setEditable(false); isplanar.add(isplanarCB, null); isplanar.add(isplanarTF, null); diameterCB.setSelected(false); diameterCB.setText("diameter:"); diameterCB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { do_something(e); } }); diameterTF.setText(""); diameterTF.setMinimumSize(new Dimension(160, 21)); diameterTF.setPreferredSize(new Dimension(160, 21)); diameterTF.setEditable(false); diameter.add(diameterCB, null); diameter.add(diameterTF, null); charpolyCB.setSelected(false); charpolyCB.setText("char poly:"); charpolyCB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { do_something(e); } }); charpolyTF.setText(""); charpolyTF.setMinimumSize(new Dimension(160, 21)); charpolyTF.setPreferredSize(new Dimension(160, 21)); charpolyTF.setEditable(false); charpoly.add(charpolyCB, null); charpoly.add(charpolyTF, null); numcompCB.setSelected(false); numcompCB.setText("components:"); numcompCB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { do_something(e); } }); numcompTF.setText(""); numcompTF.setMinimumSize(new Dimension(160, 21)); numcompTF.setPreferredSize(new Dimension(160, 21)); numcompTF.setEditable(false); numcomp.add(numcompCB, null); numcomp.add(numcompTF, null); xCheckBox.setSelected(true); xCheckBox.setText("x:"); xCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { xCheckBox_actionPerformed(e); } }); yCheckBox.setSelected(true); yCheckBox.setText("y:"); yCheckBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { yCheckBox_actionPerformed(e); } }); xMin.setText("-10"); xMax.setText("10"); yMax.setText("-5"); yMin.setText("5"); xbox.add(xCheckBox, null); xbox.add(xMin, null); xbox.add(xMax, null); ybox.add(yCheckBox, null); ybox.add(yMax, null); ybox.add(yMin, null); box7.add(isplanar, null); box7.add(diameter, null); box7.add(charpoly, null); box7.add(numcomp, null); // box7.add(component1, null); //box7.add(xbox,null); //box7.add(ybox, null); func.add(box7,null); // add to displayed panel spantreeCB.setSelected(false); spantreeCB.setText("minimum weight spanning tree"); spantreeCB.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { do_something(e); } }); tgEquation.setMinimumSize(new Dimension(160, 21)); tgEquation.setPreferredSize(new Dimension(160, 21)); tgEquation.setEditable(false); tgDisplay.setText("display"); tgDisplay.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { tgDisplay_actionPerformed(e); } }); xtgLabel.setText("x:"); ytgLabel.setText("y:"); ytgOutput.setEditable(false); xtgInput.setText("0"); box8.add(spantreeCB, null); //box8.add(tgbox1, null); //box8.add(tgEquation, null); tgbox2.add(xtgbox, null); xtgbox.add(xtgLabel, null); xtgbox.add(xtgInput, null); tgbox2.add(ytgbox, null); ytgbox.add(ytgLabel, null); ytgbox.add(ytgOutput, null); //tgbox1.add(tgDisplay, null); //tgbox1.add(tgbox2, null); displayed.add(box8, null); // add to derivative panel displayDerivative.setText("display"); displayDerivative.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { displayDerivative_actionPerformed(e); } }); box9.add(displayDerivative, null); derivative.add(box9, null); // add panels to their respective boxes box2.add(graphPanel, null); box1.add(func, null); box5.add(displayed,null); //box6.add(derivative,null); // add all subboxes together in the big box box4.add(graphPropsOuterBox,null); box4.add(box1,null); box4.add(box5, null); //box4.add(box6, null); // add the big boxes together in the applet this.getContentPane().add(box2, BorderLayout.CENTER); this.getContentPane().add(box4, BorderLayout.EAST); this.getContentPane().add(ComplementButton, BorderLayout.SOUTH); } //Start the applet public void start() { } //Stop the applet public void stop() { } //Destroy the applet public void destroy() { } //Get Applet information public String getAppletInfo() { return "Applet Information"; } //Get parameter info public String[][] getParameterInfo() { return null; } //static initializer for setting look & feel static { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch(Exception e) { } } void do_something(AWTEvent e) { String[] request = new String[7]; String[] response = null; boolean needMaple = false; vertexField.setText( String.valueOf(graphPanel.getNumVertices()) ); edgeField.setText( String.valueOf(graphPanel.getNumEdges()) ); needMaple=isplanarCB.isSelected()||diameterCB.isSelected()||charpolyCB.isSelected()||numcompCB.isSelected()||spantreeCB.isSelected(); if (graphPanel.getNumVertices()>1 && needMaple) { MapleStatement statement = new MapleStatement( sHost, iPort, sUser, sPassword); request[0] = "with(networks): lprint()"; request[1] = "G := graph({$1.."+graphPanel.getNumVertices()+"} ,{"+graphPanel.getEdgeList()+"})"; // if (graphPanel.getNumEdges() > 0) { request[1] = "networks[girth](G)"; } else { request[1] = "infinity"; } request[2] = "`if`(nops(components(G))>1, infinity, diameter(G) )"; request[3] = "isplanar(G)"; request[4] = "charpoly(G,x)"; request[5] = "nops(components(G))"; request[6] = "`if`(nops(components(G))>1, null, op(ends(spantree(G))) )"; System.out.print("Request: "+request[0]+"\n"+request[1]+"\n"+request[2]+"\n"); System.out.print(request[3]+"\n"+request[4]+"\n"+request[5]+"\n"+request[6]); try { response = statement.execute(request); } catch ( MapleStatementException ex2) { System.out.println("Syntax Error: " + ex2.toString()); } catch ( MapleException ex1) { System.out.println("Error connecting to server: " + ex1.toString()); } System.out.println("Response: "+response[0]+"\n"+response[1]+"\n"+response[2]+"\n"); System.out.println(response[3]+"\n"+response[4]+"\n"+response[5]+"\n"+response[6]); } if (diameterCB.isSelected()) { diameterTF.setText( response[2] ); } if (isplanarCB.isSelected()) { isplanarTF.setText( response[3] ); } if (charpolyCB.isSelected()) { charpolyTF.setText( response[4] ); } if (numcompCB.isSelected()) { numcompTF.setText( response[5] ); } if (spantreeCB.isSelected()) { graphPanel.parseAndAddPoints( response[6] ); graphPanel.repaint(); } } void charpolyCB_actionPerformed(ActionEvent e) { do_something(e); } void xCheckBox_actionPerformed(ActionEvent e) { do_something(e); } void yCheckBox_actionPerformed(ActionEvent e) { do_something(e); } void tgDisplay_actionPerformed(ActionEvent e) { do_something(e); } void displayDerivative_actionPerformed(ActionEvent e) { do_something(e); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseClicked(MouseEvent e) { boolean somethingChanged = graphPanel.selectPoint( new Point( e.getX(), e.getY() ) ); if (somethingChanged) { do_something(e); } } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } //Main method - invoke when running standlaone public static void main(String[] args) { // Create the Applet GraphTheory applet = new GraphTheory(); applet.isStandalone = true; // Standalone requires its own frame JFrame frame = new JFrame(); frame.setDefaultCloseOperation(3); //EXIT_ON_CLOSE == 3 frame.setTitle("Maple Plotting Test"); frame.setSize( 780, 600 ); frame.getContentPane().add(applet, BorderLayout.CENTER); // Init the applet applet.init(); applet.start(); //frame.setSize( applet.getWidth()+10, applet.getHeight()+40 ); // Center Frame Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); } } class GraphPanel extends JPanel { Point holdingPoint = null; LinkedList vertexlist = new LinkedList(); HashSet edgeset = new HashSet(); HashSet spanningtree = new HashSet(); static int pointRadius = 10; public int getNumVertices() { return vertexlist.size(); } public int getNumEdges() { return edgeset.size(); } public HashSet getEdgeSet() { return edgeset; } public String getEdgeList() { String edgelist = ""; Iterator edgeIterator = edgeset.iterator(); int[] myEdge = null; while (edgeIterator.hasNext()) { myEdge = (int[])edgeIterator.next(); edgelist = edgelist.concat("{" + myEdge[0] + "," + myEdge[1] + "}"); if (edgeIterator.hasNext()) { edgelist = edgelist.concat(","); } } return edgelist; } private void setActivePoint( Point vPoint ) { if (holdingPoint != null && vertexlist.contains( holdingPoint ) ) { drawVirtualPoint( getGraphics(), holdingPoint ); } holdingPoint = vPoint; if (holdingPoint != null) { drawVirtualPoint( getGraphics(), holdingPoint, Color.blue ); } } private Point getActivePoint () { return holdingPoint; } public void drawVirtualLine(Graphics g, Point p, Point q, Color colour) { g.setColor( colour ); g.drawLine( p.x, p.y, q.x, q.y ); } public void drawVirtualPoint(Graphics g, Point p, Color colour) { g.setColor( colour ); g.drawOval(p.x-pointRadius,p.y-pointRadius,2*pointRadius,2*pointRadius); } public void drawVirtualPoint(Graphics g, Point p) { drawVirtualPoint(g, p, Color.red); } private boolean isWithinBounds(Point p) { return (p.x>=50 && p.x<=getWidth()-50 && p.y>=50 && p.y<=getHeight()-50); } private Point getClosePoint(Point p) { if (p.x >= 50 && p.x <= getWidth()-50 && p.y >= 50 && p.y <= getHeight()-50) { ListIterator pointIterator = vertexlist.listIterator(); Point myPoint = null; int dx; int dy; int r2 = 4*pointRadius*pointRadius; while (pointIterator.hasNext()) { myPoint = (Point)pointIterator.next(); dx = myPoint.x - p.x; dy = myPoint.y - p.y; if ( (dx*dx + dy*dy) < r2 ) { return myPoint; } } } return null; } private void removePoint(Point p) { vertexlist.remove(p); } private Point addPoint(Point p) { if (p.x >= 50 && p.x <= getWidth()-50 && p.y >= 50 && p.y <= getHeight()-50) { // Having got to here, we know our x-value is distinct. vertexlist.add( p ); drawVirtualPoint( getGraphics(), p ); return p; } return null; } private Point addPoint(int x, int y) { return addPoint(new Point(x,y)); } private boolean changeEdge(Point p, Point q) { int pIndex = vertexlist.indexOf(p); int qIndex = vertexlist.indexOf(q); if (p==q || pIndex == -1 || qIndex == -1) { return false; } Iterator edgeIterator = edgeset.iterator(); int[] myEdge = null; while (edgeIterator.hasNext()) { myEdge = (int[])edgeIterator.next(); if ((myEdge[0]==pIndex+1 && myEdge[1]==qIndex+1) || (myEdge[0]==qIndex+1 && myEdge[1]==pIndex+1)) { edgeset.remove(myEdge); repaint(); return true; } } myEdge = new int[2]; myEdge[0] = pIndex+1; myEdge[1] = qIndex+1; edgeset.add(myEdge); drawVirtualLine( getGraphics(), p, q, Color.black ); return false; } void clear_vertexlist() { vertexlist.clear(); edgeset.clear(); spanningtree.clear(); repaint(); } void take_complement() { for (int i=0; i