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 7673361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:29:40+00:00 2026-05-31T16:29:40+00:00

I’m implementing my own double-buffering for a BufferedImage in a JPanel so that I

  • 0

I’m implementing my own double-buffering for a BufferedImage in a JPanel so that I can display the mouse location in the BufferedImage without repainting every object back onto it on mousemovement. When a JMenu in the parent JFrame is open, the BufferedImage gets repainted on top of the JMenu.

This class is not complete, and has only the necessary methods,

    public class Foo extends JPanel implements ComponentListener {
        BufferedImage bufferedImage;
        long mousePosX;
        long mousePoxY;

        protected void paintComponent(Graphics g) {
            paintComponent(g, this.xform);
        }
        protected void paintComponent(Graphics graphics, XFormPixel xformIn) {
            bufferedImage = new BufferedImage(this.getWidth(),this.getHeight(),BufferedImage.TYPE_INT_RGB);
            Graphics g = bufferedImage.getGraphics();
            super.paintComponent(g);
            //Draw lots of stuff to graphics

            if(drawMouseLocation) {
                int width = this.getWidth();
                int height = this.getHeight();
                Color origColor = g.getColor();
                g.setColor(textColor);
                if (cursorLocation != null) {
                    g.drawString("X: "+mousePosX + " Y: " + mousePosY);
                }
            }

            g.setColor(origColor);
            graphics.drawImage(bufferedImage,0,0,null);
        }

        public void drawMouseLocation() {   

            int width = this.getWidth();
            int height = this.getHeight();
            Image image = bufferedImage;
            Graphics graphics = this.getGraphics();
            Graphics g = image.getGraphics();
            Color origColor = g.getColor();
            g.setColor(textColor);
            if (cursorLocation != null) {
                g.drawString("X: "+mousePosX + " Y: " + mousePosY);
            }
            g.setColor(origColor);
            graphics.drawImage(image,0,0,this);
        }
    }

Is there another way to do this?

Another possible dealbreaker is that when the Foo JPanel is initialized, it has a black border, but when the image is drawn to display the mouselocation, the border disappears. I’m assuming calling a repaint() or something on the parent will fix both issues, but it would also call a repaint on the child, which I am trying to avoid.

EDIT 1:
Here is the requested runnable code. When creating it, I could not get the double-buffering working correctly, so I’m also having trouble with the flickering of the mouse location when the mouse is moved.

    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;

    public class DrawingTestFrame extends JFrame {
        private static final long serialVersionUID = 1L;        

        public static void main(String[] args) {
                new DrawingTestFrame();
        }   
        public DrawingTestFrame() {
                init();
        }

        public void init() {
                JMenuBar menuBar = new JMenuBar();                  
                    setJMenuBar(menuBar);
                JMenu dropMenu = new JMenu("Drop This");
                dropMenu.add(needs);
                dropMenu.add(to);
                dropMenu.add(overlap);
                menuBar.add(dropMenu);          
                    DrawingTest test = new DrawingTest();
                setTitle("Drawing Test");
                add(test);
                setMinimumSize(new Dimension(550,270));
                pack();
                setVisible(true);
        }

            public static Action needs = new AbstractAction("Needs") {
                private static final long serialVersionUID = 1L;
                public void actionPerformed(ActionEvent ae) {}};    
            public static Action to = new AbstractAction("To") {
                private static final long serialVersionUID = 1L;
                public void actionPerformed(ActionEvent ae) {}};    
            public static Action overlap = new AbstractAction("Overlap") {
                private static final long serialVersionUID = 1L;
                public void actionPerformed(ActionEvent ae) {}};    
    }


    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.MouseInfo;
    import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.image.BufferedImage;
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;

    public class DrawingTest extends JPanel implements MouseListener  {
        private static final long serialVersionUID = 1L;
            public Component parent;
            private Point mouseLocation;
            private BufferedImage bufferedImage;
            public DrawingTest() {
                    init();
            }        
            public void init() {
                    this.setPreferredSize(new Dimension(100, 100));
                    this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                    this.addMouseListener(this);
                    this.addMouseMotionListener(new MouseMotionListener() {
                public void mouseDragged(MouseEvent e) {
                    mouseLocation = MouseInfo.getPointerInfo().getLocation();
                    DrawingTest.this.repaint();
                }

                public void mouseMoved(MouseEvent e) {
                    mouseLocation = MouseInfo.getPointerInfo().getLocation();
                    DrawingTest.this.drawLocation();
                }
                    });
                    this.setVisible(true);
            }
        public void mouseClicked(MouseEvent e) {
            mouseLocation = MouseInfo.getPointerInfo().getLocation();
            this.repaint();
        }
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}

            protected void paintComponent(Graphics graphics) {
                    bufferedImage = new   
                            BufferedImage(this.getWidth(),this.getHeight(),BufferedImage.TYPE_INT_RGB);
                    Graphics2D g = bufferedImage.createGraphics();
                    super.paintComponent(g);
                    g.setColor(Color.red);
                    g.drawRect(10,10,110,110);
                    graphics.drawImage(bufferedImage,0,0,null);
                    if (mouseLocation != null) {
                        graphics.drawString("X: " + mouseLocation.getX() + 
                "  Y: " + mouseLocation.getY(), this.getWidth()/2 - 50, this.getHeight()-10);
                    }
            }
            protected void drawLocation() {
                    this.getGraphics().drawImage(bufferedImage, 0,0,null);
                    this.getGraphics().setColor(Color.green);
                    if (mouseLocation != null) {
                        this.getGraphics().drawString("X: " + mouseLocation.getX() + 
                "  Y: " + mouseLocation.getY(), this.getWidth()/2 - 50, this.getHeight()-10);
                    }
            }
    }

Thanks!

  • 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-31T16:29:41+00:00Added an answer on May 31, 2026 at 4:29 pm

    The problem is you’re getting your Graphics object by calling getGraphics() on a component and this you should not do. Why are you doing this home-cooked double buffering, and where have you read that this is the way to implement it? I’d like to see that tutorial. If this were my application, I’d either:

    • Use a BufferedImage for my background images, and simply draw over it in paintComponent, perhaps limiting my painting to the rectangle of interest…
    • or show my mouse position using a JLabel.

    For example, this code shows both techniques:

    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.image.BufferedImage;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class DrawingTestFrame2 {
       private static void createAndShowGui() {
          JFrame frame = new JFrame("DrawingTestFrame2");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          DrawingTest2 drawingTest = new DrawingTest2();
    
          frame.getContentPane().add(drawingTest);
          frame.setJMenuBar(drawingTest.createMenuBar());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    class DrawingTest2 extends JPanel {
    
       private static final int PREF_W = 500;
       private static final int PREF_H = 300;
       private static final int RECT_W = 100;
       private static final int RECT_H = 20;
       public static final Rectangle BOTTOM_RECT = new Rectangle(PREF_W/2 - RECT_W/2, PREF_H - RECT_H, 
             RECT_W, RECT_H);
       private String[] menuItemStrings = {"One", "Two", "Three"};
       public Point mouseLocation;
       private BufferedImage bufferedImage = new BufferedImage(PREF_W, PREF_H, 
             BufferedImage.TYPE_INT_ARGB);
       private JLabel mousePositionLabel = new JLabel("", SwingConstants.RIGHT);
    
       public DrawingTest2() {
          MouseAdapter mouseAdapter = new MyMouseAdapter();
          addMouseListener(mouseAdapter);
          addMouseMotionListener(mouseAdapter);
    
          mousePositionLabel.setForeground(Color.gray);
          setLayout(new GridBagLayout());
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridx = 1;
          gbc.gridy = 1;
          gbc.gridwidth = 1;
          gbc.gridheight = 1;
          gbc.weightx = 1.0;
          gbc.weighty = 1.0;
          gbc.anchor = GridBagConstraints.SOUTHEAST;
          add(mousePositionLabel, gbc);
    
          Graphics2D g2 = bufferedImage.createGraphics();
          g2.setPaint(new GradientPaint(0, 0, Color.yellow, 40, 40, Color.green, true));
          g2.fillRect(0, 0, PREF_W, PREF_H);
          g2.dispose();
          g2 = bufferedImage.createGraphics();
          g2.setColor(Color.red);
          g2.setStroke(new BasicStroke(3f));
          g2.drawRect(10, 10, 110, 110);
          g2.dispose();
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          if (bufferedImage != null) {
             g.drawImage(bufferedImage, 0, 0, this);
          }
          if (mouseLocation != null) {
             g.drawString("X: " + mouseLocation.getX() + "  Y: "
                   + mouseLocation.getY(), this.getWidth() / 2 - 50,
                   this.getHeight() - 10);
          }
       }
    
       public JMenuBar createMenuBar() {
          JMenuBar menuBar = new JMenuBar();
          JMenu menu = new JMenu("Menu");
          for (int i = 0; i < menuItemStrings .length; i++) {
             menu.add(new JMenuItem(menuItemStrings[i]));
          }
          menuBar.add(menu );
          return menuBar;
       }
    
       class MyMouseAdapter extends MouseAdapter {
          @Override
          public void mouseMoved(MouseEvent mEvt) {
             mouseLocation = mEvt.getLocationOnScreen();
             repaint(BOTTOM_RECT);
    
             String mousePosStr = String.format("x:%d y:%d", mouseLocation.x, mouseLocation.y);
             mousePositionLabel.setText(mousePosStr);
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a

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.