Home  >  Article  >  Java  >  Java Advanced Tutorial: Displaying Pictures

Java Advanced Tutorial: Displaying Pictures

黄舟
黄舟Original
2016-12-27 11:38:341412browse

 The applet can display pictures in GIF, JPEG, BMP and other formats. In order to display an image in an applet, you need to use the drawImage() method of the java.awt.Graphics class.

The following example demonstrates all the steps of displaying images:

import java.applet.*;
import java.awt.*;
import java.net.*;
public class ImageDemo extends Applet
{
  private Image image;
  private AppletContext context;
  public void init()
  {
      context = this.getAppletContext();
      String imageURL = this.getParameter("image");
      if(imageURL == null)
      {
         imageURL = "java.jpg";
      }
      try
      {
         URL url = new URL(this.getDocumentBase(), imageURL);
         image = context.getImage(url);
      }catch(MalformedURLException e)
      {
         e.printStackTrace();
         // Display in browser status bar
         context.showStatus("Could not load image!");
      }
   }
   public void paint(Graphics g)
   {
      context.showStatus("Displaying image");
      g.drawImage(image, 0, 0, 200, 84, null);
      g.drawString("www.javalicense.com", 35, 100);
   } 
}

The above is the Java advanced tutorial: displaying the content of images. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn