jueves, 18 de julio de 2013

Dibujar Cara en Applets Java

Hola amig@s en esta nueva entrega les mostrare como crear un applet que dibuje una carita y al presionar un botón cambiar el color de todo lo dibujado

Captura:



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;

/**
 * @(#)Cara.java
 *
 * Cara Applet application
 *
 * @author Alexander
 * @version 1.00 2013/7/18
 */
public class Cara extends Applet implements ActionListener {

    Button btn = new Button("Cambiar Color");
    int indice = 1;

    public void init() {
        add(btn);
        btn.addActionListener(this);
    }

    public void paint(Graphics g) {
        if (indice == 1) {
            btn.setBackground(Color.black);
            btn.setForeground(Color.lightGray);
            setBackground(Color.red);
            g.fillOval(150, 150, 120, 120);
            g.setColor(Color.green);
            g.fillOval(170, 170, 20, 20);
            g.fillOval(220, 170, 20, 20);
            g.drawLine(200, 175, 200, 220);
            g.drawArc(180, 190, 50, 50, 180, 180);
        } else {
            btn.setBackground(Color.white);
            btn.setForeground(Color.blue);
            setBackground(Color.blue);
            g.setColor(Color.pink);
            g.fillOval(150, 150, 120, 120);
            g.setColor(Color.yellow);
            g.fillOval(170, 170, 20, 20);
            g.fillOval(220, 170, 20, 20);
            g.drawLine(200, 175, 200, 220);
            g.drawArc(180, 190, 50, 50, 180, 180);
        }
    }

    public void actionPerformed(ActionEvent e) {
        Object boton = e.getSource();
        if (boton.equals(btn)) {
            if (indice != 2) {
                indice = 2;
            } else {
                indice = 1;
            }
        }
        repaint();
    }
}


Descarga el código desde aquí.

No hay comentarios :

Publicar un comentario