Powered By Blogger

martes, 18 de mayo de 2010

Applet en Java: Eventos. Cambio de Color de fondo

Aqui un ejemplo del manejo de eventos en Java utilizando un applet para seleccionar el color de fondo del mismo haciendo uso de 3 ScrollBar por medio de las funciones paint y repain.

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JTextField;

/**
*
* @author renan
*/
public class NewApplet extends Applet implements AdjustmentListener{
JLabel etiqueta1, etiqueta2, etiqueta3;
JScrollBar barra1,barra2,barra3;
JTextField campo1,campo2,campo3;
int valor1,valor2,valor3;
/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init() {
// TODO start asynchronous download of heavy resources

barra1 = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 255);
barra2 = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 255);
barra3 = new JScrollBar(JScrollBar.HORIZONTAL, 0, 1, 0, 255);

etiqueta1 = new JLabel("Deslizador #1: ");
etiqueta2 = new JLabel("Deslizador #2: ");
etiqueta3 = new JLabel("Deslizador #3: ");

campo1 = new JTextField(3);
campo1.setEditable(false);
campo2 = new JTextField(3);
campo2.setEditable(false);
campo3 = new JTextField(3);
campo3.setEditable(false);

add(barra1);
add(barra2);
add(barra3);
add(etiqueta1);
add(campo1);
add(etiqueta2);
add(campo2);
add(etiqueta3);
add(campo3);

barra1.addAdjustmentListener(this);
barra2.addAdjustmentListener(this);
barra3.addAdjustmentListener(this);
}

public void adjustmentValueChanged(AdjustmentEvent e) {
valor1 = barra1.getValue();
campo1.setText(""+valor1);

valor2 = barra2.getValue();
campo2.setText(""+valor2);

valor3 = barra3.getValue();
campo3.setText(""+valor3);

repaint();
}
public void paint(Graphics g)
{

g.drawRect(0, 0, 200, 400);
g.setColor(new Color(valor1, valor2, valor3));

g.fillRect(0, 0, 200, 400);
g.setColor(Color.BLACK);
}
// TODO overwrite start(), stop() and destroy() methods
}

Más información: http://es.wikipedia.org/wiki/Applet_Java

No hay comentarios:

Publicar un comentario

Escribe tu comentario aqui...