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

  • Home
  • SEARCH
  • 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 8902069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:31:16+00:00 2026-06-15T01:31:16+00:00

want to draw text over image. on first mouse click a rectangle region show,

  • 0

want to draw text over image. on first mouse click a rectangle region show, as soon as enter text, text should be drawn with in rectangle shap and rectangle should be auto resize with text as displayed in attached image.

enter image description here

  • 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-06-15T01:31:17+00:00Added an answer on June 15, 2026 at 1:31 am

    The basic concept is like any normal Swing form. You just need to add a little extra work to pull it all together.

    The first thing you need to decide is do you want multi line support or not?

    The example below simple uses a JLayeredPane, to supply the free layout, and a custom JTextArea to supply the editable fields.

    The neat thing about this, is the resizing is mostly taken care for you, as is the “re-editability”, simply click back on the text and you’ll see what I mean.

    enter image description here

    I’ll leave repositioning up to you 😉

    public class TextOverImage {
    
        public static void main(String[] args) {
            new TextOverImage();
        }
    
        public TextOverImage() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new ImagePane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
        public class ImagePane extends JLayeredPane {
    
            private BufferedImage background;
    
            public ImagePane() {
                setFocusable(true);
                try {
                    background = ImageIO.read(new File("Your/image/here"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        Component focusOwner = FocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
                        if (!(focusOwner instanceof OverlayEditor)) {
                            OverlayEditor field = new OverlayEditor();
                            field.setLocation(e.getPoint());
                            add(field);
                            invalidate();
                            repaint();
                            field.requestFocusInWindow();
                        } else {
                            requestFocusInWindow();
                        }
                    }
    
                });
                InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
                ActionMap am = getActionMap();
                im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
                am.put("cancel", new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Component focusOwner = FocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
                        if (focusOwner instanceof OverlayEditor) {
                            remove(focusOwner);
                            invalidate();
                            repaint();
                        }
                    }
    
                });
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (background != null) {
                    int x = (getWidth() - background.getWidth());
                    int y = (getHeight() - background.getHeight());
                    g.drawImage(background, x, y, this);
                }
            }
    
        }
    
        public class OverlayEditor extends JTextArea {
    
            public OverlayEditor() {
                super(1, 10);
                setBorder(null);
                setForeground(Color.WHITE);
                setOpaque(false);
                setSize(getPreferredSize());
    
                getDocument().addDocumentListener(new DocumentListener() {
                    public void update() {
                        setSize(getPreferredSize());
                    }
    
                    @Override
                    public void insertUpdate(DocumentEvent e) {
                        update();
                    }
    
                    @Override
                    public void removeUpdate(DocumentEvent e) {
                        update();
                    }
    
                    @Override
                    public void changedUpdate(DocumentEvent e) {
                        update();
                    }
    
                });
    
                addFocusListener(new FocusListener() {
                    @Override
                    public void focusGained(FocusEvent e) {
                        setBorder(new LineBorder(Color.WHITE));
                        repaint();
                    }
    
                    @Override
                    public void focusLost(FocusEvent e) {
                        setBorder(null);
                        repaint();
                    }
    
                });
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                if (hasFocus()) {
                    g2d.setColor(new Color(0, 0, 0, 32));
                    g2d.fill(new Rectangle(getWidth(), getHeight()));
                }
                g2d.dispose();
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to draw some Rectangle over a single Image . For example I
I want to draw text over 3D point. The text request 2D points rect
I want to draw ImageView with text over a relative layout with 9patch background.
I want to draw a text within an image in C# by using the
I want to use core text to draw string that can span over 100
I want to draw text just like the following style -- the image is
I want to draw text on canvas., how to do it any sample example?
I want to draw a text with 32 bit transparency on a graphics object.
alt text http://www.imagechicken.com/uploads/tn1270646423002339600.png I want to draw like that. How do I draw like
I want to draw rectangle that is only specified percent high of panel im

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.