/* * 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 ImageFill class. Scaled copies * of the background image can be created by dragging out rectangular * regions with the mouse. Note that we don't draw the (scaled) * image during the drag, doing so is usally too slow because the * original image is quite large. */ public class TestImageFill extends JPanel { /** * Backing store for the showImage property. */ private boolean showImage = true; /** * Initialized by the (first call to the) constructor. This * Fill is used to paint the entire panel. */ private static ImageFill imageFill = null; /** * Simple opaque line border - two concentric rectangles * drawn in the components foreground color, with a background * colored rectangle in between. */ private static Border border = new Border() { public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) { g.setColor(c.getForeground()); g.drawRect(x, y, w-1, h-1); g.drawRect(x+2, y+2, w-5, h-5); g.setColor(c.getBackground()); g.drawRect(x+1, y+1, w-3, h-3); } public Insets getBorderInsets(Component c) { return new Insets(3, 3, 3, 3); // top left bottom right } public boolean isBorderOpaque() { return true; } }; /** * Hand press/drag/release by interactively resizing a child * TestImageFill instance. During the drag we set the * showImage property to false to ensure that performance is * snappy. */ private class MouseHandler implements MouseListener, MouseMotionListener { Point anchor = new Point(); TestImageFill drag = null; public void mousePressed(MouseEvent e) { anchor.x = e.getX(); anchor.y = e.getY(); } public void mouseDragged(MouseEvent e) { int x = Math.min(anchor.x, e.getX()); int y = Math.min(anchor.y, e.getY()); int w = Math.abs(anchor.x - e.getX()); int h = Math.abs(anchor.y - e.getY()); if (drag == null) { drag = new TestImageFill(); drag.setShowImage(false); add(drag, 0); } if ((w > 0) && (h > 0)) { drag.setBounds(x, y, w, h); } } public void mouseReleased(MouseEvent e) { if (drag != null) { drag.setShowImage(true); drag = null; } } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} } /** * Create a TestImageFill instance that uses an ImageFill * object to draw "background.jpg". */ public TestImageFill() { super(null); setBorder(border); setForeground(Color.black); setBackground(Color.white); MouseHandler mouseHandler = new MouseHandler(); addMouseListener(mouseHandler); addMouseMotionListener(mouseHandler); if (imageFill == null) { URL file = getClass().getResource("background.jpg"); try { BufferedImage image = ImageIO.read(file); imageFill = new ImageFill(image); } catch(IOException e) { e.printStackTrace(); } } } /** * If false, the background.jpg image, isn't drawn. This * property is temporarily set to false while press/drag/release * gesture is underway. The default value of this property * is true. * * @see #getShowImage */ public void setShowImage(boolean showImage) { this.showImage = showImage; repaint(); } /** * Returns true if the image should be drawn. * @return the value of the showImage property. * @see #setShowImage */ public boolean getShowImage() { return showImage; } /** * Fills the component with the background.jpg image * using an ImageFill object. */ public void paintComponent(Graphics g) { super.paintComponent(g); if (getShowImage()) { Graphics gFill = g.create(); imageFill.paintFill(this, gFill); gFill.dispose(); } } /** * The preferred size is equal to the size of the ImageFill * created for 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 = imageFill.getWidth() + insets.left + insets.right; int h = imageFill.getHeight() + insets.top + insets.bottom; return new Dimension(w, h); } /** * Overridden to return false which means that children may overlap. *

* This is a wee bit obscure. */ public boolean isOptimizedDrawingEnabled() { return false; } public static void main(String[] args) throws Exception { JFrame f = new JFrame("Image Fill"); f.getContentPane().add(new TestImageFill(), BorderLayout.CENTER); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; f.addWindowListener(l); f.pack(); f.setVisible(true); } }