|
import java.awt.*; |
|
import java.awt.event.ActionEvent; |
|
import java.awt.event.ActionListener; |
|
import java.util.Map; |
|
|
|
import javax.swing.*; |
|
|
|
public class FontRenderingTest extends JFrame { |
|
|
|
private JPanel mainPanel; |
|
private JTextArea infoArea; |
|
|
|
public FontRenderingTest() { |
|
setTitle( |
|
"Font Rendering Test - Java " + System.getProperty("java.version") |
|
); |
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|
setSize(800, 600); |
|
setLocationRelativeTo(null); |
|
|
|
initComponents(); |
|
displaySystemInfo(); |
|
} |
|
|
|
private void initComponents() { |
|
mainPanel = new JPanel(new BorderLayout()); |
|
|
|
// Create header with title |
|
JLabel titleLabel = new JLabel( |
|
"Font Rendering Test Application", |
|
JLabel.CENTER |
|
); |
|
titleLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24)); |
|
titleLabel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); |
|
|
|
// Create font samples panel |
|
JPanel samplesPanel = createFontSamplesPanel(); |
|
|
|
// Create info panel |
|
infoArea = new JTextArea(); |
|
infoArea.setEditable(false); |
|
infoArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); |
|
infoArea.setBackground(new Color(248, 248, 248)); |
|
infoArea.setBorder( |
|
BorderFactory.createTitledBorder("System Information") |
|
); |
|
|
|
JScrollPane scrollPane = new JScrollPane(infoArea); |
|
scrollPane.setPreferredSize(new Dimension(800, 200)); |
|
|
|
// Create control panel |
|
JPanel controlPanel = createControlPanel(); |
|
|
|
mainPanel.add(titleLabel, BorderLayout.NORTH); |
|
mainPanel.add(samplesPanel, BorderLayout.CENTER); |
|
mainPanel.add(scrollPane, BorderLayout.SOUTH); |
|
mainPanel.add(controlPanel, BorderLayout.EAST); |
|
|
|
add(mainPanel); |
|
} |
|
|
|
private JPanel createFontSamplesPanel() { |
|
JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5)); |
|
panel.setBorder(BorderFactory.createTitledBorder("Font Samples")); |
|
|
|
// Different font sizes and styles |
|
String sampleText = |
|
"The quick brown fox jumps over the lazy dog 1234567890"; |
|
|
|
Font[] fonts = { |
|
new Font(Font.SANS_SERIF, Font.PLAIN, 10), |
|
new Font(Font.SANS_SERIF, Font.PLAIN, 12), |
|
new Font(Font.SANS_SERIF, Font.PLAIN, 14), |
|
new Font(Font.SANS_SERIF, Font.BOLD, 14), |
|
new Font(Font.SERIF, Font.PLAIN, 14), |
|
new Font(Font.SERIF, Font.ITALIC, 14), |
|
new Font(Font.MONOSPACED, Font.PLAIN, 12), |
|
new Font("Arial", Font.PLAIN, 14), |
|
new Font("Helvetica", Font.PLAIN, 14), |
|
new Font("Times New Roman", Font.PLAIN, 14), |
|
}; |
|
|
|
for (Font font : fonts) { |
|
JLabel label = new JLabel( |
|
sampleText + |
|
" (" + |
|
font.getFontName() + |
|
", " + |
|
font.getSize() + |
|
"pt)" |
|
); |
|
label.setFont(font); |
|
label.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10)); |
|
panel.add(label); |
|
} |
|
|
|
return panel; |
|
} |
|
|
|
private JPanel createControlPanel() { |
|
JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5)); |
|
panel.setBorder(BorderFactory.createTitledBorder("Controls")); |
|
panel.setPreferredSize(new Dimension(200, 0)); |
|
|
|
// Refresh button |
|
JButton refreshButton = new JButton("Refresh Info"); |
|
refreshButton.addActionListener(e -> displaySystemInfo()); |
|
|
|
// Test rendering hints button |
|
JButton hintsButton = new JButton("Test Rendering Hints"); |
|
hintsButton.addActionListener(e -> showRenderingHintsDialog()); |
|
|
|
// Font smoothing test |
|
JButton smoothingButton = new JButton("Font Smoothing Test"); |
|
smoothingButton.addActionListener(e -> showFontSmoothingTest()); |
|
|
|
panel.add(refreshButton); |
|
panel.add(hintsButton); |
|
panel.add(smoothingButton); |
|
|
|
return panel; |
|
} |
|
|
|
private void displaySystemInfo() { |
|
StringBuilder info = new StringBuilder(); |
|
|
|
// Java version info |
|
info |
|
.append("Java Version: ") |
|
.append(System.getProperty("java.version")) |
|
.append("\n"); |
|
info |
|
.append("Java Vendor: ") |
|
.append(System.getProperty("java.vendor")) |
|
.append("\n"); |
|
info |
|
.append("Java Home: ") |
|
.append(System.getProperty("java.home")) |
|
.append("\n\n"); |
|
|
|
// OS info |
|
info.append("OS: ").append(System.getProperty("os.name")).append(" "); |
|
info.append(System.getProperty("os.version")).append("\n"); |
|
info |
|
.append("Architecture: ") |
|
.append(System.getProperty("os.arch")) |
|
.append("\n\n"); |
|
|
|
// Display info |
|
GraphicsEnvironment ge = |
|
GraphicsEnvironment.getLocalGraphicsEnvironment(); |
|
GraphicsDevice gd = ge.getDefaultScreenDevice(); |
|
DisplayMode dm = gd.getDisplayMode(); |
|
|
|
info |
|
.append("Display Resolution: ") |
|
.append(dm.getWidth()) |
|
.append("x") |
|
.append(dm.getHeight()) |
|
.append("\n"); |
|
info.append("Color Depth: ").append(dm.getBitDepth()).append(" bits\n"); |
|
info |
|
.append("Refresh Rate: ") |
|
.append(dm.getRefreshRate()) |
|
.append(" Hz\n\n"); |
|
|
|
// Font rendering hints |
|
info.append("Current AWT Desktop Hints:\n"); |
|
Toolkit toolkit = Toolkit.getDefaultToolkit(); |
|
Map<?, ?> desktopHints = (Map<?, ?>) toolkit.getDesktopProperty( |
|
"awt.font.desktophints" |
|
); |
|
if (desktopHints != null) { |
|
for (Map.Entry<?, ?> entry : desktopHints.entrySet()) { |
|
info |
|
.append(" ") |
|
.append(entry.getKey()) |
|
.append(" = ") |
|
.append(entry.getValue()) |
|
.append("\n"); |
|
} |
|
} else { |
|
info.append(" No desktop hints available\n"); |
|
} |
|
|
|
// JVM arguments related to font rendering |
|
info.append("\nRelevant System Properties:\n"); |
|
String[] properties = { |
|
"apple.awt.graphics.UseQuartz", |
|
"apple.awt.textantialiasing", |
|
"sun.java2d.uiScale", |
|
"sun.java2d.uiScale.enabled", |
|
"awt.useSystemAAFontSettings", |
|
"swing.aatext", |
|
"sun.java2d.opengl", |
|
"sun.java2d.metal", |
|
}; |
|
|
|
for (String prop : properties) { |
|
String value = System.getProperty(prop); |
|
info |
|
.append(" ") |
|
.append(prop) |
|
.append(" = ") |
|
.append(value != null ? value : "not set") |
|
.append("\n"); |
|
} |
|
|
|
infoArea.setText(info.toString()); |
|
} |
|
|
|
private void showRenderingHintsDialog() { |
|
JDialog dialog = new JDialog(this, "Rendering Hints Test", true); |
|
dialog.setSize(600, 400); |
|
dialog.setLocationRelativeTo(this); |
|
|
|
JPanel panel = new JPanel(new GridLayout(0, 1)); |
|
String testText = "Sample text for rendering hints testing"; |
|
|
|
// Different rendering hint combinations |
|
RenderingHints[] hintsArray = { |
|
new RenderingHints( |
|
RenderingHints.KEY_TEXT_ANTIALIASING, |
|
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF |
|
), |
|
new RenderingHints( |
|
RenderingHints.KEY_TEXT_ANTIALIASING, |
|
RenderingHints.VALUE_TEXT_ANTIALIAS_ON |
|
), |
|
new RenderingHints( |
|
RenderingHints.KEY_TEXT_ANTIALIASING, |
|
RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT |
|
), |
|
new RenderingHints( |
|
RenderingHints.KEY_TEXT_ANTIALIASING, |
|
RenderingHints.VALUE_TEXT_ANTIALIAS_GASP |
|
), |
|
new RenderingHints( |
|
RenderingHints.KEY_TEXT_ANTIALIASING, |
|
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB |
|
), |
|
new RenderingHints( |
|
RenderingHints.KEY_TEXT_ANTIALIASING, |
|
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HBGR |
|
), |
|
}; |
|
|
|
String[] descriptions = { |
|
"TEXT_ANTIALIAS_OFF", |
|
"TEXT_ANTIALIAS_ON", |
|
"TEXT_ANTIALIAS_DEFAULT", |
|
"TEXT_ANTIALIAS_GASP", |
|
"TEXT_ANTIALIAS_LCD_HRGB", |
|
"TEXT_ANTIALIAS_LCD_HBGR", |
|
}; |
|
|
|
for (int i = 0; i < hintsArray.length; i++) { |
|
JPanel textPanel = new CustomRenderingPanel( |
|
testText, |
|
hintsArray[i], |
|
descriptions[i] |
|
); |
|
panel.add(textPanel); |
|
} |
|
|
|
dialog.add(new JScrollPane(panel)); |
|
dialog.setVisible(true); |
|
} |
|
|
|
private void showFontSmoothingTest() { |
|
JOptionPane.showMessageDialog( |
|
this, |
|
"Check the terminal for font smoothing commands.\n" + |
|
"Current AppleFontSmoothing setting can be checked with:\n" + |
|
"defaults read -g AppleFontSmoothing\n\n" + |
|
"Try values 1, 2, or 3:\n" + |
|
"defaults write -g AppleFontSmoothing -int 2", |
|
"Font Smoothing Test", |
|
JOptionPane.INFORMATION_MESSAGE |
|
); |
|
} |
|
|
|
// Custom panel for testing different rendering hints |
|
private static class CustomRenderingPanel extends JPanel { |
|
|
|
private String text; |
|
private RenderingHints hints; |
|
private String description; |
|
|
|
public CustomRenderingPanel( |
|
String text, |
|
RenderingHints hints, |
|
String description |
|
) { |
|
this.text = text; |
|
this.hints = hints; |
|
this.description = description; |
|
setPreferredSize(new Dimension(500, 40)); |
|
setBorder(BorderFactory.createTitledBorder(description)); |
|
} |
|
|
|
@Override |
|
protected void paintComponent(Graphics g) { |
|
super.paintComponent(g); |
|
Graphics2D g2d = (Graphics2D) g; |
|
g2d.setRenderingHints(hints); |
|
g2d.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 14)); |
|
g2d.drawString(text, 10, 25); |
|
} |
|
} |
|
|
|
public static void main(String[] args) { |
|
// Print current JVM arguments for reference |
|
System.out.println("Current JVM arguments (font-related):"); |
|
String[] properties = { |
|
"apple.awt.graphics.UseQuartz", |
|
"apple.awt.textantialiasing", |
|
"sun.java2d.uiScale", |
|
"awt.useSystemAAFontSettings", |
|
"swing.aatext", |
|
}; |
|
|
|
for (String prop : properties) { |
|
System.out.println(" " + prop + " = " + System.getProperty(prop)); |
|
} |
|
System.out.println(); |
|
|
|
SwingUtilities.invokeLater(() -> { |
|
try { |
|
UIManager.setLookAndFeel( |
|
UIManager.getSystemLookAndFeelClassName() |
|
); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
|
|
new FontRenderingTest().setVisible(true); |
|
}); |
|
} |
|
} |