当前位置: 动力学知识库 > 问答 > 编程问答 >

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: