0

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: enter image description here

Here is a Picture of the incorrect layout in Linux (Raspberry Pi) and the same Picture when I skip the Method f.setVisible(true)

enter image description here

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jul 15 '17 at 09:09
  • @AndrewThompson I added pictures for a better understanding. Andrew, do you know maybe there is a way to center a jlabel without having to call the method setVisible(true) of the JFrame? – Anastasia Schröter Jul 15 '17 at 09:22
  • See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Jul 15 '17 at 10:42
  • *"I added pictures for a better understanding."* You seem to have completely misunderstood what I wrote. Please be specific about anything you don't understand & I'll explain. *"Andrew, do you know maybe there is a way to center a jlabel without having to call the method setVisible(true) of the JFrame?"* Not going to give this further thought until there is an MCVE / SSCCE in which **the code** hot links to images! – Andrew Thompson Jul 15 '17 at 12:20

0 Answers0