使用鼠标和图形缩放

Posted

技术标签:

【中文标题】使用鼠标和图形缩放【英文标题】:zoom using mouse and graphics 【发布时间】:2013-11-07 17:55:57 【问题描述】:

我用 Graphics G(不是 2D)在我的 JComponent 中绘制了一些曲线等。

现在我想使用鼠标的滚轮来放大和缩小。

任何曲目?

我听说过 BufferredImage ?

【问题讨论】:

有很多注意事项。即使您将基本内容渲染为 BufferedImage,您仍然需要修改组件的首选大小,以便正确布局。查看仿射变换 【参考方案1】:

我放了这个简单的代码来向你展示如何通过向 JPanel 添加 MouseWheelListener 来使用鼠标滚轮:

myJpanel.addMouseWheelListener(new MouseWheelListener()                
   @Override
   public void mouseWheelMoved(MouseWheelEvent mwe) 
      jPanelMouseWheelMoved(mwe);
   
);

实现鼠标滚轮监听器:

private void jPaneMouseWheelMoved(MouseWheelEvent mwe) 
    if(Event.ALT_MASK != 0) 
        if(mwe.getWheelRotation() > 0) 
            //here you put your code to scrool douwn or to minimize. 
            System.out.println(" minimize by "+(-1*mwe.getWheelRotation()));             
        
        else if(mwe.getWheelRotation() < 0) 
            //here you put your code to scrool up or to maximize.
            System.out.println(" maximaze by "+(-1*mwe.getWheelRotation()));
                   
            

您可以调整此示例以缩放或滚动您想要的内容。

【讨论】:

【参考方案2】:

您需要考虑一些注意事项...

最终结果将取决于您想要实现的目标。如果您使用 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();
            
        
    


您还可以缩放直接传递给您的paintComponent 方法的Graphics 上下文。

这里重要的是记得在完成后重置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了解更多详情

【讨论】:

@Sage 怎么样?您仍然需要完成所有工作,所以为什么要进一步复杂化。如果我愿意,我也可以将RenderingHints 应用于输出 - 这只是我自己;) @MadProgrammer,可能跑题了,但仍然提到了RenderingHints:在使用缩放工作 1-2 个月前,我发现设置 RenderingHints: BICUBICBILINEAR对于缩小图像,与其他原生图像处理应用程序相比,质量较差,至少如果图像包含Text。我尝试了ANTIALIASING 提示,但有一点改进。 @Sage 文本缩放很痛苦。很多还取决于你是在做直接缩放算法还是阶梯缩放算法,for example【参考方案3】:

试试JFreeChart; setMouseWheelEnabled() 方法,用于控制 ChartPanel 的缩放,在引用的示例 here 中进行了说明。

【讨论】:

以上是关于使用鼠标和图形缩放的主要内容,如果未能解决你的问题,请参考以下文章

Java AWT 图形界面编程使用鼠标滚轮放大缩小 Canvas 画布 ( 鼠标滚轮事件监听器 MouseWheelListener )

QT 图形场景/视图 - 用鼠标移动

PCB Genesis 鼠标滚轮缩放与TGZ拖放 插件实现

VC:如何实现窗口和窗口内容在鼠标拖动下改变大小

从鼠标位置缩放和翻译图像

opengl中如何实现图象的缩放