import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
public class TextImage {
private JPanel getContent() {
float alpha = 0.4f;
int rule = AlphaComposite.SRC_OVER;
AlphaComposite ac = AlphaComposite.getInstance(rule, alpha);
BufferedImage[] images = getImages();
JPanel panel = new JPanel(new BorderLayout());
panel.setBackground(UIManager.getColor("TextField.background"));
panel.add(getTextField(images[0], ac), "First");
panel.add(getCenterComponent(images[1], ac));
return panel;
}
private JTextField getTextField(final BufferedImage image,
final AlphaComposite ac) {
JTextField textField = new JTextField("Hello world") {
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
int x = (getWidth() - image.getWidth())/2;
int y = (getHeight() - image.getHeight())/2;
Composite orig = g2.getComposite();
g2.setComposite(ac);
g2.drawImage(image, x, y, this);
g2.setComposite(orig);
super.paintComponent(g);
}
};
textField.setHorizontalAlignment(JTextField.CENTER);
Dimension d = textField.getPreferredSize();
d.height = image.getHeight() + 4;
textField.setPreferredSize(d);
textField.setOpaque(false);
return textField;
}
private JScrollPane getCenterComponent(final BufferedImage image,
final AlphaComposite ac) {
JTextArea textArea = new JTextArea();
try {
File file = new File(getClass().getName() + ".java");
Reader reader = new FileReader(file);
textArea.read(reader, file);
reader.close();
} catch(IOException e) {
System.out.println("Read error: " + e.getMessage());
textArea.setText("Hello world");
}
textArea.setOpaque(false);
JScrollPane scrollPane = new JScrollPane();
JViewport viewport = new JViewport() {
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
int x = (getWidth() - image.getWidth())/2;
int y = (getHeight() - image.getHeight())/2;
Composite orig = g2.getComposite();
g2.setComposite(ac);
g2.drawImage(image, x, y, this);
g2.setComposite(orig);
super.paintComponent(g);
}
};
viewport.setOpaque(false);
viewport.setView(textArea);
scrollPane.setViewport(viewport);
scrollPane.setBackground(UIManager.getColor("TextArea.background"));
Dimension dim = scrollPane.getVerticalScrollBar().getPreferredSize();
Insets insets = scrollPane.getInsets();
Dimension d = textArea.getPreferredSize();
d.width += dim.width + insets.left + insets.right;
d.height = 300;
scrollPane.setPreferredSize(d);
return scrollPane;
}
private BufferedImage[] getImages() {
BufferedImage[] images = new BufferedImage[2];
int type = BufferedImage.TYPE_INT_RGB;
int s = 40;
images[0] = new BufferedImage(s,s,type);
Graphics2D g2 = images[0].createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setBackground(UIManager.getColor("TextField.background"));
g2.clearRect(0,0,s,s);
g2.setPaint(Color.blue);
g2.drawRect(0,0,s-1,s-1);
g2.setPaint(Color.red);
g2.drawOval(1,1,s-2,s-2);
g2.setPaint(Color.green.darker());
g2.drawLine(1,1,s-2,s-2);
g2.dispose();
s = 75;
images[1] = new BufferedImage(s,s,type);
g2 = images[1].createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setBackground(UIManager.getColor("TextField.background"));
g2.clearRect(0,0,s,s);
g2.setPaint(Color.red);
g2.drawOval(0,0,s-1,s-1);
g2.setPaint(Color.green.darker());
g2.drawLine(0,s/2,s-1,s/2);
g2.drawLine(s/2,0,s/2,s-1);
g2.dispose();
return images;
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new TextImage().getContent());
f.pack();
f.setLocation(100,100);
f.setVisible(true);
}
}
Try This
Dont forget to change name of the image file.
import java.awt.*;
import java.io.*;
import javax.swing.*;
public class TextImage {
public static void main(String args[])
{
JFrame frm = new JFrame("Text Demo.");
MyText txt = new MyText();
frm.add(new JScrollPane(txt));
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.pack();
frm.setVisible(true);
}
}
class MyText extends JTextArea
{
ImageIcon img = new ImageIcon("Unicorn.jpg");
public MyText()
{
super(20,70);
}
public boolean isOpaque()
{
return false;
}
public void paint(Graphics g)
{
img.paintIcon(this , g, 0,0);
super.paint(g);
}
}