将Graphics2d调整为JScrollPane

时间:2012-01-16 12:33:25

标签: java swing jscrollpane graphics2d

关于问题Resizing a component without repainting,我的问题是如何以表格形式创建可定制的自定义Graphics2d

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) {
                    //
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:2)

如果我理解正确,您希望滚动窗格的滚动条反映当前的缩放状态。我看到两种选择:

  • 不要覆盖组件中的getPreferredSize(),并调整鼠标侦听器中的首选大小以包含缩放的图像;它似乎在右侧略微截断。

  • 覆盖组件中的getPreferredSize(),并调整返回的Dimension(现在为常量)以包含paintComponent()中隐含的缩放边界

我更喜欢后者。我还发现编写显式转换函数来转换缩放和非缩放坐标很有帮助,如here所示。显示here的反AffineTransform也是可能的。