Cómo dibujar algo sobre todos los componentes de un JPanel

Roberto Falk:

Cómo dibujar algo sobre todos los componentes de un panel?

En el siguiente código traté de hacer esto reemplazando el paintComponentmétodo, que yo llamo super.paintComponent(g)esperando que esto atraerá a todos los componentes añadidos como parte del constructor, pero aún así mis estancias X debajo de la imagen.

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();
                }
            }
        });
    }
}

¿Hay una manera de traer a mi X al primer plano o para esperar super.paintComponent(g)a pintar todo, y luego ejecutar el código que señala a la X?

Gracias, Roberto

camickr:

Invalidar el paint(…)método de su JPanel:

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

    // add custom painting code here
}

El paint(…)método invoca el paintChildren(…)método para que su código personalizado se ejecutará después de que todos los componentes del panel están pintadas.

Ver Una mirada más cercana en el Mecanismo de Pintura para más información.

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=361798&siteId=1
Recomendado
Clasificación