How can I draw something on jPanel that won't be repainted?

How can I draw something in the JPanel that stays the same and won't be repainted, I am making a traffic simulation program and I want the road to be drawn once because it will not change. thank

+1


source to share


4 answers


I'm not sure if you really want your path to never be repainted - repaints events (for example) when your window changes, or when it becomes visible after another window, obstructing it. If your panel never gets repainted, it will look odd.

As far as I remember, Swing will only fire the appropriate paint events for these circumstances, so you should be fine following the normal JPanel subclassing method 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 to an image or other graphics format (to save computation of the displayed data multiple times) once, drawn once, it can save some time on subsequent paints.

+3


source


As far as I know, not unless there is a transparent overlays trick.

Most graphics applications I've seen (and did) just re-draw the entire panel on every redraw. Now you can do this once, in the graphics buffer, and then just paint the entire background right away by copying the graphics buffer to the JPanel. This should be faster than calling all the graphics primitives to draw the road.



Or, as some 2D games do, perhaps paint it once and update the moving parts like the sprites: erase the old place used by the sprites (restore the background there) and re-paint the sprites to the new place. This way, you still have a copy of the road in the graphics buffer, but instead of re-drawing the whole thing, you only update some small parts each time. Maybe a little faster.

+3


source


The component needs to be repainted every time the panel is closed (for example, the frame is minimized / another window is placed on top). Therefore, drawing something just once will not work the way you want it to. To make details that don't change more efficiently, you can draw them once onto the "buffered" image, and then simply draw that buffered image each time 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...
}

      

+3


source


What I am doing is giving a boolean whether a specific part needs to be redrawn. Then in the method paintComponent()

I can check the value and redraw the specific thing or not.

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

      

Something like that.

+1


source







All Articles