FileDocCategorySizeDatePackage
SplitImageComponent.javaAPI DocExample2632Mon Apr 05 11:13:10 BST 1999None

SplitImageComponent

public class SplitImageComponent extends JPanel

Fields Summary
private BufferedImage
mImage
private BufferedImage
mSecondImage
private int
mSplitX
Constructors Summary
public SplitImageComponent(String path)

    setImage(path);
    init();
  
public SplitImageComponent(BufferedImage image)

    setImage(image);
    init();
  
Methods Summary
public java.awt.image.BufferedImagegetImage()

 return mImage; 
public java.awt.DimensiongetPreferredSize()

    int width = getImage().getWidth();
    int height = getImage().getHeight();
    if (mSecondImage != null) {
      width = Math.max(width, mSecondImage.getWidth());
      height = Math.max(height, mSecondImage.getHeight());
    }
    return new Dimension(width, height);
  
public java.awt.image.BufferedImagegetSecondImage()

 return mSecondImage; 
private voidinit()

    setBackground(Color.white);
    addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent me) {
        mSplitX = me.getX();
        repaint();
      }
    });
    addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseDragged(MouseEvent me) {
        mSplitX = me.getX();
        repaint();
      }
    });
  
public voidpaint(java.awt.Graphics g)

    Graphics2D g2 = (Graphics2D)g;
    int width = getSize().width;
    int height = getSize().height;
    
    // Explicitly clear the window.
    Rectangle clear = new Rectangle(0, 0, width, height);
    g2.setPaint(getBackground());
    g2.fill(clear);
    // Clip the first image, if appropriate,
    //   to be on the right side of the split.
    if (mSplitX != 0 && mSecondImage != null) {
      Rectangle firstClip = new Rectangle(mSplitX, 0,
          width - mSplitX, height);
      g2.setClip(firstClip);
    }
    g2.drawImage(getImage(), 0, 0, null);
    
    if (mSplitX == 0 || mSecondImage == null) return;
    
    Rectangle secondClip = new Rectangle(0, 0, mSplitX, height);
    g2.setClip(secondClip);
    g2.drawImage(mSecondImage, 0, 0, null);
    
    Line2D splitLine = new Line2D.Float(mSplitX, 0, mSplitX, height);
    g2.setClip(null);
    g2.setColor(Color.white);
    g2.draw(splitLine);
  
public voidsetImage(java.lang.String path)

    Image image = Utilities.blockingLoad(path);
    mImage = Utilities.makeBufferedImage(image);
  
public voidsetImage(java.awt.image.BufferedImage image)

 mImage = image; 
public voidsetSecondImage(java.awt.image.BufferedImage image)

    mSecondImage = image;
    repaint();