Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rounded Panel clipping image #840

Closed
felix-caba opened this issue May 6, 2024 · 3 comments
Closed

Rounded Panel clipping image #840

felix-caba opened this issue May 6, 2024 · 3 comments
Milestone

Comments

@felix-caba
Copy link

felix-caba commented May 6, 2024

I have a rounded panel that is inside a Panel with a BackgroundImage painted over it using PaintComponent. Problem is, this happens:

image

My actual code is:

For the Background Panel:

public class JPanelBackground extends JPanel {

    private BufferedImage backgroundImage;

    public JPanelBackground(String imagePath) throws IOException {
        backgroundImage = ImageIO.read(new File(imagePath));
    }

    @Override
    protected void paintComponent(Graphics g) {

        super.paintComponent(g);
        int panelWidth = getWidth();
        int panelHeight = getHeight();

        // Obtener el ancho y alto de la imagen de fondo
        int imageWidth = backgroundImage.getWidth();
        int imageHeight = backgroundImage.getHeight();

        // Calcular la relación de aspecto de la imagen
        double aspectRatio = (double) imageWidth / imageHeight;

        // Calcular el ancho y alto de la imagen para que se ajuste al panel manteniendo la relación de aspecto
        int scaledWidth = panelWidth;
        int scaledHeight = (int) (panelWidth / aspectRatio);

        // Si la altura escalada es menor que la altura del panel, recalcula el ancho y alto para ajustar a la altura del panel
        if (scaledHeight < panelHeight) {
            scaledHeight = panelHeight;
            scaledWidth = (int) (panelHeight * aspectRatio);
        }

        // Calcular las coordenadas de dibujo para centrar la imagen
        int x = (panelWidth - scaledWidth) / 2;
        int y = (panelHeight - scaledHeight) / 2;

        // Dibujar la imagen de fondo escalada y centrada en el panel
        g.drawImage(backgroundImage, x, y, scaledWidth, scaledHeight, this);

    }


}

For the Rounded Panel;

putClientProperty( FlatClientProperties.STYLE, "arc: 90" ); setBackground( Color.red);

Is this a problem, or is this a Skill Issue from my behalf?

@DevCharly
Copy link
Collaborator

Panels fills the whole background if they are opaque, which is the default.

So you need to make the rounded panel non-opaque (e.g. roundedPanel.setOpaque(false)).

The downside is that FlatLaf no longer paints the rounded "red" background.
You need to paint that yourself ATM 😉

Maybe I should change FlatLaf so that panels with rounded corners are painted even if they are non-opaque...

@felix-caba
Copy link
Author

Oh thanks!

DevCharly added a commit that referenced this issue May 21, 2024
@DevCharly
Copy link
Collaborator

I've changed panel's background painting behavior:
rounded background is now always painted, even if panel is non-opaque.

Try latest 3.5-SNAPSHOT: https://github.com/JFormDesigner/FlatLaf#snapshots

@DevCharly DevCharly added this to the 3.5 milestone May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants