import java.awt.*; import java.applet.*; import java.awt.event.*; // de cuadro correspondiente. -111, 222, 333, ..., NNN public class DibujaMat extends Applet { TextField TF, TF1; Button BCrear; Button BLimpiar; int Ancho, Alto, Dim; // normalmente la GUI -Graphics User Interface- public void init() { // Se crean los componentes TF= new TextField("0",10); BCrear= new Button("Crear"); BLimpiar= new Button("Limpiar"); setLayout(null); // Se añaden al contenedor add(TF); add(BCrear); add(BLimpiar); // Posiciono los componentes TF.setBounds(10, 10, 100, 30); BCrear.setBounds(10, 50, 100, 30); BLimpiar.setBounds(10, 90, 100, 30); // Captura de eventos BCrear.addActionListener(new EventoBoton()) ; BLimpiar.addActionListener(new EventoBoton()) ; setBackground(Color.yellow); setForeground(Color.red); } public void paint(Graphics R) { int x = 200; int y = 100; String CAD = ""; int CONT = 0; for (int r = 0; r < Dim; r++) { x = 200; for (int c = 0; c < Dim; c++) { CONT ++; x += Ancho + 2; // El 2 es por la separaciòn entre celdas R.drawRect(x,y, Ancho, Alto) ; CAD = ""; for (int n = 0; n < Dim; n++) CAD += CONT; R.drawString(CAD, x+Ancho/2-CAD.length()/2*8, y+Alto/2) ; } y += Alto + 2; // El 2 es por la separaciòn entre celdas } } // Se crea la clase con el NOMBRE QUE SEA para que implemente la interfaz ActionListener public class EventoBoton implements ActionListener { public void actionPerformed(ActionEvent e) { String OPC = e.getActionCommand(); if (OPC.equals("Crear")) { Dim = Integer.parseInt(TF.getText()); Ancho = 20*Dim; Alto = 20*Dim; repaint(); } else { Dim = 0; repaint(); } } } }