2013-10-28 83 views
3

我使用Graphics G(不是2D)在我的JComponent中繪製了一些曲線等。使用鼠標和圖形縮放

現在我想用我的鼠標滾輪放大和縮小。

任何曲目?

我聽說過一個BuferredImage?

+0

有許多考慮因素。即使您將基本內容渲染到BufferedImage,仍然需要修改組件的首選大小,以便它可以正確佈置。退房AffineTransform – MadProgrammer

回答

3

嘗試JFreeChart;在引用here的示例中說明了用於控制放大的方法setMouseWheelEnabled()

11

您需要考慮幾個因素......

最終結果將取決於您想實現的目標。如果使用Graphics2D API繪製曲線,每次渲染組件時簡單地縮放座標可能會更簡單。您需要確保尺寸的任何更改都反映在組件本身的首選尺寸中。

例如,您也可以將「默認」輸出呈現爲BufferedImage,並簡單地使用AffineTransform來更改用於呈現結果的比例。

這個簡單的使用BufferedImage並從磁盤加載圖片,但基本概念是相同的。

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseWheelEvent; 
import java.awt.geom.AffineTransform; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class ZoomPane { 

    public static void main(String[] args) { 
     new ZoomPane(); 
    } 

    public ZoomPane() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new JScrollPane(new TestPane())); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private BufferedImage img; 
     private float scale = 1; 

     public TestPane() { 
      try { 
       img = ImageIO.read(new File("/path/to/image")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
      addMouseWheelListener(new MouseAdapter() { 

       @Override 
       public void mouseWheelMoved(MouseWheelEvent e) { 
        double delta = 0.05f * e.getPreciseWheelRotation(); 
        scale += delta; 
        revalidate(); 
        repaint(); 
       } 

      }); 
     } 

     @Override 
     public Dimension getPreferredSize() {    
      Dimension size = new Dimension(200, 200); 
      if (img != null) {    
       size.width = Math.round(img.getWidth() * scale); 
       size.height = Math.round(img.getHeight() * scale);     
      }   
      return size; 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (img != null) { 
       Graphics2D g2d = (Graphics2D) g.create(); 
       AffineTransform at = new AffineTransform(); 
       at.scale(scale, scale); 
       g2d.drawImage(img, at, this); 
       g2d.dispose(); 
      } 
     } 
    } 

} 

你也可以縮放Graphics環境傳送到您的paintComponent方法直接。

這裏最重要的是要記住,你已經完成後重置AffineTransform,否則將被傳遞到時,他們呈現的其他組件,這將不會產生預期的輸出...

這個例子基本上創建Graphics背景下,我們可以操縱和不影響原處分的副本,使它更易於惹

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseWheelEvent; 
import java.awt.geom.AffineTransform; 
import java.awt.geom.GeneralPath; 
import java.awt.geom.Path2D; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class ZoomPane { 

    public static void main(String[] args) { 
     new ZoomPane(); 
    } 

    public ZoomPane() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new JScrollPane(new TestPane())); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private float scale = 1; 

     public TestPane() { 
      addMouseWheelListener(new MouseAdapter() { 

       @Override 
       public void mouseWheelMoved(MouseWheelEvent e) { 
        double delta = 0.05f * e.getPreciseWheelRotation(); 
        scale += delta; 
        revalidate(); 
        repaint(); 
       } 

      }); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      Dimension size = new Dimension(200, 200); 
      size.width = Math.round(size.width * scale); 
      size.height = Math.round(size.height * scale); 
      return size; 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      AffineTransform at = new AffineTransform(); 
      at.scale(scale, scale); 
      g2d.setTransform(at); 

      g2d.setColor(Color.RED); 

      // This is for demonstration purposes only 
      // I prefer to use getWidth and getHeight 
      int width = 200; 
      int height = 200; 

      Path2D.Float path = new Path2D.Float(); 
      int seg = width/3; 
      path.moveTo(0, height/2); 
      path.curveTo(0, 0, seg, 0, seg, height/2); 
      path.curveTo(
        seg, height, 
        seg * 2, height, 
        seg * 2, height/2); 
      path.curveTo(
        seg * 2, 0, 
        seg * 3, 0, 
        seg * 3, height/2); 

      g2d.draw(path); 


      g2d.dispose(); 
     } 
    } 
} 

看看Transforming Shapes, Text and Images更多細節

+0

非常好!另見['SineTest'](http://stackoverflow.com/a/9373195/230513)。 – trashgod

+0

@Sage如何?你仍然需要做所有的工作,所以爲什麼它會更復雜。如果我願意,我也可以將'RenderingHints'應用於輸出 - 這只是我的;) – MadProgrammer

+0

@MadProgrammer,呃!我錯過了這一點。忘了它:) +1 – Sage

相關問題