I have a JTextPane with an image for its background.
prevWords = new JTextPane()
{
public void paint(Graphics g)
{
BufferedIm
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.