Discussing how to display images in Application

Source: Internet
Author: User

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 );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.