Вы находитесь на странице: 1из 9

package javaapplication33;

import java.awt.Color; import java.awt.Graphics;

/** * * @author arvind */ public class NewJFrame extends javax.swing.JFrame { private int v,ctr; int x[]= new int[20]; int y[] = new int[20]; int x1[]=new int[20]; int y1[] = new int[20]; int node1; int x2[]= new int[20]; int y2[] = new int[20]; int node2; public static int adj[][] = new int[100][100];

/** * Creates new form NewJFrame */ public NewJFrame() { initComponents();

/** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() {

jPanel4 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel();

javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout.setHorizontalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 100, Short.MAX_VALUE) ); jPanel4Layout.setVerticalGroup( jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 100, Short.MAX_VALUE) );

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 268, Short.MAX_VALUE) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 203, Short.MAX_VALUE) );

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(74, 74, 74) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(157, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(38, Short.MAX_VALUE))

);

pack(); }// </editor-fold>

/** * @param args the command line arguments */ private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) { x[ctr]=evt.getX()+10; y[ctr]=evt.getY()-25; if(x[ctr]<(300+10)) { draw(x[ctr],y[ctr]+55);

ctr++; } }

private void jPanel1MousePressed(java.awt.event.MouseEvent evt) { for(int a=0;a<ctr;a++) { if(evt.getX()<x[a]+30 && evt.getX()>x[a]-30 && evt.getY()<y[a]+30 && evt.getY()>y[a]-30) { x1[v]=evt.getX()-20; y1[v]=evt.getY()+75; node1=a;

} } // TODO add your handling code here: }

private void jPanel1MouseReleased(java.awt.event.MouseEvent evt) { for(int a=0;a<ctr;a++) { if(evt.getX()<x[a]+30 && evt.getX()>x[a]-30 && evt.getY()<y[a]+30 && evt.getY()>y[a]-30) { x2[v]=evt.getX()-20; y2[v]=evt.getY()+75; node2=a; drawline();

adjacency(); v++;}} // TODO add your handling code here: }


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { str="The Adjacency matrix is : \n"; str=str+" "; for(int i=0;i<ctr;i++) { str=str+(i+1)+" "; } str=str+"\n"; for(int i=0;i<ctr;i++) { str+=(i+1)+" "; for(int j=0;j<ctr;j++) { str+=(adj[i][j]+" "); } str+="\n"; }

jTextArea1.setText(str);

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

str2="\nThe Adjacency List is : \n"; for(int i=0;i<ctr;i++) { str2+=(i+1)+"-->"; for(int j=0;j<ctr;j++) { if(adj[i][j]==1) { str2=str2+(j+1)+","; } } str2=str2+"\n";

} jTextArea2.setText(str2);

public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName());

break; } } } catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold>

/* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } });

} // Variables declaration - do not modify private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel4; // End of variables declaration

private void draw(int i, int i0) { Graphics g=this.getGraphics(); g.setColor(Color.red); g.fillOval(i, i0, 30, 30); g.setColor(Color.black); g.drawString(""+(ctr+1),i+10,i0+18);

private void drawline() { Graphics g=this.getGraphics(); g.setColor(Color.red); g.drawLine(x[node1]+10, y[node1]+75, x[node2]+10, y[node2]+75); }

private void adjacency() { for(int i=0;i<ctr;i++) { for(int j=0;j<ctr;j++) { adj[node1][node2]=1;

adj[node2][node1]=1; } } } }

Вам также может понравиться