简体   繁体   English

Jpanel缩放就像画图程序一样

[英]Jpanel Zooming like in a paint program

I've got a problem over a zoom feature on a program. 我在程序的缩放功能上遇到问题。 Here's the deal, I got a canvas which extends a jpanel and has a grid drawn on it. 这是交易,我得到了一个画布,该画布扩展了jpanel并在其上绘制了网格。 My problem is how to implement a zoom function. 我的问题是如何实现缩放功能。 By zooming on the jpanel each small squares on the grid will divided into 3x3. 通过缩放jpanel,网格上的每个小方块将分为3x3。

I've tried doing a 3 panel zoom, 3 panels with 3 different sizes with different division on the grid but it takes up a lot of memory and run time. 我尝试进行3面板缩放,3面板大小不同的3面板,并在网格上进行不同划分,但是这会占用大量内存和运行时间。 I'm implementing only 1 jpanel with fix size and will zoom on it by using Graphics2d.scale(...) but seems to have an offset on the mouse coordinates and the jpanel's. 我只实现了固定大小的1个jpanel,并会使用Graphics2d.scale(...)对其进行缩放,但在鼠标坐标和jpanel上似乎有偏移。 I know it could be fix but my other concern is the jpanel is implemented with jscrollpane. 我知道它可以解决,但我的另一个担心是使用jscrollpane实现了jpanel。 Will fixing the coordinate offset will solve this issue, i haven't tried because i don't know how. 将修复坐标偏移将解决此问题,我没有尝试过,因为我不知道如何。

paint in the jpanel drawing the grid 在jpanel中绘制绘制网格

public void paint(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    int x=0;int y=0;
    g2.setColor(Color.GRAY);
    for(int i=1; i<this.divisions; i++){
        x = i *divSize;
        g2.drawLine(x, 0, x, this.size);
    }
    for(int i=1; i<this.divisions; i++){
        y = i *divSize;
        g2.drawLine(0, y, this.size, y);
    }
            /*code for filling the small squares with colors*/

}

You can draw all your content on a BufferedImage. 您可以在BufferedImage上绘制所有内容。 Graphics of the image can be scaled to achieve desired zoom effect. 可以缩放图像的图形以实现所需的缩放效果。

But anyway if you use JScrollPane with panel inside the panel's size (preferred size) must be adapted to show scrollbars when it's needed. 但是无论如何,如果您在面板尺寸(首选尺寸)内使用面板时使用JScrollPane,则必须对其进行调整以在需要时显示滚动条。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM