import java.awt.*; import java.applet.*; import java.awt.event.*; public class estrella extends Applet { private int n, xPunto, yPunto, xCentro, yCentro; double razon; boolean presionado = false; boolean error = false; Label letrero = new Label("Dame el número de picos de la estrella:"); TextField entradaN = new TextField("",3); Button dibujar = new Button("Dibujar"); public void init() { setBackground(Color.blue); letrero.setForeground(Color.white); entradaN.setBackground(Color.white); add(letrero); add(entradaN); dibujar.addActionListener(new escuchaBoton()); add(dibujar); repaint(); } public class escuchaBoton implements ActionListener { public void actionPerformed(ActionEvent event) { try {n = (new Integer(entradaN.getText())).intValue();} catch(NumberFormatException ex) { error = true; } if (error == false) n = (new Integer(entradaN.getText())).intValue(); presionado = true; repaint(); } } public void dibujaEstrella(int xCentral, int yCentral, int x, int y, double tamano, int k, Graphics f) { double teta = 2*Math.PI/k; double teta1 = Math.PI/k; int x1,y1,a,b,c,d; x1 = xCentral + (int)Math.round(tamano*((x - xCentral)*Math.cos(teta1) + (yCentral - y)*Math.sin(teta1))); y1 = yCentral + (int)Math.round(tamano*((y - yCentral)*Math.cos(teta1) + (x - xCentral)*Math.sin(teta1))); Polygon e = new Polygon(); for (int i = 0 ; i < k ; i++) { a = xCentral + (int)Math.round((x - xCentral)*Math.cos(i*teta) + (yCentral - y)*Math.sin(i*teta)); b = yCentral + (int)Math.round((y - yCentral)*Math.cos(i*teta) + (x - xCentral)*Math.sin(i*teta)); e.addPoint(a,b); c = xCentral + (int)Math.round((x1 - xCentral)*Math.cos(i*teta) + (yCentral - y1)*Math.sin(i*teta)); d = yCentral + (int)Math.round((y1 - yCentral)*Math.cos(i*teta) + (x1 - xCentral)*Math.sin(i*teta)); e.addPoint(c,d); } f.setColor(Color.white); f.fillPolygon(e); } public void paint(Graphics g) { xCentro = getSize().width/2; yCentro = getSize().height/2; xPunto = 550; yPunto = 300; razon = 0.4; g.setColor(Color.white); Font SansSerif = new Font("SansSerif", Font.BOLD, 18); g.setFont(SansSerif); if (presionado == true) { if (error == true) { g.drawString("El número de picos debe ser un entero entre 3 y 100",50,getSize().height/2 ); error = false; } else { if (n>2 && n<101) dibujaEstrella(xCentro, yCentro, xPunto, yPunto, razon, n, g); else { g.drawString("El número de picos debe ser un entero entre 3 y 100",50,getSize().height/2 ); } } } else g.drawString("Esperando datos...",50,getSize().height/2 ); } }