import java.applet.*; import java.awt.*; import java.awt.event.*; // en un campo de texto. Se crean los campos de texto necesarios // resultado del producto. // Se hace subclase de la clase Applet public class ProductoMat extends Applet { int Dim = 0; TextField TF1[][], TF2[][]; TextField TFOrden; Button BIgual; Label LX; boolean Pintar = false; int Xdib, Ydib; int RESP[][]; public void init() { Label LOrden = new Label("Orden: "); // Etiqueta LX = new Label("x"); TFOrden = new TextField(); // Campo de texto Button BCrear = new Button("Crear"); // Boton Button BLimpiar = new Button("Limpiar"); // Boton BIgual = new Button("="); // Fijar el contenedor setLayout(null); add(LOrden); add(BCrear); add(BLimpiar); add(TFOrden); add(BIgual); add(LX); // Posicionamiento en el contenedor //LOrden.setBounds(x, y, ancho, alto) LOrden.setBounds(10, 10, 80, 30); TFOrden.setBounds(100, 10, 80, 30); BCrear.setBounds(10, 50, 80, 30); BLimpiar.setBounds(100, 50, 80, 30); // Captura de los eventos BCrear.addActionListener(new EventoBoton()); BLimpiar.addActionListener(new EventoBoton()); BIgual.addActionListener(new EventoBoton()); BIgual.setVisible(false); Pide_Datos(Dim); } public void paint(Graphics R) { int x = 0, y = Ydib; int Ancho = 30, Alto = 30; String CAD = ""; if (Pintar) { for (int r = 0; r < Dim; r++) { x = Xdib + 10; for (int c = 0; c < Dim; c++) { CAD = "" + RESP[r][c]; x += Ancho + 2; // El 2 es por la separaciòn entre celdas R.drawRect(x,y, Ancho, Alto); 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 } } else } // Hacer invisibles a los componentes que conforman las matrices void Limpia_Datos(int Dim) { for (int r = 0; r < Dim; r++) for (int c = 0; c < Dim; c++) { TF1[r][c].setVisible(false); TF2[r][c].setVisible(false); } BIgual.setVisible(false); Pintar = false; repaint(); } void Pide_Datos(int Dim) { TF1 = new TextField[Dim][Dim]; // Dimensionamiento TF2 = new TextField[Dim][Dim]; int x = 50, y = 100, r; for (r = 0; r < Dim; r++) { x = 50; for (int c = 0; c < Dim; c++) { TF1[r][c].setBounds(x, y, 30, 30); // Posicionamiento en el contenedor TF2[r][c] = new TextField(); add(TF2[r][c]); TF2[r][c].setBounds(x+35*Dim+50, y, 30, 30); x += 35; } y += 35; } LX.setBounds(50 + 35*Dim, 90 + 35 * Dim/2, 30, 30); Xdib = x+35*Dim+50; Ydib = 100 + 30*Dim / 2; BIgual.setBounds(Xdib, Ydib, 20, 20); } public class EventoBoton implements ActionListener { public void actionPerformed(ActionEvent RAFA) { String Boton = RAFA.getActionCommand(); Dim = Integer.parseInt(TFOrden.getText()); // Se compara con "Crear" if (Boton.equals("Crear")) { Pide_Datos(Dim); BIgual.setVisible(true); } if (Boton.equals("Limpiar")) Limpia_Datos(Dim); if (Boton.equals("=")) { Producto(); } } } void Producto() { RESP = new int[Dim][Dim]; for (int r1 = 0; r1 < Dim; r1++) { for (int c1 = 0; c1 < Dim; c1++) { RESP[r1][c1] = 0; for (int r2 = 0; r2 < Dim; r2++) RESP[r1][c1] += Integer.parseInt(TF1[r1][r2].getText()) * Integer.parseInt(TF2[r2][c1].getText()); } } Pintar = true; repaint(); } }