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

📄 imagebean2.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
  */
public class ImageBean2 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();
   /** No-argument constructor; sets the filename to a
     * default value.
     */
   public ImageBean2() {
      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 );
         }
      }
   }
   /** Accessor for the filename property.
     * @return The current image's filename
     */
   public String getFileName() {
      return fileName;
   }
   /** The preferred size of the panel
     * @return The size of the current image
     */
   public Dimension getPreferredSize() {
      return new Dimension( width, height );
   }
   /** Accessor for the fillColor property
     * @return The current fill color
     */
   public Color getFillColor() {
      return fill;
   }
   /** 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 then draw the image.
     * @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 ( panelSize.width > width ||
           panelSize.height > height ) {
         g.setColor( fill );
         g.fillRect( ins.left, ins.top,
                     actWidth, actHeight );
      }
      if ( image != null ) {
         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();
   }
   /** 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 + -