A JPanel with a textured background. : Panel « Swing Components « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing Components » Panel 




A JPanel with a textured background.
     
/*
 *  TexturedPanel.java
 *  2006-11-02
 */

//cb.aloe.decor;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.image.BufferedImage;

import javax.swing.Icon;
import javax.swing.JPanel;

/**
 * A JPanel with a textured background.
 
 @author Christopher Bach
 */
public class TexturedPanel extends JPanel {

  private TexturePaint ourPainter = null;

  private Color ourDefaultForeground = Color.white;

  private Color ourDefaultBackground = Color.gray;

  /**
   * Creates a new TexturedPanel with a simple striped pattern.
   */
  public TexturedPanel() {
    super();
    ourDefaultForeground = Color.white;
    ourDefaultBackground = getBackground();
    setupDefaultPainter(ourDefaultForeground, ourDefaultBackground);
  }

  /**
   * Creates a new TexturedPanel with a simple striped pattern consisting of the
   * given foreground and background colors.
   */
  public TexturedPanel(Color foreground, Color background) {
    super();
    ourDefaultForeground = (foreground != null ? foreground : Color.white);
    ourDefaultBackground = (background != null ? background : getBackground());

    setupDefaultPainter(ourDefaultForeground, ourDefaultBackground);
  }

  /**
   * Creates a new TexturedPanel with a simple pattern based on the provided
   * texture map and consisting of the given foreground and background colors.
   */
  public TexturedPanel(Color foreground, Color background, boolean[][] texture) {
    super();
    ourDefaultForeground = (foreground != null ? foreground : Color.white);
    ourDefaultBackground = (background != null ? background : getBackground());

    setupTexturePainter(ourDefaultForeground, ourDefaultBackground, texture, 1);
  }

  /**
   * Creates a new TexturedPanel with a simple pattern based on the provided
   * texture map and consisting of the given foreground and background colors.
   */
  public TexturedPanel(Color foreground, Color background, boolean[][] texture, int scale) {
    super();
    ourDefaultForeground = (foreground != null ? foreground : Color.white);
    ourDefaultBackground = (background != null ? background : getBackground());

    setupTexturePainter(ourDefaultForeground, ourDefaultBackground, texture, scale);
  }

  /**
   * Creates a new TexturedPanel that tiles the provided image.
   */
  public TexturedPanel(Image texture) {
    super();
    ourDefaultForeground = Color.white;
    ourDefaultBackground = getBackground();

    if (texture != null)
      setupImagePainter(texture);
    else
      setupDefaultPainter(ourDefaultForeground, ourDefaultBackground);
  }

  /**
   * Creates a new TexturedPanel that tiles the provided icon.
   */
  public TexturedPanel(Icon textureIcon) {
    super();
    ourDefaultForeground = Color.white;
    ourDefaultBackground = getBackground();

    if (textureIcon != null) {
      setupIconPainter(textureIcon);
    }

    else
      setupDefaultPainter(ourDefaultForeground, ourDefaultBackground);
  }

  /**
   * Sets up this TexturedPanel to paint a simple background based on the
   * provided texture and consisting of the provided colors.
   */
  public void setTexture(Color foreground, Color background, boolean[][] texture) {
    if (foreground != null && background != null && texture != null && texture.length > 0
        && texture[0].length > 0) {
      setupTexturePainter(foreground, background, texture, 1);
    }
  }

  /**
   
   */
  public void setTexture(Color foreground, Color background, boolean[][] texture, int scale) {
    setupTexturePainter(foreground, background, texture, scale);
  }

  /**
   * Sets up this TexturedPanel to paint a striped background consisting of the
   * provided colors.
   */
  public void setTextureColors(Color foreground, Color background) {
    if (foreground != null && background != null) {
      ourDefaultForeground = foreground;
      ourDefaultBackground = background;
      setupDefaultPainter(foreground, background);
    }
  }

  /**
   * Sets up this TexturedPanel to paint a tiled background consisting of the
   * provided image. If the image is null, the background will revert to a
   * striped texture consisting of the last known colors.
   */
  public void setTextureImage(Image texture) {
    if (texture != null)
      setupImagePainter(texture);
    else
      setupDefaultPainter(ourDefaultForeground, ourDefaultBackground);
  }

  /**
   * Sets up this TexturedPanel to paint a tiled background consisting of the
   * provided icon. If the icon is null, the background will revert to a striped
   * texture consisting of the last known colors.
   */
  public void setTextureIcon(Icon textureIcon) {
    if (textureIcon != null) {
      setupIconPainter(textureIcon);
    }

    else
      setupDefaultPainter(ourDefaultForeground, ourDefaultBackground);
  }

  /**
   * Returns the image buffer used by this TexturedPanel's painter.
   */
  public Image getTexture() {
    if (ourPainter == null)
      return null;
    else
      return ourPainter.getImage();
  }

  /**
   * Creates a new TexturePaint using the provided colors.
   */
  private void setupDefaultPainter(Color foreground, Color background) {
    if (foreground == null || background == null) {
      ourPainter = null;
      return;
    }

    BufferedImage buff = new BufferedImage(66, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();

    g2.setColor(background);
    g2.fillRect(0066);

    g2.setColor(foreground);
    g2.drawLine(0262);
    g2.drawLine(0565);

    ourPainter = new TexturePaint(buff, new Rectangle(0066));

    g2.dispose();
  }

  /**
   * Creates a new TexturePaint using the provided colors and texture map.
   */
  private void setupTexturePainter(Color foreground, Color background, boolean[][] texture,
      int scale) {
    if (texture == null || texture.length < || texture[0].length < 1) {
      setupDefaultPainter(foreground, background);
      return;
    }

    else if (foreground == null || background == null) {
      ourPainter = null;
      return;
    }

    scale = Math.max(1, scale);
    int w = texture[0].length;
    int h = texture.length;

    BufferedImage buff = new BufferedImage(w * scale, h * scale, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();

    g2.setColor(background);
    g2.fillRect(00, w * scale, h * scale);

    g2.setColor(foreground);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        try {
          if (texture[i][j])
            g2.fillRect(j * scale, i * scale, scale, scale);
        }
        // g2.drawLine(j, i, j, i); }
        catch (ArrayIndexOutOfBoundsException aob) {
        }
      }
    }

    ourPainter = new TexturePaint(buff, new Rectangle(00, w * scale, h * scale));

    g2.dispose();
  }

  /**
   * Creates a new TexturePaint using the provided image.
   */
  private void setupImagePainter(Image texture) {
    if (texture == null) {
      ourPainter = null;
      return;
    }

    int w = texture.getWidth(this);
    int h = texture.getHeight(this);

    if (w <= || h <= 0) {
      ourPainter = null;
      return;
    }

    BufferedImage buff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();
    g2.drawImage(texture, 00this);
    ourPainter = new TexturePaint(buff, new Rectangle(00, w, h));

    g2.dispose();
  }

  /**
   * Creates a new TexturePaint using the provided icon.
   */
  private void setupIconPainter(Icon texture) {
    if (texture == null) {
      ourPainter = null;
      return;
    }

    int w = texture.getIconWidth();
    int h = texture.getIconHeight();

    if (w <= || h <= 0) {
      ourPainter = null;
      return;
    }

    BufferedImage buff = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);

    Graphics2D g2 = buff.createGraphics();
    texture.paintIcon(this, g2, 00);
    ourPainter = new TexturePaint(buff, new Rectangle(00, w, h));

    g2.dispose();
  }

  /**
   * Paints this component with its textured background.
   */
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (ourPainter != null) {
      int w = getWidth();
      int h = getHeight();
      Insets in = getInsets();

      int x = in.left;
      int y = in.top;
      w = w - in.left - in.right;
      h = h - in.top - in.bottom;

      if (w >= && h >= 0) {
        Graphics2D g2 = (Graphics2Dg;
        Paint pt = g2.getPaint();
        g2.setPaint(ourPainter);
        g2.fillRect(x, y, w, h);
        g2.setPaint(pt);
      }
    }
  }

}

   
    
    
    
    
  














Related examples in the same category
1.Yes / No Panel
2.Transparent PanelTransparent Panel
3.Swing Panel GroupSwing Panel Group
4.Swing Panel Group 2Swing Panel Group 2
5.Gradient Panel
6.Transfer focus from button to button with help of arrows keys.Transfer focus from button to button with help of arrows keys.
7.Time panel shows the current time.
8.Graph Canvas
9.A basic panel that displays a small up or down arrow.
10.HeatMap is a JPanel that displays a 2-dimensional array of data using a selected color gradient scheme.
11.GradientPanel is a class with a gradient background.
12.A simple JPanel with a border and a title
13.Translucent PanelTranslucent Panel
14.Image Panel
15.The class makes it easy to select a numerical value, possibly from a given range
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.