ImageBackground
Man letar lite i sitt utility-bibliotek och hittar följande.
/*
* JImageTextArea.java
*
* Created on den 5 september 2002, 14:07
*/
package lime.graphics.components;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.net.URL;
/** This class is an extension of a JTextArea that has a background image
* in the TextArea.
* @author ovli
*/
public class JImageTextArea extends javax.swing.JTextArea {
Image image;
/** Creates a new instance of JImageTextArea.
* Stupid and empty and should not be used.
*/
public JImageTextArea() {
super();
loadImage("bld.jpg");
setOpaque(false);
}
/** Creates a new instance of JImageTextArea
* @param i The image to display as background.
*/
public JImageTextArea(Image i) {
super();
image = i;
setOpaque(false);
}
/** Creates a new instance of JImageTextArea
* @param i The image to display as background.
* @param opaque If true the component paints every pixel within its bounds.
* Otherwise, the component may not paint some or all of its pixels, allowing the underlying pixels to show through.
*/
public JImageTextArea(Image i, boolean opaque) {
super();
image = i;
setOpaque(opaque);
}
/** Creates a new instance of JImageTextArea
* @param filename The name of the file that is to be uses as background.
* @param opaque If true the component paints every pixel within its bounds.
* Otherwise, the component may not paint some or all of its pixels, allowing the underlying pixels to show through.
*
*/
public JImageTextArea(String filename, boolean opaque) {
super();
loadImage(filename);
setOpaque(opaque);
}
/** Creates a new instance of JImageTextArea
* @param filename The name of the file that is to be used as background.
*/
public JImageTextArea(String filename) {
super();
loadImage(filename);
setOpaque(false);
}
private void loadImage(String filename){
try{
URL fileurl = ClassLoader.getSystemResource(filename);
image = this.getToolkit().getImage(fileurl);
}
catch(Exception e){ e.printStackTrace();}
try {
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 0);
tracker.waitForID(0);
}
catch ( Exception e ) {e.printStackTrace();};
}
/**
* @param g The Graphics obeject.
*/
public void paintComponent(Graphics g) {
g.drawImage(image,0,0,this);
super.paintComponent(g);
}
}
Det finns förbättringspotential... men jag har inte orkat.
/Lime