📄 imagebean4.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.*;
import java.beans.beancontext.*;
/** 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.
* This bean makes use of the BeanContext API to find
* and make use of a method tracing service.
*/
public class ImageBean4 extends JPanel
implements BeanContextProxy {
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();
private ImageBeanContextChildSupport
ibccs = new ImageBeanContextChildSupport( this );
/** 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 ImageBean4() {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
setFileName("..\\demo\\sunw\\demo\\juggler\\"
+"Juggler0.gif");
}
/** Send an event to all registered listeners */
public void fireFillColorEvent( FillColorEvent e ) {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
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() {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
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() {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
setScaling( SCALED_TO_FIT );
}
/** Accessor for the filename property.
* @return The current image's filename
*/
public String getFileName() {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
return fileName;
}
/** Accessor for the fillColor property
* @return The current fill color
*/
public Color getFillColor() {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
return fill;
}
/** The preferred size of the panel
* @return The size of the current image
*/
public Dimension getPreferredSize() {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
return new Dimension( width, height );
}
/** How the image is drawn within the panel
*/
public int getScaling() {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
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 ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
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() {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
setFillColor( Color.green );
}
/** Set the image fill color to red. The Sun
* BeanBox recognizes only methods without
* parameters that return void.
*/
public void makeFillRed() {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
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 ) {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
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 {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
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 ) {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
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 ) {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
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 ) {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
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 ) {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
fillColorListeners.addElement( l );
}
/** Add a listener interested in property change
* events
*/
public void addPropertyChangeListener(
PropertyChangeListener l ) {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
myListeners.addPropertyChangeListener( l );
}
/** Remove a listener no longer interested in
* FillColorEvent objects
*/
public void removeFillColorListener(
FillColorListener l ) {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
fillColorListeners.removeElement( l );
}
/** Remove a listener no longer interested in
* property change events
*/
public void
removePropertyChangeListener(
PropertyChangeListener l ) {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
myListeners.removePropertyChangeListener( l );
}
/** Return a reference to this object's BeanContextChild
* field
*/
public BeanContextChild getBeanContextProxy() {
if ( ibccs.getMethodTracer() != null ) {
ibccs.getMethodTracer().traceMethod();
}
return ibccs;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -