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

📄 imagebean1.java

📁 程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件
💻 JAVA
字号:
package examples.beans;
import javax.swing.JPanel;
import java.awt.*;
import java.io.*;
import java.awt.image.ImageObserver;
/** 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 ImageBean1 extends JPanel {
   private String fileName = "";
   private transient Image image;
   private int width = 200;
   private int height = 150;
   private Color fill = Color.lightGray;
   /** No-argument constructor; sets the filename to a
     * default value.
     */
   public ImageBean1() {
      setFileName( "..\\demo\\sunw\\demo\\juggler\\"
                   + "Juggler0.gif" );
   }
   /** 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 ) {
         width = img.getWidth( null );
         height = img.getHeight( null );
         repaint();
         return false;
      } else {
         return true;
      }
   }
   /** 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 ) {
      fill = c;
      repaint();
   }
   /** Mutator method for the fileName property.
     * @param fn the new image filename
     */
   public void setFileName( String fn ) {
      fileName = fn;
      image = getToolkit().getImage( fileName );
      repaint();
   }
}

⌨️ 快捷键说明

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