Draws the texture from a backing bitmap, creating it on the first call. - Java 2D Graphics

Java examples for 2D Graphics:Texture

Description

Draws the texture from a backing bitmap, creating it on the first call.

Demo Code


import javax.swing.*;
import java.awt.*;
import java.awt.geom.Area;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

public class Main{
    private static int focusedHeight = -1;
    private static int unfocusedHeight = -1;
    private static Icon unfocused = null;
    private static Icon focused = null;
    private static Rectangle scratch = new Rectangle();
    private static final int DEFAULT_IMAGE_WIDTH = 200;
    public static final int FOCUS_TYPE = 4;
    /**// w w  w  .ja  v a 2 s .co  m
     * Draws the texture from a backing bitmap, creating it on the first call.
     * Profiling shows that 95% of the main window drawing time is spent
     * painting this texture on the output window border.  So instead, we
     * generate a bitmap on the first call and subsequent calls just blit it to
     * the screen.
     */
    private static void drawTexture(Graphics g, int x, int y, int width,
            int height, int type, int yDecline) {

        if (!g.hitClip(x, y, width, height)) {
            return;
        }
        if (type == FOCUS_TYPE) {
            if (focused == null || height > focusedHeight * 2) {
                //Create the focused backing bitmap
                Image img = createBitmap(height, type, yDecline);
                //Store the height in case somebody asks to paint a region
                //larger than our stored bitmap.  Probably will never happen,
                //but it would be difficult to diagnose if it did
                focusedHeight = height;

                focused = new ImageIcon(img);
            }
            blitBitmap(g, focused, x, y, width, height);

        } else {
            if (unfocused == null || unfocusedHeight > height * 2) {
                //create the unfocused backing bitmap
                Image img = createBitmap(height, type, yDecline);
                //Store the height in case somebody asks to paint a region
                //larger than our stored bitmap.  Probably will never happen,
                //but it would be difficult to diagnose if it did
                unfocusedHeight = height;
                unfocused = new ImageIcon(img);
            }
            blitBitmap(g, unfocused, x, y, width, height);
        }
    }
    /**
     * Create a backing bitmap, painting the texture into it with the specified
     * parameters.  The bitmap will be created at 2*height, so that even if
     * there is some minor variation in height, it will not force recreating the
     * bitmap
     */
    private static BufferedImage createBitmap(int height, int type,
            int yDecline) {

        //Create an optimal image for blitting to the screen with no format conversion
        BufferedImage result = GraphicsEnvironment
                .getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration()
                .createCompatibleImage(200, height * 2);

        Graphics g = result.getGraphics();

        if (result.getAlphaRaster() == null) {
            Color c = type == FOCUS_TYPE ? OfficeViewTabDisplayerUIOld
                    .getActBgColor() : OfficeViewTabDisplayerUIOld
                    .getInactBgColor();
            g.setColor(c);
            g.fillRect(0, 0, DEFAULT_IMAGE_WIDTH, height * 2);
        }

        //draw the texture into the offscreen image
        _drawTexture(g, 0, 0, DEFAULT_IMAGE_WIDTH, height * 2, type,
                yDecline);
        return result;
    }
    /**
     * Paint a backing bitmap into the specified rectangle of the passed
     * Graphics object.  Sets the clipping rectangle to match the coordinates
     * and loops until the rectangle has been filled with the texture.
     */
    private static void blitBitmap(Graphics g, Icon icon, int x, int y,
            int w, int h) {
        //Store the current clip to reset it after
        Shape clip = g.getClip();

        if (clip == null) {
            //Limit it to the space we're requested to paint
            g.setClip(x, y, w, h);
        } else {
            //If there is an existing clip, get the intersection with the
            //rectangle we want to paint and set that

            scratch.setBounds(x, y, w, h);
            Area area = new Area(clip);
            area.intersect(new Area(scratch));
            g.setClip(area);
        }
        int iwidth = icon.getIconWidth();
        int widthPainted = 0;
        while (widthPainted < w) {
            //Loop until we've covered the entire area
            icon.paintIcon(null, g, x + widthPainted, y);
            widthPainted += iwidth;
        }

        //restore the clip
        g.setClip(clip);
    }
    /**
     * Actually draws the texture. yDecline parameter is initial y-coordination
     * decline in pixels, effective values are <0,3>, will change the "shape" of
     * texture a bit.
     */
    private static void _drawTexture(Graphics g, int x, int y, int width,
            int height, int type, int yDecline) {
        Color brightC = UIManager.getColor("TabbedPane.highlight");
        Color darkC;
        if (type == FOCUS_TYPE) {
            darkC = UIManager.getColor("TabbedPane.focus");
        } else {
            darkC = UIManager.getColor("controlDkShadow");
        }
        // assure that last column and row will be dark - make width even
        // and height odd
        if (width % 2 != 0) {
            width--;
        }
        if (height % 2 != 0) {
            height--;
        }
        for (int curX = x; curX < x + width; curX++) {
            g.setColor((curX - x) % 2 == 0 ? brightC : darkC);
            for (int curY = y + ((curX - x + yDecline) % 4); curY < y
                    + height; curY += 4) {
                g.drawLine(curX, curY, curX, curY);
            }
        }
    }
}

Related Tutorials