package grafica; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Vector; /** * Clase de interfaz grafica para el programa que va a graficar datos * * @author Magus */ public class Main extends JFrame implements ActionListener { JLabel lblValue; JTextField txtValue; JButton btnAdd; JButton btnClear; Board draw; Vector elements; /** * Metodo que crea la interfaz grafica que me va a permitir crear la grafica * y mandar llamar todos los metodos */ public Main() { getContentPane().setLayout( new BorderLayout() ); JPanel pnlControls = new JPanel( new GridLayout(2, 2) ); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); lblValue = new JLabel("Pon el valor: "); pnlControls.add(lblValue); txtValue = new JTextField(); pnlControls.add(txtValue); btnAdd = new JButton("Aņadir"); btnAdd.addActionListener(this); pnlControls.add(btnAdd); btnClear = new JButton("Limpiar"); btnClear.addActionListener(this); pnlControls.add(btnClear); getContentPane().add(pnlControls, BorderLayout.WEST); draw = new Board(); getContentPane().add(draw, BorderLayout.CENTER); elements = new Vector(); setSize(300, 300); setVisible(true); setTitle("Graficador"); } /** * @param args the command line arguments */ public static void main(String[] args) { Main app = new Main(); } /** * Metodo que responde a eventos * * @param e ActionEvent que representa el evento que sucedio. */ public void actionPerformed(ActionEvent e) { if (e.getSource() == btnAdd) { int value = Integer.parseInt(txtValue.getText()); if (value >= 0) { elements.add(value); draw.drawGraph(elements); } else { JOptionPane.showMessageDialog(null, "Necesitas poner un numero positivo"); } } else if (e.getSource() == btnClear) { elements.clear(); draw.drawGraph(elements); } } }