I have a JFrame with a JLabel in it. My aim is to illustrate the JLabel in the Middle of the JFrame and save a picture of it.
This is the Main Code:
f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
image = ImageIO.read(new File(
pfadVorlageDatei + bezeichnungVorlageDatei));
} catch (IOException e) {
e.printStackTrace();
}
JPanel panel = new JPanel() {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(
image.getWidth(null),
image.getHeight(null));
}
};
drawTextOnJPanel(jodelText, panel);
f.getContentPane().add(panel);
f.pack
f.setLocationRelativeTo(null);
f.setState(JFrame.NORMAL);
f.setVisible(true);
f.dispose();
BufferedImage img2 = getScreenShot(f.getContentPane());
Graphics2D graphics2D = img2.createGraphics();
try {
ImageIO.write(img2, "png", new File(speicherPfad + dateiBezeichnung));
} catch (Exception exception) {
System.out.println("Fehler");
}
The Code for drawTextOnJPane()
String htmlTest = "";
try {
htmlTest = "<html><div align=center>" +
Emoji.parse(finalJodelTextWithCorrectTags, String.valueOf(size - 2)) +
"</div></html>";
} catch (Exception e) {
e.printStackTrace();
}
JLabel label = new JLabel(htmlTest);
EmptyBorder border = new EmptyBorder(5, 235, 70, 100); //Ränderabstände
label.setBorder(border);
label.setFont(new Font("OpenSansEmoji", Font.PLAIN, size));
label.setForeground(Color.white);
panel.setLayout(new BorderLayout());
panel.add(label, BorderLayout.CENTER);
The Code works perfectly when running on Windows. But when I switch it to my Raspberry Pi the Layout is not correct, the JLabel is moved down.
Here is a Picture of the correct layout in Windows:
Here is a Picture of the incorrect layout in Linux (Raspberry Pi) and the same Picture when I skip the Method f.setVisible(true)
When I skip the Method setVisible(true) on Windows then it looks the same, so I think the Method setVisible(true) doesn't work correctly on the Linux OS.