import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageBackground extends JViewport {
public static void main(String[] args) throws IOException {
JTextArea ta = new JTextArea();
ta.setFont(new Font("Lucida Sans Typewriter", Font.BOLD, 12));
ta.setOpaque(false);
ta.setFont(new Font("Monospaced", Font.PLAIN, 12));
ta.setForeground(Color.BLUE);
// TextAreas Farbe Translucent
Color oldCol = ta.getSelectionColor();
Color newCol = new Color(oldCol.getRed(), oldCol.getGreen(), oldCol.getBlue(), 128);
ta.setSelectionColor(newCol);
JScrollPane textScroller = new JScrollPane(ta);
InputStream in = ImageBackground.class
.getResourceAsStream("ImageBackground.java");
ta.read(new InputStreamReader(in), null);
JScrollPane sp = new JScrollPane();
String url = "file:images/pv.gif";
//String url = "file:images/bird.gif";
sp.setViewport(new ImageBackground(ImageIO.read(new URL(url))));
sp.setViewportView(ta);
JFrame f = new JFrame("Farbiger Text auf Hintergrundbild");
f.getContentPane().add(sp);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(500, 400);
f.setLocationRelativeTo(null);
f.show();
}
private BufferedImage image;
public ImageBackground(BufferedImage image) {
this.image = image;
setBackground(Color.WHITE); // hier Hintergrundfarbe anpassen
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = (getWidth() - image.getWidth()) / 2;
int y = (getHeight() - image.getHeight()) / 2;
g.drawImage(image, x, y, null);
}
}