Java examples for 2D Graphics:Image
Paint a check pattern, used for a background to indicate image transparency.
//package com.java2s; import java.awt.*; import java.awt.image.*; public class Main { private static BufferedImage backgroundImage = null; /**/*from ww w. ja v a2s. co m*/ * Paint a check pattern, used for a background to indicate image transparency. * @param c the component to draw into * @param g the Graphics objects * @param x the x position * @param y the y position * @param width the width * @param height the height */ public static void paintCheckedBackground(Component c, Graphics g, int x, int y, int width, int height) { if (backgroundImage == null) { backgroundImage = new BufferedImage(64, 64, BufferedImage.TYPE_INT_ARGB); Graphics bg = backgroundImage.createGraphics(); for (int by = 0; by < 64; by += 8) { for (int bx = 0; bx < 64; bx += 8) { bg.setColor(((bx ^ by) & 8) != 0 ? Color.lightGray : Color.white); bg.fillRect(bx, by, 8, 8); } } bg.dispose(); } if (backgroundImage != null) { Shape saveClip = g.getClip(); Rectangle r = g.getClipBounds(); if (r == null) r = new Rectangle(c.getSize()); r = r.intersection(new Rectangle(x, y, width, height)); g.setClip(r); int w = backgroundImage.getWidth(); int h = backgroundImage.getHeight(); if (w != -1 && h != -1) { int x1 = (r.x / w) * w; int y1 = (r.y / h) * h; int x2 = ((r.x + r.width + w - 1) / w) * w; int y2 = ((r.y + r.height + h - 1) / h) * h; for (y = y1; y < y2; y += h) for (x = x1; x < x2; x += w) g.drawImage(backgroundImage, x, y, c); } g.setClip(saveClip); } } }