Java 2D 图形矩形

发布于 2024-08-12 03:34:16 字数 469 浏览 6 评论 0原文

我正在尝试制作一个 Java Applet,它允许我在画布中绘制图形数据结构。我将通过单击要创建节点的位置,然后单击节点来连接它们来完成此操作。问题是我无法让 paint() 方法正确运行。我使用 mousePressed(MouseEvent e) 方法将新节点添加到图形(以及画布上的正方形)中,

Graphics g = this.getGraphics();
g.setColor(Color.blue);
g.fillRect(e.getX(), e.gety(), 40, 40);

一切正常,直到我调整窗口大小,然后所有填充的矩形消失。我将 Paint 方法重写为空方法,但同样的事情仍然发生。我无法在 Paint() 中添加 fillRect 命令,因为在用户使用鼠标与其交互之前我不知道存在哪些矩形。

如何在鼠标侦听器方法中使用 g.fillRect() 并使它们粘住?

I'm attempting to make a Java Applet that will allow me to draw a graph data structure in a canvas. I will do this by clicking where I want to create nodes, and clicking the nodes to connect them. The problem is I cannot get the paint() method to behave correctly. I add new nodes to the graph (and squares on the canvas) inside the mousePressed(MouseEvent e) method using,

Graphics g = this.getGraphics();
g.setColor(Color.blue);
g.fillRect(e.getX(), e.gety(), 40, 40);

Everything works fine, until I resize the window, and then all the filled rectangles vanish. I overrided the paint method to just an empty method, but the same thing still happens. I can't add the fillRect commands inside paint() because I don't know what rectangles exist until the user interacts with it using the mouse.

How can I use g.fillRect() inside the mouse listener methods and make them stick?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

勿忘初心 2024-08-19 03:34:16

问题是你所画的地方并不持久。任何时候,你都可能失去你所吸引的一切。发生这种情况时,将调用 Paint(Graphics) 方法。每次发生这种情况时,您要么需要重新绘制整个图片,要么需要留出一块画布来绘制,并根据需要将内容复制到小程序的图形中。

以下是创建和绘制图像的方法:
http://java.sun.com/docs/books/tutorial/2d/images/drawonimage.html< /a>

然后,在您的绘制方法中,使用您的 Graphics 的 drawImage(...) 方法来显示您创建的图像。

The problem is the place you're drawing to isn't persistant. At any moment, you can lose everything you've drawn to it. The paint(Graphics) method is called when this happens. You'll either need to repaint the entire picture every time this happens, or you'll need to set aside a canvas to draw to and copy the contents to your applet's Graphics as needed.

Here's how to create and draw to an image:
http://java.sun.com/docs/books/tutorial/2d/images/drawonimage.html

Then, in your paint method, use your Graphics' drawImage(...) method to display the image you've created.

灯下孤影 2024-08-19 03:34:16

我不知道我是否正确地阅读了这篇文章,但为什么不将最后一次单击的位置存储在一个变量中,以便稍后在调用 Paint() 方法时绘制呢?

I don't know if I'm reading this correctly, but why not just store the location of the last click in a variable to be painted later, when the paint() method is called?

萌︼了一个春 2024-08-19 03:34:16

您必须覆盖窗口调整大小操作侦听器并调用 repaint在里面。

You've got to override the window resize action listener and call repaint inside of it.

夏了南城 2024-08-19 03:34:16

图形是临时的。当一个区域变脏时,它将被重新粉刷。

最好的方法是创建一个 BufferedImage,在 mousePressed 上对其进行绘制并调用 repaint。
当调用paint时,将图像绘制到传递的图形对象上。这样您就不需要存储矩形,并且获得了一个可以提高性能的缓冲区。

The Graphics is temporary. When a region gets dirty, it will be repainted.

The best way is to create a BufferedImage, paint to it on mousePressed and call repaint.
When paint is called, draw the Image onto the passed Graphics Object. This way you don't need to store the Rectangles and you got a buffer which will improve performance.

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