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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display a picture

Status
Not open for further replies.

proteome

Technical User
May 14, 2003
115
US
I am trying to display a picture when a buttonis clicked, how do I do this. My picture is in the form of a .gif. I have tryed looking throught some references but nothing is helping. PLease help, code would be great.

 
In what ? A standalone gui app, a web app ...
 
This would be in a stand alone gui app (JFrame)
 
Override the method paint of your applet.

public void paint(Graphics g)
{

Graphics2D g2;
g2 = (Graphics2D) g;
setBackground(Color.white);
g2.setColor(Color.red);
int centerWidth = 450;
int centerHeight = 600;
Image image = getImage(getDocumentBase(),"test.jpg");

g.drawImage(image, centerWidth, centerHeight, this);
}
//your image file could be any location.Here in this example it should be located on the same directory your applet runs


Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
turkey_clr.gif
 
If you don't want to use an applet ...
Code:
import java.awt.*;
import javax.swing.*;

public class DispTest extends JComponent {

	Image image;

	public DispTest() {
		System.out.println(image);
		image = Toolkit.getDefaultToolkit().getImage("handlerightmiddle.gif");

	}

	public void paint(Graphics g) {

		Graphics2D g2 = (Graphics2D)g;
		g2.drawImage(image, 0, 0, this);
	}

	public static void main (String args[]) {
		JFrame f = new JFrame();
		f.setSize(600, 600);
		f.getContentPane().add(new DispTest());
		f.setVisible(true);
	}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top