2012-01-16 80 views
2

與問題Resizing a component without repainting連接是我的問題是如何在表單中創建resiziable定製的Graphics2D調整大小的Graphics2D到JScrollPane的

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class ZoomWithSelectionInViewport implements MouseWheelListener { 

    private JComponent b; 
    private int hexSize = 3; 
    private int zoom = 80; 
    private JScrollPane view; 

    public ZoomWithSelectionInViewport() throws Exception { 
     b = new JComponent() { 

      private static final long serialVersionUID = 1L; 

      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(700, 700); 
      } 

      @Override 
      protected void paintComponent(Graphics g) { 
       super.paintComponent(g); 
       Graphics2D g2d = ((Graphics2D) g); 
       int vertOffsetX, vertOffsetY, horizOffsetX, horizOffsetY; 
       vertOffsetX = (int) ((double) hexSize * Math.sqrt(3.0f)); 
       vertOffsetY = (int) ((double) -hexSize - 1 * Math.sqrt(3.0f)/2.0f); 
       horizOffsetX = (int) ((double) hexSize * Math.sqrt(3.0f)); 
       horizOffsetY = (int) ((double) hexSize + 1 * Math.sqrt(3.0f)/2.0f); 
       for (int x = 0; x < 50; x++) { 
        for (int y = 0; y < 50; y++) { 
         int[] xcoords = new int[6]; 
         int[] ycoords = new int[6]; 
         for (int i = 0; i < 6; i++) { 
          xcoords[i] = (int) ((hexSize + x * horizOffsetX + y * vertOffsetX) 
            + (double) hexSize * Math.cos(i * 2 * Math.PI/6)); 
          ycoords[i] = (int) (((getSize().height/2) + x * horizOffsetY 
            + y * vertOffsetY) + (double) hexSize * Math.sin(i * 2 * Math.PI/6)); 
         } 
         g2d.setStroke(new BasicStroke(hexSize/2.5f)); 
         g2d.setColor(Color.GRAY); 
         g2d.drawPolygon(xcoords, ycoords, 6); 
        } 
       } 
      } 
     }; 
     view = new JScrollPane(b); 
     b.addMouseWheelListener(this); 
     JFrame f = new JFrame(); 
     f.setLocation(10, 10); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.add(view); 
     f.setPreferredSize(b.getPreferredSize()); 
     f.pack(); 
     f.setVisible(true); 
    } 

    public void mouseWheelMoved(MouseWheelEvent e) { 
     zoom = 100 * -Integer.signum(e.getWheelRotation()); 
     if (hexSize - Integer.signum(e.getWheelRotation()) > 0) { 
      hexSize -= Integer.signum(e.getWheelRotation()); 
     } 
     Dimension targetSize = new Dimension(b.getWidth() + zoom, b.getHeight() + zoom); 
     b.setPreferredSize(targetSize); 
     b.setSize(targetSize); 
     b.revalidate(); 
     b.repaint(); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        ZoomWithSelectionInViewport example = new ZoomWithSelectionInViewport(); 
       } catch (Exception ex) { 
        // 
       } 
      } 
     }); 
    } 
} 

回答

2

如果我理解正確的,你要滾動窗格的滾動條,以反映當前的變焦狀態。我看到兩種選擇:

  • 做在組件未倍率getPreferredSize(),並調整在小鼠聽者的優選大小爲包括在縮放的圖像;它看起來在右邊略有截斷。

  • 執行倍率getPreferredSize()在組件,並調節返回Dimension(現在是常數),以包括在縮放邊界paintComponent()隱式的。

我更喜歡後者。我還發現寫明確的轉換函數來轉換縮放和未縮放的座標很有幫助,如here所示。顯示爲here的逆AffineTransform也是可能的。

+0

謝謝,因爲我真的不明白什麼,怎麼樣,爲什麼可能,因爲啄木鳥到木頭 – mKorbel 2012-01-16 17:33:35

相關問題