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

📄 previewscrollpane.java

📁 java写的图片浏览器,类似acds
💻 JAVA
字号:
package org.net9.oops.jsee;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import javax.swing.event.*;
//import javax.media.*;
import java.io.*;
//import java.util.*;
import java.net.*;

public class PreviewScrollPane extends JScrollPane {
    JLabel label = new JLabel();
    ImageIcon image,
              originalImage;
    Jsee parentFrame;

    int Preview_Area_Width,
        Preview_Area_Height;

    boolean noImageYet = true;
    boolean jmfInstalled;
    boolean movieIsPlaying;
    MoviePanel moviePanel;

    public PreviewScrollPane(Jsee parent) {
        setViewportView(label);
        parentFrame = parent;

        try {
            Class jmfClass = Class.forName("javax.media.Player");                    
            jmfInstalled = true;
        } catch(ClassNotFoundException e){
            jmfInstalled = false;
            System.err.println("JMF probably not installed");
        }
        
        addComponentListener(new ComponentListener() {
            public void componentResized( ComponentEvent e ) {
                ResizePreview();
            }
            public void componentMoved( ComponentEvent e ) {}
            public void componentShown( ComponentEvent e ) {}
            public void componentHidden( ComponentEvent e ) {}
        });

        addMouseListener(
            new MouseListener() {
                public void mouseClicked( MouseEvent event ) {/*double click: display image fullscreen*/}
                public void mousePressed /*Unix*/( MouseEvent event ) {
                    PopupMenu(event);
                }
                public void mouseReleased /*Windows*/( MouseEvent event ) {
                    PopupMenu(event);
                }
                // have to have these functions
                public void mouseEntered( MouseEvent event ) {}
                public void mouseExited( MouseEvent event ) {}
            }
        );
    }

    public void PopupMenu(MouseEvent event) {
        if ( event.isPopupTrigger() )
        {
            int x = event.getX() ;
            int y = event.getY() ; //20;
            if (x >= (int)getViewport().getWidth() - 117)
                x = (int)getViewport().getWidth() - 117;
            if (y >= (int)getViewport().getHeight() - 120)
                y = (int)getViewport().getHeight() - 120;
            parentFrame.menu.show(label, x, y);
        }
    }

    public void DisplayImage(String filename) {
      image = new ImageIcon(filename);

      // check if movie is playing
      if (moviePanel != null) {
          //System.out.println("Yes it's still on mate");
          moviePanel.stop();
          //setViewportView(label);
          this.remove(moviePanel);
          movieIsPlaying = false;
      }
      if (image.getIconHeight() < 0 )
      {
          try {
              String lowerCaseFileName = filename.toLowerCase();
              if (lowerCaseFileName.endsWith(".xpm")) {
                  FileInputStream file = new FileInputStream (filename);
                  DataInputStream in = new DataInputStream (file);
                  byte[] buff = new byte[in.available()];
                  int i = file.read(buff);
                  String s = new String(buff);
                  image = new ImageIcon(Xpm.XpmToImage(s));
              }
              else if (lowerCaseFileName.endsWith(".bmp")) {
                  try {
                      URL url = (new File(filename)).toURL();
                      image = new ImageIcon(createImage(BMPReader.getBMPImage(url.openStream())));
                  } catch ( MalformedURLException e ) {System.out.println("File or URL exception");}
                    catch (IOException e) {}
              }
              else if (lowerCaseFileName.endsWith(".pcx"))
              {
                try {
                  image = new ImageIcon(PcxReader102V01.loadImage(new FileInputStream(filename)));
                } catch (NullPointerException ne) {}
              }
              else // try to open by media player
              {
                  if (jmfInstalled) {                    
                    ClearPreviewArea();
                    GetPreviewAreaSize();
                    moviePanel = new MoviePanel((new File(filename)).toURL(), Preview_Area_Width, Preview_Area_Height);

                    this.add(moviePanel);
                    moviePanel.start();
                    movieIsPlaying = true;
                    return;                
                  }
              }          
          } catch (FileNotFoundException fe) {/*System.out.println("no JMF found");*/}
            catch (IOException ie) {}

          if (image.getIconHeight() < 0) { // if still cannot decode image
              System.out.println("This is not an image or format not supported by JCDSee");
              return;
          }
      }

      originalImage = image;
      int originalW,
          originalH;
      int imageW = image.getIconWidth();
      int imageH = image.getIconHeight();
      originalW = imageW;
      originalH = imageH;
      if (imageW > 0 && imageH > 0)
      {
        // enable View button
        parentFrame.setEnableViewButton(true);

        if ( parentFrame.getAutoSize() )
        {
            GetPreviewAreaSize(); // get the preview area size to be sure.
            if ( (imageW > 0 && imageH > 0) && (imageW > Preview_Area_Width || imageH > Preview_Area_Height) )
            {
                ScaleImage(imageW, imageH);
            }
        }
      } else {
          parentFrame.setEnableViewButton(false);
      }

      label = new JLabel(image);
      setViewportView(label);
      this.validate();
      // status bar
      String infoText;
      if (originalW > 0)
          infoText = filename.substring(filename.lastIndexOf(System.getProperty("file.separator")) + 1) + (" (" + originalW + "x" + originalH + ")");
      else
          infoText = filename.substring(filename.lastIndexOf(System.getProperty("file.separator")) + 1);
      parentFrame.statusBar.SetInfoText(infoText);
      //
      if (noImageYet)
          noImageYet = false;
    } // end DisplayImage

    // Display the About JCDSee information on Preview area
    public void displayAbout(){
      String html = "<html><p><b><font size='6'><font color='green'>JCDSee</font> <font color=bue>1.1</font></font></b>" +
                "<p><font color=black size='4'>www.jcdsee.com</font>" +
                "<p><font size='4'><font color=green>Release:</font> <font color=blue>9-Aug-2002</font></font>" +
//                "<p><font size='4'><font color=green>Variant:</font> <font color=blue>support JAI</font></font>" +
                "<p><font size='4'><font color=green>Author:</font> <font color=blue>Tri Tran</font></font>" +
                "<p>" +
                "<p><font size='4' color=green>Code contributor:</font>" +
                "<p><font size='4' color=blue>Neoya M</font>" +
                "<p><font size='4' color=blue>Matthias Burg - </font>" +
                "<font color=black size='4'>www.burgsoft.de</font>" +
                "<p><font size='4' color=blue>Philip Brown - </font></font>" +
                "<font color=black size='4'>www.bolthole.com</font>" +
                "<p>" +
                "<p><font size='3'><font color=green>Note:</font> <font color=blue>Name ordered by lastest code contribution</font></</html>\n";

      label = new JLabel(html, SwingConstants.CENTER);
      setViewportView(label);
      this.validate();
    }

    public void ScaleImage(int imageW, int imageH) {
      float imageScale = (float)imageW / imageH;
      if (imageH > imageW)
      {
        if (imageH > Preview_Area_Height)
          imageH = Preview_Area_Height;
        imageW = (int)(imageH * imageScale);
        if (imageW > Preview_Area_Width)
          imageW = Preview_Area_Width;
        imageH = (int)(imageW / imageScale);
      }
      else
      {
          if (imageW > Preview_Area_Width)
            imageW = Preview_Area_Width;
          imageH = (int) (imageW / imageScale);
          if (imageH > Preview_Area_Height)
          {
              imageH = Preview_Area_Height;
              imageW = (int)(imageH * imageScale);
          }
      }
      //Added by Neoya 2002-03-06
      if (image==null)
        image=new ImageIcon();
      switch(parentFrame.resize_quality) {
      case 0:
          image = new ImageIcon(image.getImage().getScaledInstance(
                          imageW, imageH, Image.SCALE_FAST));
          break;
      case 1:
          image = new ImageIcon(image.getImage().getScaledInstance(
                          imageW, imageH, Image.SCALE_DEFAULT));
          break;
      case 2:
          image = new ImageIcon(image.getImage().getScaledInstance(
                          imageW, imageH, Image.SCALE_SMOOTH));
          break;
      }
    }

    public void GetPreviewAreaSize() {
        // 20, 30 are the horizontal/vertical margin for image to locate within preview area
        Preview_Area_Width = this.getWidth() - 30;
        Preview_Area_Height = this.getHeight() - 20;
    }

    public ImageIcon Load_Image(String filename) {
        return parentFrame.Load_Image(filename);
    }

    public void ClearPreviewArea() {
        label = new JLabel();
        setViewportView(label);
        this.validate();
    }

    public void ResizePreview() {
        if (movieIsPlaying) {            
            return; // if movie is showing no need resize
        }
        if ( parentFrame.getAutoSize() && !noImageYet)
        {
            int imageW = originalImage.getIconWidth();
            int imageH = originalImage.getIconHeight();
            GetPreviewAreaSize(); // get the preview area size to be sure.

            if ( (imageW > 0 && imageH > 0) && (imageW > Preview_Area_Width || imageH > Preview_Area_Height) )
            {
                ScaleImage(imageW, imageH);
            } else {
                image = originalImage;
            }
        } // end if

        label = new JLabel(image);
        setViewportView(label);
        this.validate();
    }
}

⌨️ 快捷键说明

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