Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add text to image

Status
Not open for further replies.

jaredgalen

Programmer
May 28, 2003
34
LT
I want to superimpose some text over an image.
How would I go about doing this in java?
Is it possible?
Appreciate any help.

JG
 
Check out the Java Advanced Imaging API (JAI)
 
One possible way to do this :
Code:
import java.awt.*;
import javax.swing.*;

public class ImageExample {

  private Image image;

  public static void main(String[] args) {

    ImageExample example = new ImageExample();

    example.doIt();
  }

  public void doIt() {
    MyFrame f = new MyFrame("Text over image");
    getImage();
    f.setSize(500,500);
    f.setVisible(true);
  }

  public Image getImage() {
    if (image == null) {
      try {
        image = ImageLoader.loadImage("D:/images/splash.jpg");
      } catch (InterruptedException e) {
      }
    }
    return image;
  }

  public class MyFrame extends JFrame {

    public MyFrame(String title) {
      super(title);
    }

    public void paint(Graphics g) {
      super.paint(g);
      g.drawImage(image, 10,10,null);
      g.setColor(Color.CYAN);
      g.drawString("text", 100, 100);
     }
    }

}
=========================================
Code:
public class ImageLoader extends Component {

  private static ImageLoader imageLoader;

  static {
    imageLoader = new ImageLoader();
  }

  private ImageLoader() {
    super();
  }

  public static Image loadImage(String imageName) throws InterruptedException {
    Image image = Toolkit.getDefaultToolkit().getImage(imageName);
    MediaTracker tracker = new MediaTracker(imageLoader);
    tracker.addImage(image,0);
    tracker.waitForID(0);
    return image;
  }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top