How to draw something over all components of a JPanel

Roberto Falk :

How to draw something over all components of a Panel?

In the code below I tried to do this overriding the paintComponent method, I call super.paintComponent(g) hoping this will draw all components added as part of the constructor, but still my X stays below the image.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Stack extends JPanel {

    private final JLabel some_text = new JLabel("Some very long text that will be covered", JLabel.LEFT);
    private final JLabel some_icon = new JLabel((Icon)null, JLabel.CENTER);
    public static final String COMPASS_URL = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Compass_Rose_English_North.svg/240px-Compass_Rose_English_North.svg.png";

    public Stack() throws IOException {
        super(new GridBagLayout());

        URL compassUrl = new URL(COMPASS_URL);
        BufferedImage compassImage = ImageIO.read(compassUrl);
        ImageIcon compassIcon = new ImageIcon(compassImage);
        some_icon.setIcon(compassIcon);

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;

        add(some_icon, c);

        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        add(some_text, c);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setColor(Color.RED);
        g2.setStroke(new BasicStroke(15));
        g2.drawLine(0, 0, this.getWidth(), this.getHeight());
        g2.drawLine(this.getWidth(), 0, 0, this.getHeight());
        g2.dispose();
    }

    private static void createAndShowUI() throws IOException {
        JFrame frame = new JFrame("MyFrame");
        frame.getContentPane().add(new Stack());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    createAndShowUI();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

Is there a way to bring my X to the foreground or to wait for super.paintComponent(g) to paint everything, and then run the code that draws the X?

Thanks, Roberto

camickr :

Override the paint(…) method of your JPanel:

protected void paint(Graphics g)
{
    super.paint(g);

    // add custom painting code here
}

The paint(…) method invokes the paintChildren(…) method so your custom code will be executed after all the components on the panel are painted.

See A Closer Look at the Painting Mechanism for more information.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=356284&siteId=1