Java - 如何扩展必要的区域

时间:2015-03-14 21:04:52

标签: java swing zoom paintcomponent mousewheel

我使用简单的grapher,我想在鼠标滚轮上缩放红色区域。我添加MouseWheelListener

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

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    AffineTransform at = new AffineTransform();
    at.scale(scale, scale);
    g2.setTransform(at);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // read points in series and add series in dataset...
    // draw white background
    g2.setColor(Color.WHITE);
    g2.fillRect(padding + labelPadding, padding, getWidth() - (2 * padding) - labelPadding, getHeight() - 2 * padding - labelPadding);

    // create hatch marks and grid lines for x and y axis...
    // create x and y axes 
    g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, padding + labelPadding, padding);
    g2.drawLine(padding + labelPadding, getHeight() - padding - labelPadding, getWidth() - padding, getHeight() - padding - labelPadding);
    //draw graphs...
}

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

private static void createAndShowGui() {
    ...
    SimpleGrapher2 mainPanel = new SimpleGrapher2(scores);
    mainPanel.setPreferredSize(new Dimension(width, height));
    JFrame frame = new JFrame("DrawGraph");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(mainPanel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        createAndShowGui();
     }
  });

} }

那么如何只缩放我的红色区域而不是所有程序窗口?

enter image description here

how my code works

0 个答案:

没有答案