2013-05-02 204 views
2

我必須將Zoom In and Zoom Out功能添加到JPanel,其中包含JLabelImageIcon等組件。Jpanel中的放大和縮小功能

我想要Zoom JPanel及其組件適當 我試着用下面的代碼片段,但它沒有正常工作。

JPanel它有null佈局和本身放置在Applet內。 我不確定爲什麼它不起作用,或者是因爲Applet或別的什麼!

cPanel是我JPanel它包含了放大Button點擊JLabel

下面的代碼片段, 它顯示了在上按一下按鈕閃爍屏幕,原來

Graphics g = cPanel.getGraphics(); 
Graphics2D g2d = (Graphics2D) g; 
AffineTransform savedXForm = g2d.getTransform(); 
g2d.scale(1.0, 1.0); 
g2d.setColor(Color.red); 
super.paint(g); 
g2d.setTransform(savedXForm); 
cPanel.validate(); 
cPanel.repaint(); 
+2

*「我想放大和放大」*我想要問題很容易閱讀。請停止「嘟'」在句子的開頭添加大寫字母。 – 2013-05-02 07:06:11

+1

我會看看你的佈局問題,然後我會看看'JXLayer' – MadProgrammer 2013-05-02 07:11:35

+1

請參閱*** [this](http://stackoverflow.com/questions/2944442/zoom-in-java-swing - 應用程序?rq = 1)***後 – 2013-05-02 07:15:04

回答

0

你可以看看這個鏈接: http://blog.sodhanalibrary.com/2015/04/zoom-in-and-zoom-out-image-using-mouse_9.html#.Vz6iG-QXV0w

要引用的源代碼(這裏已經使用MouseWh完成了eelListener):

import java.awt.EventQueue; 
import javax.imageio.ImageIO; 
import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JScrollPane; 
import java.awt.BorderLayout; 
import javax.swing.JPanel; 
import com.mortennobel.imagescaling.ResampleOp; 
import java.awt.event.MouseWheelListener; 
import java.awt.event.MouseWheelEvent; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 

public class ImageZoom { 

    private JFrame frmImageZoomIn; 
    private static final String inputImage = "C:\\my-pfl-pic.jpg"; // give image path here 
    private JLabel label = null; 
    private double zoom = 1.0; // zoom factor 
    private BufferedImage image = null; 
    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        ImageZoom window = new ImageZoom(); 
        window.frmImageZoomIn.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    * @throws IOException 
    */ 
    public ImageZoom() throws IOException { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    * @throws IOException 
    */ 
    private void initialize() throws IOException { 
     frmImageZoomIn = new JFrame(); 
     frmImageZoomIn.setTitle("Image Zoom In and Zoom Out"); 
     frmImageZoomIn.setBounds(100, 100, 450, 300); 
     frmImageZoomIn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JScrollPane scrollPane = new JScrollPane(); 
     frmImageZoomIn.getContentPane().add(scrollPane, BorderLayout.CENTER); 

     image = ImageIO.read(new File(inputImage)); 

     JPanel panel = new JPanel(); 
     panel.setLayout(new BorderLayout()); 

     // display image as icon 
     Icon imageIcon = new ImageIcon(inputImage); 
     label = new JLabel(imageIcon); 
     panel.add(label, BorderLayout.CENTER); 

     panel.addMouseWheelListener(new MouseWheelListener() { 
      public void mouseWheelMoved(MouseWheelEvent e) { 
       int notches = e.getWheelRotation(); 
       double temp = zoom - (notches * 0.2); 
       // minimum zoom factor is 1.0 
       temp = Math.max(temp, 1.0); 
       if (temp != zoom) { 
        zoom = temp; 
        resizeImage(); 
       } 
      } 
     }); 
     scrollPane.setViewportView(panel); 
    } 

    public void resizeImage() { 
      System.out.println(zoom); 
      ResampleOp resampleOp = new ResampleOp((int)(image.getWidth()*zoom), (int)(image.getHeight()*zoom)); 
       BufferedImage resizedIcon = resampleOp.filter(image, null); 
      Icon imageIcon = new ImageIcon(resizedIcon); 
      label.setIcon(imageIcon); 
     } 


}