将文本写入带有图像背景的JTextPane [英] Write text into a JTextPane with an image background

查看:114
本文介绍了将文本写入带有图像背景的JTextPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有背景图像的JTextPane。

I have a JTextPane with an image for its background.

prevWords = new JTextPane()
    {
        public void paint(Graphics g)
        {
            BufferedImage img;
            try 
            {
                img = ImageIO.read(new File("Images/logo.png"));
                img.getGraphics().setColor(new Color(Color.TRANSLUCENT));
                g.drawImage(img, 0, 0, null);
            } 
            catch (IOException e) 
            {
                System.out.println("Failed to load logo.");
            }
            super.paintComponents(g);
        }
    };

当我将文字写入窗格时,我看不到它。我已将窗格中的文本设置为白色。

When I write text to the the pane, it I cannot see it. I have set the text in pane to be white as well.

推荐答案

这是一个完整的黑客。

这里的问题是,用户界面正在绘制背景两次 ......

The problem here is, the UI is painting the background twice...

你需要规避UI以这样的方式使您可以将图像绘制到背景中,同时仍然可以将文本渲染到顶部。

You need to circumvent the UI in such a way so that you can paint the image into the background while still getting the text to render over the top.

最后,我必须制作文本窗格透明,因此我可以强制UI不要绘制背景。

In the end, I had to make the text pane transparent so I could force the UI not to paint the background.

public class TextPaneBackground {

    public static void main(String[] args) {
        new TextPaneBackground();
    }

    public TextPaneBackground() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(new TextPaneWithBackground()));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TextPaneWithBackground extends JTextPane {

        private BufferedImage background;

        public TextPaneWithBackground() {
            try {
                background = ImageIO.read(new File("C:/Users/shane/Dropbox/MegaTokyo/Evil_Small.jpg"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            setForeground(Color.WHITE);
            setOpaque(false);
        }

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return background == null ? super.getPreferredScrollableViewportSize() : new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g.create();

            if (isOpaque()) {
                g2d.setColor(getBackground());
                g2d.fillRect(0, 0, getWidth(), getHeight());
            }

            if (background != null) {
                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight()- background.getHeight()) / 2;
                g2d.drawImage(background, x, y, this);
            }

            getUI().paint(g2d, this);
            g2d.dispose();
        }
    }
}

Reimeus暗示能够直接将图像插入文档,这可能是一个更好的长期解决方案。

Reimeus hinted at the ability to insert an image into the Document directly, this might be a better, long term solution.

这篇关于将文本写入带有图像背景的JTextPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆