Im making a Swing application which uses a custom JPanel that has rounded corners. This JPanel, uses Flatlaf library to round its corners. Like:
putClientProperty( FlatClientProperties.STYLE, "arc: 90" );
Problem is, my main JPanel that has an image to it, that serves as the root to this rounded JPanel, has some problems.
As you can see, for the sake of visiblity, my rounded JPanel is red, with rounded corners. Yet, I can see the old corners coming in, displaying the black squared corners.
This is my Background Panel code:
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);
}
}
And this is my GUI code, which has createUIComponents from Intellij Idea GUI Designer.
private void createUIComponents() {
try{
panelLogin = new JPanelBackground("src/main/resources/photos/fondoLogin.png"); //BackgroundIMG Panel
panelLoginIn = new JPanelRounded(); // Rounded JPanel
} catch (IOException e) {
e.printStackTrace();
}
}
}
I tried looking into some other stackoverflow posts, but seems like the problem is not exactly the same as I have. I dont know how I could fix this.