How to display images in Java and how to display images in Java

Source: Internet
Author: User

How to display images in Java and how to display images in Java

Recently, I am working on a small swing project. I need to display the image files stored in the hard disk and summarize the following methods:

1.

Graphics g = getGraphics ();
String name = "E:/CapabilityModel/out.gif ";
Image img = Toolkit. getDefaultToolkit (). getImage (name );
G. drawImage (img, 538,408,585,305, null );
G. dispose ();

This method selects a certain amount of space on the interface to display the image. The image size cannot be automatically matched. If the image is too large, the image will be blurred, the displayed image cannot be updated in real time based on the changes in the image file. The example is shown below:

 

2.

JLabel imgLabel = new JLabel (new ImageIcon ("D:/AGVsModel/temp/out.gif "));
SetTitle ("ShowImage ");
JPanel cp = (JPanel) this. getContentPane ();
JPanel imgPanel = new JPanel ();
ImgPanel. add (imgLabel );

Cp. add (imgPanel, BorderLayout. CENTER );
This. setSize (950,320 );
This. setVisible (true );
This. setdefaclocloseoperation (JDialog. DISPOSE_ON_CLOSE );

This method is to bring up a dialog box to display the image, but it cannot be changed according to the content of the image file to display the image in real time. Later, Baidu changed the first line of code:

JLabel imgLabel = null;
Try {
ImgLabel = new JLabel (new ImageIcon (ImageIO. read (new File ("E:/CapabilityModel/out.gif "))));
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}

In this way, the desired result is reached... The following figure shows an example:

3.

Protected Shell;
Public static Display myDisplay;
Public static boolean internalCall = false;

/**
* Open the window.
*/
Public void open (){
Display display = Display. getDefault ();
CreateContents (myDisplay );
Image img = new Image (display, "E:/CapabilityModel/out.gif ");
Shell. open ();
Center (display, shell );
GC gc = new GC (shell );
Gc. drawImage (img, 0, 0 );
Shell. layout ();
While (! Shell. isDisposed ()){
If (! Display. readAndDispatch ()){
Display. sleep ();
}
}
Img. dispose ();
If (internalCall) display. dispose ();
}

/**
* Create contents of the window.
*/
Protected void createContents (Display display ){
MyDisplay = display;
Shell = new Shell ();
Shell. setSize (900,400 );
Shell. setText ("Show Image ");
}
Public static void center (Display display, Shell shell)
{
Rectangle bounds = display. getPrimaryMonitor (). getBounds ();
Rectangle rect = shell. getBounds ();
Int x = bounds. x + (bounds. width-rect. width)/2;
Int y = bounds. y + (bounds. height-rect. height)/2;
Shell. setLocation (x, y );
}

This method is quite desirable. You can update and display images in real time, but the pop-up box is always displayed behind the interface, and the images cannot be displayed when the project is exported to a jar file for running, then I gave up.

Writing code is to constantly encounter problems, solve the problems, and summarize and transform the experience into your own. Every time you solve a problem, you will feel a sense of accomplishment.

 

 

 

 

 

 

 

 

 

Related Article

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.