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

📄 myimagepanel.java

📁 基于jxta的局域网P2P文件共享,可以实现局域网中的文件p2p共享,实现文件快速传输及交流
💻 JAVA
字号:
package connex.plugins.Messenger;

import org.jdesktop.swingx.JXImagePanel;
import java.awt.event.MouseAdapter;
import javax.swing.ImageIcon;
import java.io.File;
import javax.swing.JPanel;
import javax.swing.JFileChooser;
import java.awt.event.MouseEvent;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.Insets;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import connex.app.utils.fileChooserUtils.ImageFilter;
import connex.app.utils.fileChooserUtils.ImagePreview;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class MyImagePanel
    extends JXImagePanel {
  /**
   * If true, then the image can be changed. Perhaps a better name is
   * &quot;readOnly&quot;, but editable was chosen to be more consistent
   * with other Swing components.
   */
  private boolean editable = false;
  /**
   * The mouse handler that is used if the component is editable
   */
  private MouseHandler mhandler = new MouseHandler();
  /**
   * Text informing the user that clicking on this component will allow them to set the image
   */
  private static final String TEXT =
      "<html><i><b>Click here<br>to set the image</b></i></html>";

  public MyImagePanel() {
  }

  public void setEditable(boolean editable) {
    if (editable != this.editable) {
      //if it was editable, remove the mouse handler
      if (this.editable) {
        removeMouseListener(mhandler);
      }
      this.editable = editable;
      //if it is now editable, add the mouse handler
      if (this.editable) {
        addMouseListener(new MouseHandler());
      }
      setToolTipText(editable ? TEXT : "");
      firePropertyChange("editable", !editable, editable);
      repaint();
    }
  }

  /**
   * @return whether the image for this panel can be changed or not via
   * the UI. setImage may still be called, even if <code>isEditable</code>
   * returns false.
   */
  public boolean isEditable() {
    return editable;
  }

  /**
   * Handles click events on the component
   */
  private class MouseHandler
      extends MouseAdapter {
    private Cursor oldCursor;
    private JFileChooser chooser;

    public void mouseClicked(MouseEvent evt) {
      if (chooser == null) {
        chooser = new JFileChooser();
        //Add a custom file filter and disable the default
      //(Accept All) file filter.
      chooser.addChoosableFileFilter(new ImageFilter());
      chooser.setAcceptAllFileFilterUsed(false);
      //Add the preview pane.
      chooser.setAccessory(new ImagePreview(chooser));

      }
      int retVal = chooser.showOpenDialog(MyImagePanel.this);
      if (retVal == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        try {
          Image img = new ImageIcon(file.toURI().toURL()).getImage();
          Insets insets = getInsets();
          final int imgWidth = img.getWidth(null);
          final int imgHeight = img.getHeight(null);

          final int pw = getWidth() - insets.left; //- insets.right;
          final int ph = getHeight() - insets.top ;//- insets.bottom;

          int w;
          int h;
          if ( (imgWidth - pw) > (imgHeight - ph)) {
            w = pw;
            final float ratio = ( (float) w) / ( (float) imgWidth);
            h = (int) (imgHeight * ratio);
          }
          else {
            h = ph;
            final float ratio = ( (float) h) / ( (float) imgHeight);
            w = (int) (imgWidth * ratio);
          }

          BufferedImage dst = new BufferedImage(w, h,
                                                BufferedImage.TYPE_INT_ARGB);
          Graphics2D g2 = (Graphics2D) dst.getGraphics();
          g2.drawImage(img, 0, 0, w, h, null);
           g2.dispose();

          setImage( (Image) dst);
        }
        catch (Exception ex) {
        }
      }
    }

    public void mouseEntered(MouseEvent evt) {
      if (evt.getSource() instanceof JPanel) {
        JPanel label = (JPanel) evt.getSource();
        if (oldCursor == null) {
          oldCursor = label.getCursor();
          label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }
      }
    }

    public void mouseExited(MouseEvent evt) {
      JPanel label = (JPanel) evt.getSource();
      if (oldCursor != null) {
        label.setCursor(oldCursor);
        oldCursor = null;
      }
    }
  }

}

⌨️ 快捷键说明

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