Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7523073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:41:26+00:00 2026-05-30T02:41:26+00:00

I have a component extends JPanel. It saves itself as bufferedimage on every call

  • 0

I have a component extends JPanel. It saves itself as bufferedimage on every call of paintComponent method. Component is not completely transparent, only its background. Problem is background is not transparent. I am using setOpaque(false);

Here is my relevant code;

private BufferedImage bufImage = null;

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;

    // if first time call
    if (bufImage == null) {
        int w = this.getWidth();
        int h = this.getHeight();
        bufImage = (BufferedImage)this.createImage(w, h);
    }

    g2.drawImage(bufImage, null, 0, 0);

    // draw sth
    g2.draw(sth);
}

—

I also tried

bufImage =  new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

instead of

bufImage = (BufferedImage)this.createImage(w, h);

When i do that; background transperancy works but i can only draw with white color. I have no idea what causes that.

Note:
I used that code to check if it is working;

File outputfile = new File("saved.png");
ImageIO.write(bufImage, "png", outputfile);

saved.png had transparent background but drawings were only white.


This is the component, only lets drawing rectangle with mouse;

class PaintPanel extends JPanel implements MouseListener, MouseMotionListener {
    private BufferedImage _bufImage = null;
    private boolean dragging = false;
    private Point _start = null, _end = null;

    public PaintPanel() {
        setOpaque(false);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;

        if (_bufImage == null) {
            int w = this.getWidth();
            int h = this.getHeight();
            _bufImage =  new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            //_bufImage = (BufferedImage)this.createImage(w, h);
        }

        g2.drawImage(_bufImage, null, 0, 0);

        if (dragging) {
            drawCurrentShape(g2);
        }
    }

    private void drawCurrentShape(Graphics2D g2) {
        int startx = (int) _start.getX();
        int starty = (int) _start.getY();
        int stopx = (int) _end.getX();
        int stopy = (int) _end.getY();

        int width = Math.abs(startx - stopx);
        int height = Math.abs(starty - stopy);
        int x = startx, y = starty;
        if(x > stopx)
            x = stopx;
        if(y > stopy)
            y = stopy;

        Rectangle r = new Rectangle(x, y, width, height);
        g2.draw(r);
    }

    public void mousePressed(MouseEvent e) {
        dragging = true;       
        _start = e.getPoint();
        _end   = _start;
    }

    public void mouseDragged(MouseEvent e) {
        _end = e.getPoint();
        this.repaint();
    }

    public void mouseReleased(MouseEvent e) {
        _end = e.getPoint();
        if (dragging) {
            dragging = false;
            drawCurrentShape(_bufImage.createGraphics());  
            this.repaint();
        }
    }

    public void mouseMoved  (MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited (MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:41:27+00:00Added an answer on May 30, 2026 at 2:41 am

    try this:

    bufImage = new BufferedImage(w,h,java.awt.image.BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = bufImage.createGraphics();
    this.print(graphics);
    graphics.dispose();
    

    The key is to use print()

    Edit: I tried the following and transparency works like a charm:

    import java.awt.Dimension;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Test2 {
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            final JPanel p = new JPanel();
            p.setPreferredSize(new Dimension(400, 400));
            p.setOpaque(false);
            JButton button = new JButton("Hello world");
            p.add(button);
            frame.add(p);
            frame.pack();
            frame.setVisible(true);
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    BufferedImage bufImage = new BufferedImage(p.getWidth(), p.getHeight(), java.awt.image.BufferedImage.TYPE_INT_ARGB);
                    Graphics2D graphics = bufImage.createGraphics();
                    p.print(graphics);
                    graphics.dispose();
                    try {
                        ImageIO.write(bufImage, "png", new File("d:/tmp/tmp.png"));
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a Hexagon component that extends JPanel . It draws a hexagon
I have a DataGridColumn with an ItemRenderer that extends the Box component. The default
I have a component that inherits from JPanel, I draw a grid on it.
I have a ListCellRenderer which extends JPanel . Now I try to override its
I have built a custom component that shows only a line. The line is
I have a class whitch extends JPanel: public class ButtonPanel extends JPanel { private
So I have a JPanel object as a component of a JFrame, and I
I have a MyCanvas class that extends JComponent. On this canvas I have drawn
I have a component which writes/generates javascript from a server side renderer. This component
I have a component which has a List<T> property. The class in the list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.