📄 picturepanel.java
字号:
// HTMLParser Library $Name: v1_6 $ - A java-based parser for HTML// http://sourceforge.org/projects/htmlparser// Copyright (C) 2003 Derrick Oswald//// Revision Control Information//// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/lexerapplications/thumbelina/PicturePanel.java,v $// $Author: derrickoswald $// $Date: 2005/04/12 11:27:41 $// $Revision: 1.2 $//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//package org.htmlparser.lexerapplications.thumbelina;import java.awt.Component;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Image;import java.awt.Insets;import java.awt.Point;import java.awt.Rectangle;import java.awt.event.ComponentEvent;import java.awt.event.ComponentListener;import java.awt.event.HierarchyEvent;import java.awt.event.HierarchyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.util.Enumeration;import java.util.HashSet;import javax.swing.JPanel;import javax.swing.JViewport;import javax.swing.Scrollable;import javax.swing.border.BevelBorder;/** * Hold and display a group of pictures. * @author derrick */public class PicturePanel extends JPanel implements MouseListener, Scrollable, ComponentListener, HierarchyListener{ /** * Scrolling unit increment (both directions). */ protected static final int UNIT_INCREMENT = 10; /** * Scrolling block increment (both directions). */ protected static final int BLOCK_INCREMENT = 100; /** * The thumbelina object in use. */ protected Thumbelina mThumbelina; /** * The display mosaic. */ protected TileSet mMosaic; /** * The preferred size of this component. * <code>null</code> initially, caches the results of * <code>calculatePreferredSize ()</code>. */ protected Dimension mPreferredSize; /** * Creates a new instance of PicturePanel * @param thumbelina The <code>Thumeblina</code> this panel is associated * with. */ public PicturePanel (final Thumbelina thumbelina) { mThumbelina = thumbelina; mMosaic = new TileSet (); mPreferredSize = null; setBorder (new BevelBorder (BevelBorder.LOWERED)); addMouseListener (this); addHierarchyListener (this); } /** * Clears the panel, discarding any existing images. */ public void reset () { mMosaic = new TileSet (); repaint (); } /** * Move the given picture to the top of the Z order. * Adds it, even it if it doesn't exist. * Also puts the URL in the url text of the status bar. * @param picture The picture being brought forward. */ public void bringToTop (final Picture picture) { picture.reset (); mMosaic.bringToTop (picture); repaint (picture.x, picture.y, picture.width, picture.height); mThumbelina.mUrlText.setText (picture.getURL ().toExternalForm ()); } /** * Find a picture with the given URL in the panel. * This should really only be used to discover if the picture is still * visible. There could be more than one picture with the given URL * because it may be partially obscured by another picture, in which * case the pieces are each given their own picture object, but all * point at the same <code>URL</code> and <code>Image</code>. * @param url The url to locate. * @return The first picture encountered in the panel, * or null if the picture was not found. */ public Picture find (final String url) { Enumeration enumeration; Picture picture; Picture ret; ret = null; enumeration = mMosaic.getPictures (); while ((null == ret) && enumeration.hasMoreElements ()) { picture = (Picture)enumeration.nextElement (); if (url.equals (picture.getURL ().toExternalForm ())) ret = picture; } return (ret); } /** * Draw an image on screen. * @param picture The picture to draw. * @param add If <code>true</code>, the picture is added to the history. */ protected void draw (final Picture picture, final boolean add) { Component parent; boolean dolayout; Dimension before; Dimension after; parent = getParent (); dolayout = false; synchronized (mMosaic) { if (parent instanceof JViewport) { before = getPreferredSize (); mMosaic.add (picture); after = calculatePreferredSize (); if (after.width > before.width) dolayout = true; else after.width = before.width; if (after.height > before.height) dolayout = true; else after.height = before.height; if (dolayout) mPreferredSize = after; } else mMosaic.add (picture); } if (dolayout) revalidate (); repaint (picture.x, picture.y, picture.width, picture.height); if (add) mThumbelina.addHistory (picture.getURL ().toExternalForm ()); } /** * Updates this component. * @param graphics The graphics context in which to update the component. */ public void update (final Graphics graphics) { paint (graphics); } /** * Adjust the graphics clip region to account for insets. * @param graphics The graphics object to set the clip region for. */ public void adjustClipForInsets (final Graphics graphics) { Dimension dim; Insets insets; Rectangle clip; dim = getSize (); insets = getInsets (); clip = graphics.getClipBounds (); if (clip.x < insets.left) clip.x = insets.left; if (clip.y < insets.top) clip.y = insets.top; if (clip.x + clip.width > dim.width - insets.right) clip.width = dim.width - insets.right - clip.x; if (clip.y + clip.height > dim.height - insets.bottom) clip.height = dim.height - insets.bottom - clip.y; graphics.setClip (clip.x, clip.y, clip.width, clip.height); } /** * Paints this component. * Runs through the list of tiles and for every one that intersects * the clip region performs a <code>drawImage()</code>. * @param graphics The graphics context used to paint with. */ public void paint (final Graphics graphics) { Rectangle clip; Enumeration enumeration; HashSet set; // just so we don't draw things twice Picture picture; Image image; Point origin; int width; int height; adjustClipForInsets (graphics); clip = graphics.getClipBounds (); synchronized (mMosaic) { if (0 == mMosaic.getSize ()) super.paint (graphics); else { super.paint (graphics); enumeration = mMosaic.getPictures (); set = new HashSet (); while (enumeration.hasMoreElements ()) { picture = (Picture)enumeration.nextElement (); if ((null == clip) || (clip.intersects (picture))) { image = picture.getImage (); if (!set.contains (image)) { origin = picture.getOrigin (); width = image.getWidth (this); height = image.getHeight (this); graphics.drawImage (picture.getImage (), origin.x, origin.y, origin.x + width, origin.y + height, 0, 0, width, height, this); set.add (image); } } } } } } /** * Get the preferred size of the component. * @return The dimension of this component. */ public Dimension getPreferredSize () { if (null == mPreferredSize) setPreferredSize (calculatePreferredSize ()); else if ((0 == mPreferredSize.width) || (0 == mPreferredSize.height)) setPreferredSize (calculatePreferredSize ()); return (mPreferredSize); } /** * Sets the preferred size of this component. * @param dimension The new value to use for * <code>getPreferredSize()</code> until recalculated. */ public void setPreferredSize (final Dimension dimension) { mPreferredSize = dimension; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -