简体   繁体   中英

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. My problem is how to implement a zoom function. By zooming on the jpanel each small squares on the grid will divided into 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. 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. I know it could be fix but my other concern is the jpanel is implemented with jscrollpane. 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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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