Create a backing bitmap, painting the texture into it with the specified parameters. - Java 2D Graphics

Java examples for 2D Graphics:Texture

Description

Create a backing bitmap, painting the texture into it with the specified parameters.

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{
    public static void main(String[] argv) throws Exception{
        int height = 2;
        int type = 2;
        int yDecline = 2;
        System.out.println(createBitmap(height,type,yDecline));
    }/*from   w w w  . j  a  v a2  s .  c  om*/
    private static final int DEFAULT_IMAGE_WIDTH = 200;
    public static final int FOCUS_TYPE = 4;
    /**
     * 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;
    }
    /**
     * 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