绘制点网格 [英] Drawing a dot grid
问题描述
我是新来的图形编程。我试图创建一个程序,让您可以绘制有向图。一开始我已成功地绘制一组矩形(重presenting的节点),并通过覆盖paint方法在Java中做平移和缩放功能。
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.
这一切似乎进行得相当顺利,而不会有太多的节点。我的问题是,当涉及到试图绘制一个点网格。我用测试code一个简单的位首先是重叠使用两个嵌套的for循环点网格:
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.
任何帮助或指针将不胜AP preciated。
Any help or pointers would be greatly appreciated.
斯蒂芬 -
推荐答案
使用的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.
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
}
和缩放是很容易实现使用此:
And zooming is easily achieved using this:
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
绘制成图形缩放会导致更大的比例线条宽度等。
Drawing into the zoomed Graphics will lead to proportionally larger line width, etc.
这篇关于绘制点网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!