import java.awt.*; import java.applet.*; import java.awt.event.*; public class divisor extends Applet { int x,y; boolean presionado = false, error = false; Label titulo = new Label(" Dame dos números enteros y calcularé su máximo común divisor: "); TextField entrada1 = new TextField("",3); TextField entrada2 = new TextField("",3); Button aceptar = new Button("Aceptar"); public void init() { setBackground(Color.blue); titulo.setForeground(Color.white); entrada1.setBackground(Color.white); entrada2.setBackground(Color.white); aceptar.addActionListener(new boton()); add(titulo); add(entrada1); add(entrada2); add(aceptar); repaint(); } public class boton implements ActionListener { public void actionPerformed(ActionEvent event) { try {x = (new Integer(entrada1.getText())).intValue();} catch (NumberFormatException e) { error = true; } try {y = (new Integer(entrada2.getText())).intValue();} catch (NumberFormatException e1) { error = true; } presionado = true; repaint(); } } public void paint(Graphics g) { g.setColor(Color.white); maximoComunDivisor(x,y,g); } public void maximoComunDivisor(int m, int n, Graphics f) { int m1,n1,minimo,mcd=1; if (error == true) { f.drawString("Los números deben ser enteros no ambos 0.",50,75); error = false; } if (presionado == true && error == false) { x = (new Integer(entrada1.getText())).intValue(); y = (new Integer(entrada2.getText())).intValue(); if (m == 0 && n == 0) f.drawString("El máximo común divisor de 0 y 0 no está definido.",50,75); else { if (m == 0) mcd = n; else { if (n == 0) mcd = m; else { m1 = Math.abs(m); n1 = Math.abs(n); if (m1 <= n1) minimo = m1; else minimo=n1; for (int i=1; i<=minimo; i++) { if (m1 % i == 0 && n1 % i == 0) mcd = i; } } } f.drawString("El máximo común divisor de " + m + " y " + n + " es: " + mcd,50,75); } } } }