绘制点网格

我是图形编程的新手。 我正在尝试创建一个允许您绘制有向图的程序。 首先,我设法绘制了一组矩形(代表节点),并通过覆盖Java中的paint方法来实现平移和缩放function。

这一切似乎都运行得相当好,而没有太多的节点。 我的问题是在尝试绘制点网格时。 我首先使用了一小段测试代码,使用两个嵌套的for循环覆盖了一个点网格:

int iPanX = (int) panX; int iPanY = (int) panY; int a = this.figure.getWidth() - iPanX; int b = this.figure.getHeight() - (int) iPanY; for (int i = -iPanX; i < a; i += 10) { for (int j = -iPanY; j < b; j += 10) { g.drawLine(i, j, i, j); } } 

这允许我平移网格而不是缩放。 但是,平移时的表现太可怕了! 我做了很多搜索,但我觉得我必须遗漏一些明显的东西,因为我找不到任何关于这个问题的东西。

任何帮助或指示将不胜感激。

–Stephen

对点网格使用BufferedImage。 初始化它一次,之后只绘制图像而不是一遍又一遍地绘制网格。

 private init(){ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics g = image.getGraphics(); // then draw your grid into g } public void paint(Graphics g) { g.drawImage(image, 0, 0, null); // then draw the graphs } 

使用此function可轻松实现缩放:

 g.drawImage(image, 0, 0, null); // so you paint the grid at a 1:1 resolution Graphics2D g2 = (Graphics2D) g; g2.scale(zoom, zoom); // then draw the rest into g2 instead of g 

绘制到缩放的图形将导致比例更大的线宽等。

我认为每次鼠标移动时重新绘制所有点都会给你带来性能问题。 也许您应该考虑将视图的快照作为位图并平移,在用户释放鼠标按钮时“正确”重新绘制视图?