Inserting a picture into program

Using the Java SDK with TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
sh00ter555
Posts: 3
Joined: Sat May 15, 2004 12:55 am

Inserting a picture into program

Post by sh00ter555 »

Hi, my name is jason and i just started to use this forum. I was wondering if someone can help me out in adding a background picture to my program. I have a picture that i have saved on my computer called "tablePicture.gif" and was wondering how i add this picture to my code so it is visable when i run the program.
so far i have done this:
I made sure that "java.awt.*" was imported at the top of the program.
then I made a "private Image tablePicture"
i think that is right so far, but now im stuck.

PS: im assuming that i need a .getImage or something, but im not sure. If anyone would be so kind to help me out it would be greatly appriciated.
Thank you,
Jason
User avatar
talleyrand
Posts: 624
Joined: Mon Jul 21, 2003 6:56 pm
Location: Kansas City, MO, USA
Contact:

Post by talleyrand »

Liberated from JGuru because I don't do GUIs...

Code: Select all

import javax.swing.*;
import java.awt.*;

public class BackgroundSample {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Background Example");
    final ImageIcon imageIcon = new ImageIcon("tablePicture.gif");
    JTextArea textArea = new JTextArea() {
      Image image = imageIcon.getImage();
      Image grayImage = GrayFilter.createDisabledImage(image);
      {setOpaque(false);}  // instance initializer
      public void paintComponent (Graphics g) {
        g.drawImage(grayImage, 0, 0, this);
        super.paintComponent(g);
      }
    };
    JScrollPane scrollPane = new JScrollPane(textArea);
    Container content = frame.getContentPane();
    content.add(scrollPane, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(3);
    frame.setSize(250, 250);
    frame.setVisible(true);
  }
}
I choose to fight with a sack of angry cats.
Post Reply