Java Study Notes: [getting image resources from the network] Implementation

Source: Internet
Author: User

JAVA accesses network resources

The following uses the java url class to obtain images from the Internet.

 

1 // obtain image resources from the network
2 // InternetAccess. java
3 import java. awt .*;
4 // import java. awt. event .*;
5 import javax. swing .*;
6 import java.net .*;
7
8. public class InternetAccess
9 {
10 public static void main (String [] args)
11 {
12 ImageFrame frame = new ImageFrame ();
13 frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
14 // frame. show (); // The show method is outdated.
15 frame. setVisible (true );
16}
17}
18
19 class ImageFrame extends JFrame
20 {
21 /**
22 *
23 */
24 private static final long serialVersionUID = 1L;
25 public static final int WIDTH = 300;
26 public static final int HEIGHT = 120;
27
28 public ImageFrame ()
29 {
30 setTitle ("InternetAccess ");
31 setSize (WIDTH, HEIGHT );
32
33 ImagePanel panel = new ImagePanel ();
34 Container contentPane = getContentPane ();
35 contentPane. add (panel );
36}
37}
38
39 class ImagePanel extends JPanel
40 {
41 /**
42 *
43 */
44 private static final long serialVersionUID = 1L;
45 private Image image;
46 URL;
47
48 public ImagePanel ()
49 {
50 try
51 {
52 // specify the URL of the resource to be obtained
53 url = new URL ("http://www.kklinux.com/uploads/090313/2_204213_1.jpg ");
54}
55 catch (MalformedURLException e)
56 {
57
58}
59 // obtain the image on the specified URL
60 image = Toolkit. getdefatooltoolkit (). getImage (url );
61}
62
63 public void paintComponent (Graphics g)
64 {
65 super. paintComponent (g );
66
67 int imageWidth = image. getWidth (this );
68 int imageHeight = image. getHeight (this );
69
70 // display the image in the window
71g. drawImage (image, 0, 0, imageWidth, imageHeight, null );
72g. drawImage (image, 0, 0, null );
73 // display string
74g. drawString ("downloading image...", 100, 80 );
75}
76}

Running result:

 

When the window is displayed, a string is displayed before the image is displayed. This result is in the opposite order of programming. The reason is that JAVA uses a multithreading mechanism. Because image downloading is a time-consuming operation, while the display string is a local operation. If multithreading is not applicable, you must wait until the image download is complete before the string is displayed, in this way, the user may not know what the program is doing while waiting for the image to be downloaded. Therefore, the multi-thread mechanism is adopted, and the program does not have to wait for the download operation all the time, but can run multiple operations simultaneously, which makes the program highly user-friendly.

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.