如何在 jPanel 上绘制不会被重新绘制的内容?

发布于 2024-07-08 06:40:58 字数 78 浏览 5 评论 0原文

我怎样才能在 JPanel 中绘制一些保持不变而不被重新绘制的东西,我正在做一个交通模拟程序,我希望道路被绘制一次,因为它不会改变。 谢谢

How can I draw something in JPanel that will stay the same and not be repainted, I am doing a traffic simulation program and I want the road to be drawn once because It will not change.
Thanks

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

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

发布评论

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

评论(4

森林迷了鹿 2024-07-15 06:40:58

我不确定您是否真的希望您的道路永远不会被重新绘制 - 例如,当您的窗口大小调整时,或者当它在另一个窗口阻挡它之后变得可见时,重新绘制事件会触发。 如果你的面板从不重新粉刷,那么它看起来会很奇怪。

据我记得,Swing 只会在这些情况下触发适当的绘制事件,因此您应该可以遵循使用合适的覆盖子类化 JPanel 的常用方法:

public class RoadPanel extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // your drawing code here
    }
}

如果您将道路缓存为图像或其他图形格式(以节省计算)多次显示数据)一旦绘制一次,这可能会为您节省后续绘制的时间。

I'm not sure you actually want your road to never be repainted - repaint events fire (for example) when your window is resized, or when it becomes visible following another window obstructing it. If your panel never repaints then it'll look peculiar.

As far as I remember, Swing will only fire appropriate paint events for these circumstances, so you should be OK following the usual method of subclassing JPanel with a suitable override:

public class RoadPanel extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // your drawing code here
    }
}

If you cache your road into an image or another graphics format (to save calculating the display data multiple times) once drawn once, this might save you some time on subsequent paints.

以为你会在 2024-07-15 06:40:58

据我所知,不会,除非有透明覆盖层的技巧。

我看到(并且确实)的大多数图形应用程序只是在每次重绘时重新绘制整个面板。 现在,您可以在图形缓冲区中执行此操作一次,然后通过将图形缓冲区复制到 JPanel 来快速绘制整个背景。 它应该比调用所有图形基元来绘制道路更快。

或者,像某些 2D 游戏那样,也许绘制一次并更新移动部分,例如精灵:它需要擦除精灵使用的旧位置(恢复那里的背景)并在新位置重新绘制精灵。 因此,您在图形缓冲区中仍然有道路的副本,但不是每次都重新绘制整个道路,而是仅更新一些小部分。 可以稍微快一点。

To my knowledge, no, unless there is a trick with transparent overlays.

Most graphical applications I saw (and did) just re-draw the whole panel on each repaint. Now, you can do that once, in a graphic buffer, and then just paint the whole background at once, quickly, by copying the graphic buffer to the JPanel. It should be faster than calling all graphical primitives to draw the road.

Or, the way some 2D games do, perhaps paint it once and update the moving parts, like sprites: it needs to erase the old place used by the sprites (restore the background there) and re-draw the sprites at the new place. So you still have a copy of the road in a graphic buffer but instead of re-drawing it whole each time, you update only some small parts. Can be slightly faster.

握住你手 2024-07-15 06:40:58

每次面板被遮挡时(即框架最小化/另一个窗口放在顶部),组件都需要重新绘制。 因此,只绘制一次的东西不会达到你想要的效果。 为了使不改变的部分更有效地绘制,您可以将它们绘制到“缓冲区”图像一次,然后每次需要重新绘制面板或组件时绘制此缓冲区。

// Field that stores the image so it is always accessible
private Image roadImage = null;
// ...
// ...
// Override paintComponent Method
public void paintComponent(Graphics g){

  if (roadImage == null) {
      // Create the road image if it doesn't exist
      roadImage = createImage(width, height);
      // draw the roads to the image
      Graphics roadG = roadImage.getGraphics();
      // Use roadG like you would any other graphics
      // object to draw the roads to an image
  } else {
      // If the buffer image exists, you just need to draw it.
      // Draw the road buffer image
      g.drawImage(roadImage, 0, 0, null);
  }
  // Draw everything else ...
  // g.draw...
}

The component will need to be repainted every time that the panel is obscured (ie frame minimized/another window put on top). Therefore drawing something only once will not work as you want it to. To make parts that do not change be drawn more efficiently you can draw them once to a 'buffer' image, and then just draw this buffer each time that the panel or component needs to be redrawn.

// Field that stores the image so it is always accessible
private Image roadImage = null;
// ...
// ...
// Override paintComponent Method
public void paintComponent(Graphics g){

  if (roadImage == null) {
      // Create the road image if it doesn't exist
      roadImage = createImage(width, height);
      // draw the roads to the image
      Graphics roadG = roadImage.getGraphics();
      // Use roadG like you would any other graphics
      // object to draw the roads to an image
  } else {
      // If the buffer image exists, you just need to draw it.
      // Draw the road buffer image
      g.drawImage(roadImage, 0, 0, null);
  }
  // Draw everything else ...
  // g.draw...
}
小姐丶请自重 2024-07-15 06:40:58

我所做的是设置一个布尔值来确定某个部分是否需要重绘。 然后,在 paintComponent() 方法中,我可以检查该值并重绘某些东西,或者不重绘。

protected void paintComponent(Graphics g){
    super.paintComponent(g);
    if (drawRoad) {
        drawRoadMethod(g);
    }
    drawTheRest(g);
}

有点像那样。

What I do is set a boolean value to whether or not a certain part needs to be redrawn. Then, in the paintComponent() method I can check the value and redraw the certain thing, or not.

protected void paintComponent(Graphics g){
    super.paintComponent(g);
    if (drawRoad) {
        drawRoadMethod(g);
    }
    drawTheRest(g);
}

Kinda like that.

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