0

背景の画像を持つ JTextPane があります。

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

ペインにテキストを書き込むと、テキストが表示されません。ペインのテキストも白に設定しました。

4

1 に答える 1

1

これは完全なハックです。

ここでの問題は、UI が背景​​を 2 回ペイントしていることです...

画像を背景にペイントしながら、テキストを上にレンダリングできるように、UI を回避する必要があります。

最後に、UI が背景​​をペイントしないように強制できるように、テキスト ペインを透明にする必要がありました。

ここに画像の説明を入力

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 は、イメージを に直接挿入する機能をほのめかしましたDocument。これは、より優れた長期的な解決策になる可能性があります。

于 2013-02-07T23:43:32.950 に答える