0

I wanted to store the current look of my JPanel in order to re-use it as the background for the next iterations of my game loop. I've tried several things that were mentionned on the Internet, but I wanted to do it in a very simple way. I'm currently using this to story the current Image of the JPanel:

if(needImage){
                Graphics g = tmp.getGraphics();
                g = getGraphics();
                needImage = false;
            }

and this to draw it:

 g.drawImage(tmp, 0, 0, this.getWidth(), this.getHeight(), null);

Any ideas ?

Edit:

I tried this method, found at Can I create a BufferedImage from a JPanel...:

   private BufferedImage getScreenShot(JPanel panel){
        BufferedImage bi = new BufferedImage(
            panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
        panel.paint(bi.getGraphics());
        return bi;
    }

But it doesn't work right. I'm drawing 1 pixel large rectangles in a loop, in order to draw lines when using the move keys, but it seems to only capture the last point with this method.

g.setColor(new Color(hexaR, hexaV, hexaB));
g.fillRect(x, y, 1, 1);

I've taken some screenshots to show you the result: Original:

Original Image

enter image description here

Image after some modification

enter image description here

As you can see, the original image as been deleted, but it remains the final dots of the "cursor" (x and y inputs of the keyboard). Basically, what I want it to do is to stop the drawing of the lines when I press space, but I want to be able to look where the dots I'm moving are.

I don't know if it clear or not, please tell me. I'll try my best to provide clearer explanations.

Community
  • 1
  • 1
Noxilex
  • 98
  • 1
  • 9
  • Why do you think `getGraphics` gives you the current image of the JPanel? `Graphics` is a drawing environment, not an image. – RealSkeptic Nov 18 '15 at 19:54
  • 1
    Create a BufferedImage, use its Graphics content to "print" the component – MadProgrammer Nov 18 '15 at 19:55
  • How do I do that ? I'm not very used to use `Graphics` – Noxilex Nov 18 '15 at 20:13
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Nov 19 '15 at 02:02
  • As an aside, if there is a common BG to different frames of a game, I would draw the BG to a `BufferedImage`, and draw the image to the panel for each frame of the game animation. I feel the code seen here is taking the wrong approach to achieve the stated goal. – Andrew Thompson Nov 19 '15 at 02:04
  • 1
    @AndrewThompson Got it sir! :) – m4n0 Nov 19 '15 at 03:58
  • 1
    @ManojKumar Now that's a classy edit. I was hoping you'd have the good sense to embed the images when doing the edit. :) – Andrew Thompson Nov 19 '15 at 04:19
  • @AndrewThompson Drawing the BG to a BufferedImage and painting it for each frame is exactly what I want to do. But rather than the expected result, my image gets reset and the only thing that's printed is the last point drawn. – Noxilex Nov 19 '15 at 10:01
  • *"But rather than the expected result,.."* Time to take this up to the next level, with my most common 'copy/paste comment'... ;) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Provide a complete (short) example that will run and display the problem, and I'm sure the solution will become clear to someone that happens by the question. – Andrew Thompson Nov 19 '15 at 11:05

0 Answers0