I am creating a word search GUI using Swing for a club project. I want the answers "hidden" by making them upside down in the GUI. I've seen a post where a person put a negative Integer as font size but it was 9 years ago and it doesn't seem to work anymore, instead the text simply does not appear
Font FontUpsideDown = new Font("Sans_Serif", Font.PLAIN, -50);
JTextArea upsideDown = new JTextArea("Hello");
upsideDown.setFont(FontUpsideDown);
upsideDown.setEditable(false);
...
JPanel.add(upsideDown, gbc);
I've seen a post where a person put a negative Integer as font size but it was 9 years ago
If you are referring to this question then this answer , to that question, worked for me. Simply override method paintComponent of the relevant JComponent
(which appears, from your question, to be JTextArea
). Below code demonstrates.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Inverted {
private static JScrollPane createTextArea() {
JTextArea textArea = new JTextArea(10, 50) {
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.scale( -1.0, -1.0 );
g2.translate( -getWidth(), -getHeight() );
super.paintComponent(g2);
}
};
textArea.append("Is this upside down?");
JScrollPane scrollPane = new JScrollPane(textArea);
return scrollPane;
}
private static void gui() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Inverted") {
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.scale( -1.0, -1.0 );
g2.translate( -getWidth(), -getHeight() );
super.paintComponent(g2);
}
};
JPanel inverted = new JPanel();
inverted.add(label);
frame.add(inverted, BorderLayout.PAGE_START);
frame.add(createTextArea(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(Inverted::gui);
}
}
When I run the above code, using Java 19 on Windows 10, I get the following GUI:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.