绘制点网格
我是图形编程的新手。 我正在尝试创建一个程序,允许您绘制有向图。 首先,我成功地绘制了一组矩形(代表节点),并通过重写 Java 中的 Paint 方法实现了平移和缩放功能。
在没有太多节点的情况下,这一切似乎都运行得相当好。 我的问题是在尝试绘制点网格时。 我首先使用了一些简单的测试代码,使用两个嵌套的 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);
}
}
这允许我平移网格但不能缩放。 然而平移时的表现却很糟糕! 我做了很多搜索,但我觉得我一定错过了一些明显的东西,因为我找不到关于这个主题的任何内容。
任何帮助或指示将不胜感激。
——史蒂芬
I'm new to graphics programming. I'm trying to create a program that allows you to draw directed graphs. For a start I have managed to draw a set of rectangles (representing the nodes) and have made pan and zoom capabilities by overriding the paint method in Java.
This all seems to work reasonably well while there aren't too many nodes. My problem is when it comes to trying to draw a dot grid. I used a simple bit of test code at first that overlayed a dot grid using two nested for loops:
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);
}
}
This allows me to pan the grid but not zoom. However, the performance when panning is terrible! I've done a lot of searching but I feel that I must be missing something obvious because I can't find anything on the subject.
Any help or pointers would be greatly appreciated.
--Stephen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 BufferedImage 作为点网格。 初始化一次,以后只绘制图像,而不是一遍又一遍地绘制网格。
使用此方法可以轻松实现缩放:
绘制到缩放的图形中将导致线宽成比例增大等。
Use a BufferedImage for the dot grid. Initialize it once and later only paint the image instead of drawing the grid over and over.
And zooming is easily achieved using this:
Drawing into the zoomed Graphics will lead to proportionally larger line width, etc.
我认为每次鼠标移动时重新绘制所有点会给您带来性能问题。 也许您应该考虑将视图快照作为位图并进行平移,当用户释放鼠标按钮时“正确”重绘视图?
I think re-drawing all your dots every time the mouse moves is going to give you performance problems. Perhaps you should look into taking a snapshot of the view as a bitmap and panning that around, redrawing the view 'properly' when the user releases the mouse button?