Results 1 to 4 of 4
  1. #1
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Best Way to Clear a BufferedImage?

    I have an array of BufferedImages (TYPE_4BYTE_ABGR), and I need to clear them before I paint on them each time. What is the best way to do this?

    I have tried using clearRect(), but that just makes it all black. (I am of course using Graphics2D to paint on it.) Any additional info needed, please ask.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Best Way to Clear a BufferedImage?

    Well, you could set the color with an alpha value of 0. That would make it transparent and show the Components color thru the image. All other renderings on the image would show normally assuming opaque coloring. When setting the alpha to 0 (e.g. setColor(new Color(0,0,0,0)) use fillRect to "clear" the background. You could also just choose an opaque background color with which to fill the image.


    Java Code:
    public class Test extends JPanel {
    
       public JFrame frame = new JFrame();
       BufferedImage buf   = new BufferedImage(500, 500,
                                 BufferedImage.TYPE_4BYTE_ABGR);
    
       public Test() {
          frame.setTitle("Paint Example");
          setPreferredSize(new Dimension(500, 500));
          frame.setLocationRelativeTo(null);
          frame.setResizable(false);
          frame.add(this);
          frame.pack();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
          setBackground(Color.blue);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                new Test();
             }
          });
       }
       /*
        * Paint red circle in center of image.  Show blue background of panel
        * thru transparent image
        */
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics gbuf = buf.getGraphics();
          gbuf.setColor(new Color(0, 0, 0, 0));
          gbuf.fillRect(0, 0, 500, 500);
          gbuf.setColor(Color.red);
          gbuf.fillOval(200, 200, 100, 100);
          g.drawImage(buf, 0, 0, null);
       }
    
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Best Way to Clear a BufferedImage?

    I just discovered something. The clearRect() method clears the background with the current color of the drawing surface. However, to set the color of the drawing surface you need to use the setBackground method which is only available via Graphics2D. So cast you Graphics instance to Graphics2D.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Best Way to Clear a BufferedImage?

    Quote Originally Posted by jim829 View Post
    I just discovered something. The clearRect() method clears the background with the current color of the drawing surface. However, to set the color of the drawing surface you need to use the setBackground method which is only available via Graphics2D. So cast you Graphics instance to Graphics2D.

    Regards,
    Jim
    Worked great, thanks!

Similar Threads

  1. BufferedImage not going to GC
    By vspin in forum New To Java
    Replies: 11
    Last Post: 07-03-2012, 09:42 AM
  2. BufferedImage rgb
    By Bill87 in forum New To Java
    Replies: 2
    Last Post: 02-25-2010, 06:31 PM
  3. BufferedImage clear off background
    By gogromat in forum Java 2D
    Replies: 2
    Last Post: 12-21-2009, 01:17 AM
  4. Using BufferedImage
    By timkd127 in forum New To Java
    Replies: 5
    Last Post: 12-19-2009, 09:17 PM
  5. BufferedImage through FTP
    By dudejonne in forum New To Java
    Replies: 7
    Last Post: 11-05-2009, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •