Draw on JPanel and then save to a file, but saved file is all white

Go To StackoverFlow.com

0

I'm a new poster to stackoverflow, but I've always read the posts for inspiration and I am happy to be a part of the community.

I am drawing an image to a JPanel and then I wish to save that image to a file. The painting on the JPanel goes fine, but when I look at the image it is either all white or all black. I don't know why the image is not saving the way it looks on the JPanel. I guess it is possible I am not correctly referencing the Panel when drawing the image to the buffer and saving it? Its almost like the bufferedImage is blank. I don't have a lot of experience with awt so I have a feeling I'm making a really stupid mistake.

I only overwrite the paintComponent() method once, and in it I do my drawing(which shows up flawlessly on the JPanel) and then at the bottom of it I call the saveImage() method which is supposed to save the image to a file. But as I metioned before, its always a blank image. I use the repaint() method in the constructor.

I won't bog this post down with the entire code. Its a very simple code and the relevant pieces are below.

class drawingBarcode extends JPanel 

    public drawingBarcode(){
        repaint();
        try{
            Thread.sleep(999);

        }catch(InterruptedException e){
            e.printStackTrace();
        }

public void saveImage() {
    BufferedImage bi = new BufferedImage(350, 150, BufferedImage.TYPE_INT_RGB);    

      Graphics2D g2 = bi.createGraphics();//creates and returns a graphics 2d for drawing into buffer

     //  g2.setColor(color1);
       super.paintComponent(g2);

        g2.dispose();
       try
       {
            ImageIO.write(bi, "jpg", new File("test.jpg\\"));        
       }
       catch(IOException ioe)
       {
        System.out.println("Something went wrong");
            ioe.printStackTrace();
        }

public void paintComponent(Graphics g){
        Graphics2D g2D = (Graphics2D) g;

        super.paintComponent(g2D);

        setStrokeWithPen1(g2D);
        drawAsterix(g2D);//draw asterix(start digit) always

/* some drawing takes place here using g2D. */

        g2D.dispose();
        saveImage();
        }
}

Any help that can be offered or advice would be more than greatly appreciated!

2012-04-04 22:34
by user1313973
1) For better help sooner, post an SSCCE. For a working SSCCE, see <code>ComponentImageCapture.java</code>. 2) Why all the blank lines in the code? They did not make it any more clear. 3) Thread.sleep(999); Oh boy.. Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling Thread.sleep(n) implement a Swing Timer for repeating tasks or a SwingWorker for long running tasks. See Concurrency in Swing for more details - Andrew Thompson 2012-04-05 05:51


1

Your save image routine calls super.paintComponent, missing out all your custom paint code when painting to the image graphics! I would refactor your code - you dont want the file to be saved every time the UI paints do you?

2012-04-05 06:16
by davidfrancis
Ps hope this helps! Andrews advice would then be your next port of call I imagin - davidfrancis 2012-04-05 06:17
Ads