Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sadly, scaling to a larger size from a fixed resolution will always result in some aliasing artifact. Here's an alternative approach that scales the font used by <code>JTextPane</code>.</p> <p>For low-level control, consider <a href="http://download.oracle.com/javase/6/docs/api/java/awt/font/TextLayout.html" rel="nofollow noreferrer"><code>TextLayout</code></a>, which includes a <a href="http://download.oracle.com/javase/6/docs/api/java/awt/font/FontRenderContext.html" rel="nofollow noreferrer"><code>FontRenderContext</code></a> that can manage the anti-aliasing and fractional metrics settings, as seen in this <a href="https://stackoverflow.com/questions/4287269">example</a>.</p> <p><img src="https://i.stack.imgur.com/YAMGH.png" alt="alt text"></p> <pre><code>import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; /** @see https://stackoverflow.com/questions/4566211 */ public class ScaledJTextPane { private static final int SIZE = 14; private static final String FONT = "Dialog"; private static void createAndShowGUI() { JFrame frame = new JFrame("ScaledJTextPane using BufferedImage"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextPane tp = new JTextPane(); tp.setFont(new Font(FONT, Font.PLAIN, SIZE)); tp.setPreferredSize(new Dimension(400, 300)); StyledDocument doc = tp.getStyledDocument(); Style defaultStyle = StyleContext.getDefaultStyleContext() .getStyle(StyleContext.DEFAULT_STYLE); Style boldStyle = doc.addStyle("bold", defaultStyle); StyleConstants.setBold(boldStyle, true); String boldText = "Four score and seven years ago "; String plainText = "our fathers brought forth on this continent, " + "a new nation, conceived in Liberty, and dedicated to the " + "proposition that all men are created equal."; try { doc.insertString(doc.getLength(), boldText, boldStyle); doc.insertString(doc.getLength(), plainText, defaultStyle); } catch (BadLocationException ble) { ble.printStackTrace(System.err); } final JPanel panel = new JPanel(); panel.add(tp); final JComboBox zoomCombo = new JComboBox(new String[]{ "75%", "100%", "150%", "175%", "200%"}); zoomCombo.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String s = (String) zoomCombo.getSelectedItem(); s = s.substring(0, s.length() - 1); double scale = new Double(s).doubleValue() / 100; int size = (int) (SIZE * scale); tp.setFont(new Font(FONT, Font.PLAIN, size)); } }); zoomCombo.setSelectedItem("100%"); JPanel optionsPanel = new JPanel(); optionsPanel.add(zoomCombo); panel.setBackground(Color.WHITE); frame.add(panel, BorderLayout.CENTER); frame.add(optionsPanel, BorderLayout.NORTH); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createAndShowGUI(); } }); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload