// Amin Y. Teymorian // 26 July 2005 import java.awt.*; import java.awt.event.*; import javax.swing.*; //---------------------------------------------------------------------- // // GN - a class that defines an applet that has the ability to display // G(n) graphs (as described by V. Levenshtein and V. Tonchev) // on the screen // //---------------------------------------------------------------------- public class GN extends JApplet implements ActionListener { // properties private JLabel title = new JLabel("G(n) Graph Generator",SwingConstants.CENTER); private JLabel info = new JLabel("n = ",SwingConstants.RIGHT); private JTextField field = new JTextField(8); private Graph graph = new Graph(); // // init - a method that does initialization of window // public void init() { // set up window Container window = getContentPane(); window.setBackground(Color.white); window.setLayout(new BorderLayout()); title.setOpaque(true); title.setForeground(Color.black); graph.setBackground(Color.white); graph.setPreferredSize(new Dimension(420,420)); JPanel input = new JPanel(); input.setLayout(new FlowLayout()); input.add(info); field.addActionListener(this); input.add(field); JPanel display = new JPanel(); display.setLayout(new BorderLayout()); display.add(input,BorderLayout.NORTH); display.add(graph,BorderLayout.CENTER); window.add(title,BorderLayout.NORTH); window.add(display,BorderLayout.CENTER); } // end of init method // // actionPerformed - a method that listens for events // public void actionPerformed(ActionEvent event) { if (event.getSource() == field) { try { int n = Integer.parseInt(field.getText()); graph.compute(n); } catch (NumberFormatException e) { graph.compute(0); } } } // end of actionPerformed method } // end of GN class //---------------------------------------------------------------------- // // Graph - a class that defines a graph object // //---------------------------------------------------------------------- class Graph extends JPanel { // properties private int nv = 0; private int [] pos; private int [] edges; private double inc; private int radius = 4; private int scale = 200; private int shift = 25; private int type = 0; private int ec = 0; // // paintComponent - a method that draws the graph on the screen // public void paintComponent(Graphics g) { // call in super class super.paintComponent(g); // draw each vertex w/ label for (int i=0; i 2) && (type == 0)) { nv = (n-1)/2; pos = new int[2*nv]; edges = new int[6*nv]; inc = (2*Math.PI)/nv; // initialize multiplicity portion of edges for (int i=2; i<(6*nv); i+=3) { edges[i] = 0; } // generate vertex position int offset = 0; for (int i=0; i 2) && (type == 1)) { nv = (n-1)/2; pos = new int[2*nv]; edges = new int[2*nv]; inc = (2*Math.PI)/nv; // generate vertex position int offset = 0; for (int i=0; i