Páginas

23/11/15

JLabel gradient background.

JLabel gradient background.

Realizar JLabel con color de fondo gradiente.




Algo que se ve muy bien en las aplicaciones de escritorio en Java, son los titulos con un color de fondo gradiente (JLabel Gradient Background), para hacerlo solo hay que sobreescribir el metodo void paintComponent(Graphics g) de la clase JComponent, recuerda que esta es la clase base para los componentes SWING.
Para lograr un color de fondo gradiente horizontal:
JLabel gradient background horizontal
JLabel Gradiente
Hay que implementar este codigo:
  JLabel label = new JLabel("...... DatoJava ......") {
   @Override
   protected void paintComponent(Graphics g) {
    Color colorFinal = new Color(255, 1, 1);
    
    Graphics2D graphics2d = (Graphics2D) g.create();
    graphics2d.setPaint(new GradientPaint(0, 0, getBackground(),
      getWidth(), 0, colorFinal));
    graphics2d.fillRect(0, 0, getWidth(), getHeight());
    graphics2d.dispose();
    
    super.paintComponent(g);
   }
  };
    
Para lograr un color de fondo gradiente vertical:
JLabel gradient background Vertical
JLabel Gradiente
Hay que implementar este codigo:
  JLabel label = new JLabel("...... DatoJava ......") {
   @Override
   public void setForeground(Color fg) {
    super.setForeground(fg.RED);
   }

   @Override
   protected void paintComponent(Graphics g) {
    LinearGradientPaint linearGradientPaint = new LinearGradientPaint(
      new Point(0, 10), new Point(0, getHeight()),
      new float[] { 0.240f, 0.250f }, new Color[] {
        Color.LIGHT_GRAY, Color.GRAY });

    Graphics2D graphics2d = (Graphics2D) g.create();
    graphics2d.setPaint(linearGradientPaint);
    graphics2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
    graphics2d.dispose();

    super.paintComponent(g);
   }
  };
    
Con esto podemos jugar y hacer varias cosas interesantes, por ejemplo:
JLabel gradient background Esquina
JLabel Esquina Gradiente
  JLabel label = new JLabel("...... DatoJava ......") {
   @Override
   protected void paintComponent(Graphics g) {
    LinearGradientPaint linearGradientPaint = new LinearGradientPaint(
      new Point(0, 10), new Point(10, getHeight()),
      new float[] { 0.240f, 0.250f }, new Color[] {
        Color.BLACK, getBackground() });

    Graphics2D graphics2d = (Graphics2D) g.create();
    graphics2d.setPaint(linearGradientPaint);
    graphics2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
    graphics2d.dispose();

    super.paintComponent(g);
   }
  };
    
JLabel gradient background Linea abajo
JLabel Linea Abajo
  JLabel label = new JLabel("...... DatoJava ......") {
   @Override
   protected void paintComponent(Graphics g) {
    LinearGradientPaint linearGradientPaint = new LinearGradientPaint(
      new Point(0, 25), new Point(0, getHeight()),
      new float[] { 0.240f, 0.250f }, new Color[] {
        getBackground(), Color.RED });

    Graphics2D graphics2d = (Graphics2D) g.create();
    graphics2d.setPaint(linearGradientPaint);
    graphics2d.fill(new Rectangle(0, 0, getWidth(), getHeight()));
    graphics2d.dispose();

    super.paintComponent(g);
   }
  };
    
JLabel gradient background linea abajo gradiente
JLabel Linea Abajo
  JLabel label = new JLabel("...... DatoJava ......") {
   @Override
   protected void paintComponent(Graphics g) {
    Color colorFinal = new Color(0, 0, 0);
    Graphics2D graphics2d = (Graphics2D) g.create();
    graphics2d.setPaint(new GradientPaint(0, 0, getBackground(),
      getWidth(), 0, colorFinal));
    graphics2d.fillRect(0, 25, getWidth(), getHeight());
    graphics2d.dispose();

    super.paintComponent(g);
   }
  };
    

No hay comentarios :

Publicar un comentario