lunes, 12 de mayo de 2014

Botón con imagen cambiando color de fondo

package explicacionventana;

import java.awt.Color;
import java.awt.Image;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.util.Random;

/**
*
* @author juan
*/
public class Ventana extends javax.swing.JFrame {

    /**
     * Creates new form Ventana
     */
    JButton boton1;
    public Ventana() {
        initComponents();
     


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                        
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        panColor = new javax.swing.JPanel();
        btnAzul = new javax.swing.JButton();
        btnAzar = new javax.swing.JButton();
        etImagen = new javax.swing.JLabel();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ventana con colores");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        panColor.setBackground(new java.awt.Color(255, 0, 0));

        btnAzul.setText("Azul");
        btnAzul.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAzulActionPerformed(evt);
            }
        });

        btnAzar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/explicacionventana/logocarballeira.png"))); // NOI18N
        btnAzar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAzarActionPerformed(evt);
            }
        });

        etImagen.setText("jLabel1");

        javax.swing.GroupLayout panColorLayout = new javax.swing.GroupLayout(panColor);
        panColor.setLayout(panColorLayout);
        panColorLayout.setHorizontalGroup(
            panColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panColorLayout.createSequentialGroup()
                .addGap(80, 80, 80)
                .addComponent(etImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(panColorLayout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(btnAzul, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(58, 58, 58)
                .addComponent(btnAzar, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        panColorLayout.setVerticalGroup(
            panColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panColorLayout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(etImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(70, 70, 70)
                .addGroup(panColorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panColorLayout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addComponent(btnAzul, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnAzar)))
        );

        getContentPane().add(panColor, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>                      

    private void formWindowClosing(java.awt.event.WindowEvent evt) {                                  
        System.out.println("Salí");
        System.exit(0);
    }                                

    private void btnAzulActionPerformed(java.awt.event.ActionEvent evt) {                                      
        ImageIcon puente = new ImageIcon(getClass().getResource("/imagenes/ponteromano.jpg"));
        Icon icono = new ImageIcon(puente.getImage().getScaledInstance(etImagen.getWidth(),etImagen.getHeight(),Image.SCALE_DEFAULT));
        panColor.setBackground(Color.BLUE);
        etImagen.setIcon(icono);
    }                                      

    private void btnAzarActionPerformed(java.awt.event.ActionEvent evt) {                                      
        //ImageIcon imagen = new ImageIcon("/explicacionventana/carballeiraentrada.jpg");
        //Icon icono = new ImageIcon(imagen.getImage().getScaledInstance(btnAzul.getWidth(), btnAzul.getHeight(), Image.SCALE_DEFAULT));
        etImagen.setIcon(new ImageIcon(getClass().getResource("/explicacionventana/carballeiraentrada.jpg")));
        etImagen.repaint();
      
      
        Random al = new Random();
        Color azar;
        azar = new Color(al.nextFloat(),al.nextFloat(),al.nextFloat());
        panColor.setBackground(azar);
      
    }                                      

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Ventana().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton btnAzar;
    private javax.swing.JButton btnAzul;
    private javax.swing.JLabel etImagen;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel panColor;
    // End of variables declaration                  
}

No hay comentarios: