绘制点网格

发布于 2024-07-30 11:41:49 字数 607 浏览 3 评论 0原文

我是图形编程的新手。 我正在尝试创建一个程序,允许您绘制有向图。 首先,我成功地绘制了一组矩形(代表节点),并通过重写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉鱼一梦 2024-08-06 11:41:49

使用 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
}

使用此方法可以轻松实现缩放:

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

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

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.

习ぎ惯性依靠 2024-08-06 11:41:49

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文