简体   繁体   English

如何在不会​​重新绘制的jPanel上绘制某些内容?

[英]How can I draw something on a jPanel that will not be repainted?

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. 我如何在JPanel中绘制将保持不变且不会重新粉刷的东西,我正在执行交通模拟程序,并且希望绘制一次道路,因为它不会改变。 Thanks 谢谢

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. 现在,您可以在图形缓冲区中执行一次该操作,然后只需将图形缓冲区复制到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. 或者,某些2D游戏的方式可能是绘制一次并更新活动内容,例如sprites:它需要擦除sprite使用的旧位置(在其中恢复背景)并在新位置重新绘制sprite。 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. 可以稍微快一点。

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: 据我所知,Swing只会在这些情况下触发适当的绘制事件,因此您应该遵循使用适当的重写对JPanel进行子类化的常规方法:

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. 如果将道路绘制成一次后将其缓存为图像或其他图形格式(以节省多次计算显示数据的次数),则可以节省后续油漆的时间。

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...
}

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. 然后,在paintComponent()方法中,我可以检查该值并重新绘制某些东西。

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

Kinda like that. 有点像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM