Discussing how to display images in Application
**************************************** *********************
**
** Author: Liu xiao------------------- one small step in one day
** Date: 2000-12-20 ------------ ==================
** Jeru@163.net ----------------- enrich my every day
**
**************************************** *********************
The Toolkit class is used to display images in the application.
Here I found a simple way to get an Image object from an ImageIcon object using the getImage () method.
The procedure is as follows:
// Obtain the image file path
// The getResource () method will automatically find your image file in CLASSPATH. This is a good method.
// Even if your image file is in the jar package, we can easily find it
URL imgURL = getClass (). getResource ("img/test.gif ");
// Create an ImageIcon class
ImageIcon icon = new ImageIcon (imgURL );
// Obtain the img from the icon
Image img = icon. getImage ();
In this way, the code of my previous article <discussing how to display images in Applet> can be displayed in application with a slight change.
The attached routine is as follows:
Import javax. swing .*;
Import java. awt .*;
Import java.net. URL;
Import java. awt. image .*;
Public class MyFrame extends JFrame {
Int xpoint = 100, ypoint = 100;
Public MyFrame (){
// Do frame stuff.
Super ("MyFrame ");
}
Public void paint (Graphics g ){
URL imgURL = getClass (). getResource ("img/test.gif ");
ImageIcon icon = new ImageIcon (imgURL );
G. drawImage (icon. getImage (), xpoint, ypoint, this );
}
// Main function
Public static void main (String [] args ){
MyFrame frame = new MyFrame ();
Frame. pack ();
Frame. setVisible (true );
}
}