2017-09-22 10 views
1

私はグラフ・ペーパーのようなセルを作成するためにグリッド・バッグ・レイアウトを使用するJavaのプログラムを持っています。マウスがセルの1つ上にある場合、そのセルはその色を変更します。マウスのスクロールホイールを使ってズーム機能を実装したいと思います。また、ズームインしたときに、ズームしたキャンバスをクリックしてドラッグすることでパンを動かすことができる機能を追加したいと思います。パンニングを指定するには、クリックしてドラッグしてGoogleマップなどのズームインした地図を移動することを考えてください。ズーム機能とパン機能をJPanelに追加する方法は?

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.Border; 
import javax.swing.border.MatteBorder; 

public class Testing { 

    public int RowI = 10; 
    public int ColI = 10; 

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

    public Testing() { 
     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 TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     public TestPane() { 
      setLayout(new GridBagLayout()); 

      GridBagConstraints gbc = new GridBagConstraints(); 
      for (int row = 0; row < RowI; row++) { 
       for (int col = 0; col < ColI; col++) { 
        gbc.gridx = col; 
        gbc.gridy = row; 

        CellPane cellPane = new CellPane(); 
        Border border = null; 
        if (row < 4) { 
         if (col < 4) { 
          border = new MatteBorder(1, 1, 0, 0, Color.GRAY); 
         } else { 
          border = new MatteBorder(1, 1, 0, 1, Color.GRAY); 
         } 
        } else { 
         if (col < 4) { 
          border = new MatteBorder(1, 1, 1, 0, Color.GRAY); 
         } else { 
          border = new MatteBorder(1, 1, 1, 1, Color.GRAY); 
         } 
        } 
        cellPane.setBorder(border); 
        add(cellPane, gbc); 
       } 
      } 
     } 
    } 

    public class CellPane extends JPanel { //The CellPane class changes the color of an individual cell based on whether or no the mouse I on a cell. 

     private Color defaultBackground; //This is a private color that is only used by the mouseListener. 

     public CellPane() { 
      addMouseListener(new MouseAdapter() { 
       @Override 
       public void mouseEntered(MouseEvent e) { //If mouse is on cell turn cell the given color(in this case green). 
        defaultBackground = getBackground(); 
        setBackground(Color.GREEN); 
       } 

       @Override 
       public void mouseExited(MouseEvent e) { //If mouse is not on cell revert to default background. 
        setBackground(defaultBackground); 
       } 
      }); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(50, 50); //Cell size on x and y axis. 
     } 
    } 
} 

enter image description here

+1

私が考えることができるより簡単な解決策の中には、あなたの例に基づいて、単に「CellPane」を「スケール」することによってコンポーネントの「preferredSize」を変更することがあります。 JScollPaneを使って基本的にあなたの必要条件を満たしているカップル – MadProgrammer

+0

調べられたアプローチ[here](https://stackoverflow.com/q/11739989/230513)も参照してください。 – trashgod

答えて

0

たぶんthis questionは、あなたが探しているものをお答えします。

+0

これは代わりにコメントにする必要があります... – Frakcool

関連する問題