Displaying image in swing

Displaying image in swing

Suppose we want to make the push button to perform some action when clicked by the user. For this purpose, we should add action listener to the button and implement actionPerformed() method. When the button is clicked, this method is executed. So the action part should be written in this method.

Example
import java.awt.*;
import javax.swing.JFrame;
public class test extends Canvas
{
public void paint(Graphics g)
{
Toolkit t=Toolkit.getDefaultToolkit();
Image i=t.getImage(“src/java1.jpg”);
g.drawImage(i, 120,100,this);
}
public static void main(String[] args)
{
test m=new test();
JFrame f=new JFrame();
f.add(m);
f.setSize(400,400);
f.setVisible(true);
}
}

Output:

Untitled

Leave a Reply

Your email address will not be published. Required fields are marked *