/* * Copyright 2002 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. */ import java.io.*; import java.net.URL; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import javax.imageio.*; /** * Simple demonstration of the TiledFill class. Fill a window, using * a TiledFill object, with the texture.jpg image. Dragging on the lower * right hand corner of the green rectangle changes the tile size. */ public class TestTiledFill extends JPanel { /** * Initialized by the (first call to the) constructor. This * Fill is used to paint the entire panel. */ private static TiledFill tiledFill = null; /** * A simple rectangular outline, intended to represent the size * of a single tile, that can be resized by dragging the mouse * on the bottom right corner. */ private class ResizePanel extends JPanel implements MouseListener, MouseMotionListener { private boolean doDrag = false; private final Color color = new Color(0x99, 0xFF, 0x99); private final Stroke stroke = new BasicStroke(4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER); public ResizePanel() { super(null); addMouseListener(this); addMouseMotionListener(this); } public void paintComponent(Graphics g) { int w = getWidth(); int h = getHeight(); Graphics2D g2d = (Graphics2D)g; g2d.setColor(color); Stroke defaultStroke = g2d.getStroke(); g2d.setStroke(stroke); g2d.drawRect(2, 2, w - 3, h - 3); g2d.setStroke(defaultStroke); if (doDrag){ g2d.setColor(Color.blue); } g2d.fillRect(w - 11, h - 11, 11, 11); } public boolean isOpaque() { return false; } public void mousePressed(MouseEvent e) { int resizeHandleX = getWidth() - 11; int resizeHandleY = getHeight() - 11; if ((e.getX() >= resizeHandleX) && (e.getY() >= resizeHandleY)){ doDrag = true; } } private void resetTile(int width, int height) { tiledFill.setTileWidth(width); tiledFill.setTileHeight(height); getParent().repaint(); } public void mouseDragged(MouseEvent e) { if (doDrag){ int w = e.getX(); int h = e.getY(); if ((w > 1) && (h > 1)){ setSize(w, h); resetTile(w, h); } } } public void mouseReleased(MouseEvent e) { doDrag = false; tiledFill.setTileWidth(getWidth()); tiledFill.setTileHeight(getHeight()); getParent().repaint(); } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } } /** * Create a TestTiledFill instance that uses an TiledFill * object to draw "texture.jpg". */ public TestTiledFill() { super(null); setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); setForeground(Color.black); setBackground(Color.white); if (tiledFill == null){ URL file = getClass().getResource("texture.jpg"); try{ BufferedImage image = ImageIO.read(file); ImageFill fill = new ImageFill(image); tiledFill = new TiledFill(fill, image.getWidth(), image.getHeight()); } catch(IOException e){ e.printStackTrace(); } } Insets insets = getInsets(); int x = insets.left; int y = insets.top; int w = tiledFill.getTileWidth() - (insets.left + insets.right); int h = tiledFill.getTileHeight() - (insets.top + insets.bottom); JPanel resizePanel = new ResizePanel(); resizePanel.setBounds(x, y, w, h); add(resizePanel); } /** * Paint the area within g.getClipBounds() * making sure that the new tiles are aligned with a tileWidth * by tileHeight grid whose origin is at 0,0. */ public void paintComponent(Graphics g) { super.paintComponent(g); /* To ensure that the tiles we paint are aligned with * a tileWidth X tileHeight grid whose origin is 0,0 we * enlarge the clipBounds rectangle so that its origin * is aligned with the origin of a tile and its size * is a multiple of the tile size. */ Rectangle clip = g.getClipBounds(); int tw = tiledFill.getTileWidth(); int th = tiledFill.getTileHeight(); int x = (clip.x / tw) * tw; int y = (clip.y / th) * th; int w = (((clip.x + clip.width + tw-1) / tw) * tw) - x; int h = (((clip.y + clip.height + th-1) / th) * th) - y; Graphics gFill = g.create(); tiledFill.paintFill(this, gFill, new Rectangle(x, y, w, h)); gFill.dispose(); } /** * The preferred size is equal to the size of a * 3x2 grid of ImageFill sized tiles (created from the * "background.jpg" image plus the insets space * allocated to the border. * * @returns the size of the background image and the border. */ public Dimension getPreferredSize() { Insets insets = getInsets(); int w = (3 * tiledFill.getTileWidth()) + insets.left + insets.right; int h = (2 * tiledFill.getTileHeight()) + insets.top + insets.bottom; return new Dimension(w, h); } public static void main(String[] args) throws Exception { JFrame f = new JFrame("Tile Fill"); f.getContentPane().add(new TestTiledFill(), BorderLayout.CENTER); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; f.addWindowListener(l); f.pack(); f.setVisible(true); } }