Write text into a JTextPane with an image background

旧时模样 提交于 2020-01-10 06:07:52

问题


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.


回答1:


This is a complete hack.

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

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.

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 hinted at the ability to insert an image into the Document directly, this might be a better, long term solution.



来源:https://stackoverflow.com/questions/14761598/write-text-into-a-jtextpane-with-an-image-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!