Mostrando entradas con la etiqueta applets. Mostrar todas las entradas
Mostrando entradas con la etiqueta applets. Mostrar todas las entradas

viernes, 19 de julio de 2013

Dibujar un cuadrado, un triángulo y un circulo en applet

Hola amig@s en esta entrega les mostrare como dibujar un cuadrado, un triángulo y un circulo en applet cada uno se dibujara al presionar el botón correspondiente a dicha figura.


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í.

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í.