簡體   English   中英

第2部分 - 如何在縮放JTextPane時獲得一致的渲染?

[英]Part 2 - How do I get consistent rendering when scaling a JTextPane?

我之前提交了此問題的另一個版本和示例程序: 在縮放JTextPane時如何獲得一致的渲染?

重述問題:我想允許用戶放大或縮小不可編輯的JTextPane。 運行在早期問題中提交的示例程序(簡單地縮放了Graphics對象),導致粗體文本和非粗體文本之間的間距不一致。

下面的示例程序嘗試通過將文本窗格繪制為100%的BufferedImage然后縮放圖像來解決問題。 這解決了間距不一致的問題,但結果文本缺乏清晰度。 是否有一些渲染提示(或其他一些變化)的組合會產生漂亮的文字?

提前感謝對此方法可行性的任何建議或意見。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class ScaledJTextPane extends JTextPane
{
    double scale_;
    BufferedImage raster_;

    public ScaledJTextPane()
    {
        scale_ = 1.0;
        raster_ = null;
    }

    public void draw(Graphics g)
    {
        if (raster_ == null)
        {
            // Draw this text pane to a BufferedImage at 100%
            raster_ = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = raster_.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);

            paint(g2);
        }

        Graphics2D g2 = (Graphics2D) g;

        // Experiment with different rendering hints
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g2.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        // Scale the BufferedImage            
        g2.scale(scale_, scale_);
        g2.drawImage(raster_, 0, 0, null);
    }

    public void setScale(double scale)
    {
        scale_ = scale;
        raster_ = null;
    }

    private static void createAndShowGUI() 
    {
        // Create and set up the window.
        JFrame frame = new JFrame("ScaledJTextPane using BufferedImage");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final ScaledJTextPane scaledTextPane = new ScaledJTextPane();
        StyledDocument doc = scaledTextPane.getStyledDocument();
        Style defaultStyle = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
        Style boldStyle = doc.addStyle("bold", defaultStyle);
        StyleConstants.setBold(boldStyle, true);

        scaledTextPane.setFont(new Font("Dialog", Font.PLAIN, 14));
        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) 
        {
            System.err.println("Couldn't insert text into text pane.");
        }

        final JComboBox zoomCombo=new JComboBox(new String[] {"75%",
                "100%", "150%", "175%", "200%"});
        final JPanel panel = new JPanel()
        {
            protected void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                scaledTextPane.draw(g);
            }
        };
        zoomCombo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String s = (String) zoomCombo.getSelectedItem();
                s = s.substring(0, s.length() - 1);
                double scale = new Double(s).doubleValue() / 100;
                scaledTextPane.setScale(scale);
                panel.invalidate();
                panel.repaint();
            }
        });
        zoomCombo.setSelectedItem("100%");

        JPanel optionsPanel = new JPanel(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.WEST;

        optionsPanel.add(zoomCombo, c);

        c.gridx++;
        c.weightx = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        optionsPanel.add(Box.createHorizontalGlue(), c);

        // Add content to the window.
        scaledTextPane.setBounds(0, 0, 450, 300);
        panel.setOpaque(true);
        panel.setBackground(Color.WHITE);
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        frame.getContentPane().add(optionsPanel, BorderLayout.NORTH);
        frame.setSize(900, 300);

        //Display the window.
        frame.setVisible(true);
    }

    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() 
            {
                createAndShowGUI();
            }
        });
    }
}

遺憾的是,從固定分辨率縮放到更大的尺寸將始終導致一些混疊偽像。 這是另一種縮放JTextPane使用的字體的方法。

對於低級別控制,請考慮TextLayout ,其中包含可以管理抗鋸齒和小數度量設置的FontRenderContext ,如本示例所示

替代文字

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();
            }
        });
    }
}

可能是這個http://java-sl.com/Scale_In_JEditorPane.html可以提供幫助。

我想允許用戶放大或縮小不可編輯的JTextPane。

由於文本窗格不可編輯,因此您可以使用Screen Image類創建文本窗格的圖像 然后,您可以使用適當的縮放系數在面板上繪制圖像。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM