Capturas:
Código:
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
*
* @author MARIO
*/
public class Figuras extends Applet implements ActionListener {
Button bt1, bt2, bt3;
int indice;
final Color[] colores = {Color.red, Color.blue, Color.orange};
public void init() {
setBackground(Color.yellow);
bt1 = new Button("Cuadrado");
bt2 = new Button("Triangulo");
bt3 = new Button("Circulo");
bt1.setBackground(Color.red);
bt1.setForeground(Color.green);
bt1.addActionListener(this);
bt2.setBackground(Color.blue);
bt2.setForeground(Color.white);
bt2.addActionListener(this);
bt3.setBackground(Color.orange);
bt3.setForeground(Color.MAGENTA);
bt3.addActionListener(this);
add(bt1);
add(bt2);
add(bt3);
}
public void paint(Graphics g) {
g.setColor(colores[indice]);
switch (indice) {
case 0:
g.fillRect(40, 40, 40, 40);
break;
case 1:
int [] x = new int [4];
x [0] = 5; x [1] = 70; x [2] = 70; x [3] = 5;
int [] y = new int [4];
y [0] = 70; y [1] = 5; y [2] = 70; y [3] = 70;
g.drawPolygon (x, y, 4);
break;
case 2:
g.fillOval(120, 96, 40, 40);
break;
}
}
public void actionPerformed(ActionEvent e) {
Object boton = e.getSource();
if (boton.equals(bt1)) {
indice = 0;
} else if (boton.equals(bt2)) {
indice = 1;
} else {
indice = 2;
}
repaint();
}
}
Puedes descargar el código desde aquí.



No hay comentarios :
Publicar un comentario