All for Joomla All for Webmasters

Displaying image in swing

For displaying image, we can use the method drawImage() of Graphics class.

Syntax of drawImage() method:

  1. public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image.
  1. import java.awt.*;
  2. import javax.swing.JFrame;
  3. public class MyCanvas extends Canvas{
  4.     public void paint(Graphics g) {
  5.         Toolkit t=Toolkit.getDefaultToolkit();
  6.         Image i=t.getImage(“p3.gif”);
  7.         g.drawImage(i, 120,100,this);
  8.     }
  9.         public static void main(String[] args) {
  10.         MyCanvas m=new MyCanvas();
  11.         JFrame f=new JFrame();
  12.         f.add(m);
  13.         f.setSize(400,400);
  14.         f.setVisible(true);
  15.     }
  16. }
PinIt
submit to reddit

Leave a Reply

Top