by Dinesh Thakur Category: AWT and Applets

In the Following example Draw Image in Applet shows how to Draw Image on Applet window using getDefaultToolkit(),drawImage(), method of Graphics class. To draw the images, we use java.awt.Graphics package comes with a drawImage() of Graphics class. The syntax for drawImage(image, int x, int y);

 

Here is the java code for the program Draw Image in Applet:.

 

import java.applet.Applet;

import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Image;
public class DrawingImage extends Applet
{
             public static void main(String[] args)
           {
          Frame ImageDrawing = new Frame("Draw Image in Applet");
          ImageDrawing.setSize(350, 250);
          Applet DrawingImage = new DrawingImage();
          ImageDrawing.add(DrawingImage);
          ImageDrawing.setVisible(true);
          ImageDrawing.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {System.exit(0); }
                                                                                     });
        }
                 
                 public void paint(Graphics g)
            {
 
                   g.setColor(Color.darkGray);
                  g.setFont(new Font("Arial",Font.BOLD,14));
                  g.drawString("Draw Image in Applet Window Example", 50, 40);
                  g.setFont(new Font("Arial",Font.BOLD,10));
                  g.drawString("http://ecomputernotes.com", 200, 205);
  
/*In our Example Toolkit is a class in the java.awt package that used for the display image. One of the Toolkit methods is getImage() that functions used  to prepare a original
Image source*/
   
                  Toolkit tookit=Toolkit.getDefaultToolkit(); 
      
       //prepare a original Image source
      //The syntax for drawImage(image, int x, int y,);
                 Image image=tookit.getImage("DineshThakur.jpg"); 
                 g.drawImage(image, 130,80,this);  
    
              }
}

 

Draw Image in Applet Window Example

Popular Article