Tuesday, 10 February 2015

Filled Under:

How To Display Image In JFrame Using Canvas In Java

A Canvas in java  is a component displays an empty black area which can be use to display image or to draw any thing on screen.In order to use Canvas we have to use paint method to draw any thing on screen.In this tutorial we use Canvas to display an image in the JFrame.

CODING:

import javax.swing.*;
import java.awt.*;
public class img extends Canvas {
public void paint(Graphics g)
{
Toolkit t = Toolkit.getDefaultToolkit();
Image i = t.getImage("pic.png");
g.drawImage(i,100,50,this);
}

public static void main(String args[]){

JFrame myFrame = new JFrame("This is my frame");

myFrame.setSize(500,400);

myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

myFrame.setVisible(true);
img m = new img();
myFrame.add(m);
}

}

OUTPUT:



0 comments:

Post a Comment