Solución: Semana 3: Evidencia: Manejo de eventos - Parte II

Solución: En la clase color existe un constructor que utiliza tres números enteros para crear un color, tal y como se muestra a continuación:

public Color(int r, int g, int b){…}

Use este constructor para crear un objeto de la clase color, y que este se pueda emplear para definir el color de un rectángulo con relleno, el cual sea dibujado dentro del applet con el método paint().


g.setColor (new Color (deslizador1, deslizador2, deslizador3) ) ;

Defina para cada parámetro el valor que se tome de un deslizador (teniendo tres deslizadores de barra), así como se muestra en la instrucción del applet. Una vez dibujado el rectángulo con el color definido por los deslizadores, vuelva a poner el color en negro para diagramar los valores de los deslizadores: g.setColor (Color.black);

El applet deberá contar con la siguiente estructura (ver ejemplos):

 



import java.awt.Panel;
import java.awt.Scrollbar;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Label;

/**
 *
 * @author gfrodriguez
 */
public class DeslizadoresApplet implements AdjustmentListener {

    private JFrame frame = new JFrame();
    private Panel panel;
    private Scrollbar scrollbarColor1, scrollbarColor2, scrollbarColor3;
    private Label labelValorDeslizador1, labelValorDeslizador2, labelValorDeslizador3;

    DeslizadoresApplet() {
        frame.setTitle("DeslizadoresApplet");

        panel = new Panel();
        scrollbarColor1 = new Scrollbar();
        scrollbarColor2 = new Scrollbar();
        scrollbarColor3 = new Scrollbar();

        labelValorDeslizador1 = new Label();
        labelValorDeslizador2 = new Label();
        labelValorDeslizador3 = new Label();

        scrollbarColor1.addAdjustmentListener(this);
        scrollbarColor2.addAdjustmentListener(this);
        scrollbarColor3.addAdjustmentListener(this);

        frame.setEnabled(true);

        scrollbarColor1.setOrientation(java.awt.Scrollbar.HORIZONTAL);
        scrollbarColor2.setOrientation(java.awt.Scrollbar.HORIZONTAL);
        scrollbarColor3.setOrientation(java.awt.Scrollbar.HORIZONTAL);

        scrollbarColor1.setMaximum(265);
        scrollbarColor2.setMaximum(265);
        scrollbarColor3.setMaximum(265);

        labelValorDeslizador1.setText("Deslizador 1 =" + scrollbarColor1.getValue());
        labelValorDeslizador2.setText("Deslizador 2 =" + scrollbarColor2.getValue());
        labelValorDeslizador3.setText("Deslizador 3 =" + scrollbarColor3.getValue());

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(panel);
        panel.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGap(0, 100, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(frame.getContentPane());
        frame.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(labelValorDeslizador1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(scrollbarColor1, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
                                                .addGap(18, 18, 18)
                                                .addComponent(scrollbarColor2, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                                                .addGap(18, 18, 18)
                                                .addComponent(scrollbarColor3, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                                        .addComponent(labelValorDeslizador2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(labelValorDeslizador3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(scrollbarColor3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(scrollbarColor1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(scrollbarColor2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(50, 50, 50)
                                .addComponent(labelValorDeslizador1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(50, 50, 50)
                                .addComponent(labelValorDeslizador2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(50, 50, 50)
                                .addComponent(labelValorDeslizador3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
        );
        frame.getContentPane().setBackground(Color.BLACK);
        frame.setMinimumSize(new Dimension(300, 300));
        frame.setSize(600, 300);
        frame.setLocationRelativeTo(null);
        //frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new DeslizadoresApplet();
    }

    public void adjustmentValueChanged(AdjustmentEvent ae) {

        labelValorDeslizador1.setText("Deslizador 1 =" + scrollbarColor1.getValue());
        labelValorDeslizador2.setText("Deslizador 2 =" + scrollbarColor2.getValue());
        labelValorDeslizador3.setText("Deslizador 3 =" + scrollbarColor3.getValue());
        frame.getContentPane().setBackground(new Color(scrollbarColor1.getValue(), scrollbarColor2.getValue(), scrollbarColor3.getValue()));
        labelValorDeslizador1.setBackground(new Color(scrollbarColor1.getValue(), scrollbarColor2.getValue(), scrollbarColor3.getValue()));
        labelValorDeslizador2.setBackground(new Color(scrollbarColor1.getValue(), scrollbarColor2.getValue(), scrollbarColor3.getValue()));
        labelValorDeslizador3.setBackground(new Color(scrollbarColor1.getValue(), scrollbarColor2.getValue(), scrollbarColor3.getValue()));
    }
}

0 comments :

Publicar un comentario