All pastes #456421 Raw Edit

Someone

public text v1 · immutable
#456421 ·published 2007-04-24 20:15 UTC
rendered paste body
 package graphique;
import javax.swing.*;        
import java.awt.*;
//import simulator.*;   

public class Graph extends JFrame {
    /**  panel_l.setBounds(2,2,200,500);
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static JPanel panel_l;
    private static JPanel panel_r;
    
    private static void createAndShowGUI() {
        //Make sure we have nice window decorations.
     //  JFrame.setDefaultLookAndFeelDecorated(true);
        //Create and set up the window.
        
        //On crée une instance du gestionnaire
        
        
        JFrame frame = new JFrame("Simulateur de circuits logiques");
        frame.setPreferredSize(new Dimension(1000,600));
        frame.setLayout(new FlowLayout(0));               
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
   
        //Create left & right panels
        panel_l = new JPanel();
        panel_r = new JPanel(); 
        panel_l.setLayout(new FlowLayout(0));
        panel_r.setLayout(null);
        //Create buttons  
        //set properties
                
        //Panels de gauche et de droite
        panel_l.setBorder(javax.swing.BorderFactory.createTitledBorder("Composants"));
        panel_l.setPreferredSize(new Dimension(100,550));
        panel_r.setBorder(javax.swing.BorderFactory.createTitledBorder("Circuit"));
        panel_r.setPreferredSize(new Dimension(850,550));
        panel_r.setBackground(new Color(25,25,55));     
        frame.getContentPane().add(panel_l);
        frame.getContentPane().add(panel_r);
          
        //Create Buttons components then set names
        Button and = new Button("and");
        Button or = new Button("or");
        Button not = new Button("not");
        Button nand = new Button("nand");
        and.setName("b_and");
        or.setName("b_or");
        not.setName("b_not");
        nand.setName("b_nand");
       
        //Set mouse listeners for all buttons
        and.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                buttonMousePressed(evt);
            }
        });
        nand.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                buttonMousePressed(evt);
            }
        });
        or.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                buttonMousePressed(evt);
            }
        });
        not.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                 buttonMousePressed(evt);
            }
        });        
        
        //Add component buttons to left panel
        panel_l.add(and);
        panel_l.add(or);
        panel_l.add(not);
        panel_l.add(nand);
        
        //Display the window.

        frame.pack();

        
        frame.setVisible(true);
    }
    
    /**
     *buttonMousePressed : catch which button has been pressed and call gate creation method
     *@param evt : Mouse event catched
     */
    
    protected static void buttonMousePressed(java.awt.event.MouseEvent evt){
        String b_name = evt.getComponent().getName();
        //If the gate is a "AND"
        Component c;
        if (b_name == "b_and"){ 
            System.out.println("Création d'une porte ET");
             c = new graphique.Component("and",2);     
        } else if (b_name == "b_or"){ 
            System.out.println("Création d'une porte OU");
             c = new graphique.Component("or",2);     
        } else if (b_name == "b_nand"){ 
            System.out.println("Création d'une porte NON ET");
             c = new graphique.Component("nand",2);     
        } else if (b_name == "b_not"){ 
            System.out.println("Création d'une porte NON");
             c = new graphique.Component("not",1);       
        } else {
            
            c = null;
        }
        panel_r.add(new JButton("test"));
        panel_r.add(c);
        System.out.println(c.getPreferredSize().height + "" + c.getPreferredSize().width + " "+ c.getLocation().toString()  );
        c.doLayout();
        panel_r.doLayout(); 
        c.doLayout();
    }
    
    public Graph(){
        run();
    }
    public static void run(){
        createAndShowGUI();
    }
//    public static void main(String[] args) {
//        //Schedule a job for the event-dispatching thread:
//        //creating and showing this application's GUI.
//        javax.swing.SwingUtilities.invokeLater(new Runnable() {
//            public void run() {
//                createAndShowGUI();
//            }
//        });
//    }
}