⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagebean3.java

📁 程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件
💻 JAVA
字号:
package examples.beans;
import javax.swing.JPanel;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.image.ImageObserver;
import java.beans.*;
/** A very simple JavaBean class that displays an image
  * and allows the user to specify the file containing
  * the image and the fill color to be used if the
  * image is smaller than the panel.  There is also a
  * scaling property that allows the image to be shown
  * in its original size or scaled to fit in the panel.
  */
public class ImageBean3 extends JPanel {
   private String fileName;
   private transient Image image;
   private int width = 200;
   private int height=150;
   private Color fill = Color.lightGray;
   private PropertyChangeSupport
      myListeners = new PropertyChangeSupport( this );
   private Vector fillColorListeners = new Vector();
   /** Specify how the image is drawn, must be one of
     * the constants defined below */
   private int scaling;
   /** Draw the image in its original size */
   public static final int ORIGINAL_SIZE = 0;
   /** Scale the image to fit in the panel */
   public static final int SCALED_TO_FIT = 1;
   /** No-argument constructor; sets the filename to a
     * default value.
     */
   public ImageBean3() {
      setFileName("..\\demo\\sunw\\demo\\juggler\\"
                  +"Juggler0.gif");
   }
   /** Send an event to all registered listeners */
   public void fireFillColorEvent( FillColorEvent e ) {
      Vector snapshot
         = (Vector) fillColorListeners.clone();
      Enumeration cursor = snapshot.elements();
      while( cursor.hasMoreElements() ) {
         FillColorListener fcl
            = (FillColorListener) cursor.nextElement();
         if ( e.getID()
                     == FillColorEvent.COLOR_CHANGE ) {
            fcl.fillColorChange( e );
         }
      }
   }
   /** Display the image at its original size.  The Sun
     * BeanBox recognizes only methods without
     * parameters that return void.
     */
   public void displayOriginalSize() {
      setScaling( ORIGINAL_SIZE );
   }
   /** Display the image scaled to fit the panel.  The
     * Sun BeanBox recognizes only methods without
     * parameters that return void.
     */
   public void displayScaledToFit() {
      setScaling( SCALED_TO_FIT );
   }
   /** Accessor for the filename property.
     * @return The current image's filename
     */
   public String getFileName() {
      return fileName;
   }
   /** Accessor for the fillColor property
     * @return The current fill color
     */
   public Color getFillColor() {
      return fill;
   }
   /** The preferred size of the panel
     * @return The size of the current image
     */
   public Dimension getPreferredSize() {
      return new Dimension( width, height );
   }
   /** How the image is drawn within the panel
     */
   public int getScaling() {
      return scaling;
   }
   /** Method for monitoring the progress of the
     * loading of the current image.
     */
   public boolean imageUpdate( Image img,
                               int infoflags,
                               int x, int y,
                               int w, int h ) {
      if ( (infoflags & ImageObserver.ALLBITS) != 0 ) {
         int oldWidth = width;
         int oldHeight = height;
         width = img.getWidth(null);
         height = img.getHeight(null);
         if ( oldWidth != width
                            || oldHeight != height ) {
            myListeners.firePropertyChange(
                  "preferredSize",
                  new Dimension( oldWidth, oldHeight ),
                  new Dimension( width, height ) );
         }
         repaint();
         return false;
      } else {
         return true;
      }
   }
   /** Set the image fill color to green.  The Sun
     * BeanBox recognizes only methods without
     * parameters that return void.
     */
   public void makeFillGreen() {
      setFillColor( Color.green );
   }
   /** Set the image fill color to red.  The Sun
     * BeanBox recognizes only methods without
     * parameters that return void.
     */
   public void makeFillRed() {
      setFillColor( Color.red );
   }
   /** Paint the fill color if the panel is bigger than
     * the image and the image will be displayed
     * original size.  Then draw the image according to
     * the selected scaling type.
     * @param g the panel's graphics context
     */
   public void paintComponent( Graphics g ) {
      super.paintComponent( g );
      Dimension panelSize = getSize();
      Insets ins = getInsets();
      int actWidth = panelSize.width - ins.right
                        - ins.left - 1;
      int actHeight = panelSize.height - ins.top
                        - ins.bottom - 1;
      if ( scaling == ORIGINAL_SIZE &&
           ( panelSize.width > width ||
             panelSize.height > height ) ) {
         g.setColor( fill );
         g.fillRect( ins.left, ins.top,
                     actWidth, actHeight );
      }
      if ( image != null ) {
         if ( scaling == SCALED_TO_FIT ) {
            g.drawImage( image, ins.left, ins.top,
                         actWidth, actHeight,
                         fill, this );
         } else {
            g.drawImage( image, ins.left, ins.top, this );
         }
      }
   }
   /** Deserialization method called for the JavaBean.
     * This is necessary because Image objects can be
     * serialized and must be regenerated manually.
     * @exception IOException if an error occurs
     *            reading the serialized JavaBean.
     * @exception ClassNotFoundException if the
     *            serialized JavaBean can't be found.
     */
   private void readObject( ObjectInputStream ois )
           throws IOException, ClassNotFoundException {
      ois.defaultReadObject();
      image = getToolkit().getImage( fileName );
      repaint();
   }
   /** Mutator method for the fillColor property.
     * @param c the new fill color value
     */
   public void setFillColor( Color c ) {
      Color oldFill = fill;
      fill = c;
      myListeners.firePropertyChange( "fillColor",
                                       oldFill,
                                       fill );
      fireFillColorEvent( new FillColorEvent( this,
                           FillColorEvent.COLOR_CHANGE,
                           c ) );
      repaint();
   }
   /** Mutator method for the fileName property.
     * @param fn the new image filename
     */
   public void setFileName( String fn ) {
      String oldFileName = fileName;
      fileName = fn;
      image = getToolkit().getImage( fileName );
      myListeners.firePropertyChange( "fileName",
                                       oldFileName,
                                       fileName );
      repaint();
   }
   /** Mutator method for the image-scaling property
     * Used to specify how the image should be drawn
     * within the panel.
     * @param s the scaling type, either ORIGINAL_SIZE
     *          or SCALED_TO_FIT
     */
   public void setScaling( int s ) {
      int oldScaling = scaling;
      scaling = s;
      myListeners.firePropertyChange( "scaling",
                             new Integer( oldScaling ),
                             new Integer( scaling ) );
   }
   /** Add a listener interested in FillColorEvent
     * objects
     */
   public void
   addFillColorListener( FillColorListener l ) {
      fillColorListeners.addElement( l );
   }
   /** Add a listener interested in property change
     * events
     */
   public void addPropertyChangeListener(
                           PropertyChangeListener l ) {
      myListeners.addPropertyChangeListener( l );
   }
   /** Remove a listener no longer interested in
     * FillColorEvent objects
     */
   public void removeFillColorListener(
                                FillColorListener l ) {
      fillColorListeners.removeElement( l );
   }
   /** Remove a listener no longer interested in
     * property change events
     */
   public void
   removePropertyChangeListener(
                           PropertyChangeListener l ) {
      myListeners.removePropertyChangeListener( l );
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -