application |
import
java.awt.*; import java.awt.event.*; import javax.swing.*; public class Ex1 { public static void main (String[] arg){ JFrame f = new JFrame("Ex 1"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyCanvas cs = new MyCanvas(); f.add("Center",cs); f.setSize(250,350); cs.setBackground(Color.yellow); cs.setForeground(Color.BLUE); cs.setFont(new Font("TimesRoman",Font.ITALIC,24)); cs.repaint(); f.setVisible(true); } } class MyCanvas extends Canvas{ int x=50,y=70; int iw=100,ih=100; Image img=Toolkit.getDefaultToolkit().getImage("1_MAGIC.JPG"); public void paint (Graphics g){ g.drawString("This an image", 20, 20) ; g.drawImage(img, iw, ih,120,120, this); } } |
application with mouse |
import
java.awt.*; import java.awt.event.*; import javax.swing.*; public class Ex1 { static Image img=Toolkit.getDefaultToolkit().getImage("1_MAGIC.JPG"); static MyCanvas cs = new MyCanvas(); public static void main (String[] arg){ JFrame f = new JFrame("Ex 1"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); cs.addMouseListener(new MyMouse()); f.add(cs); f.setSize(250,350); cs.setBackground(Color.yellow); cs.setForeground(Color.BLUE); cs.setFont(new Font("TimesRoman",Font.ITALIC,24)); cs.repaint(); f.setVisible(true); } static public class MyMouse extends MouseAdapter { public void mouseClicked(MouseEvent ev) { cs.x=ev.getX(); cs.y=ev.getY(); cs.repaint(); System.out.println("x="+ev.getX() + " y="+ ev.getY()); } } static class MyCanvas extends Canvas{ int x=50,y=70; public void paint (Graphics g){ g.drawString("This an image", 20, 20) ; g.drawImage(img, x, y,120,120, this); } } } |
Applet |
import
java.awt.*; import java.applet.*; public class Ex extends Applet { Image img; int iw=50,ih=50; public void init(){ img = getImage(getCodeBase(), "images/1_MAGIC.JPG"); setSize(250,350); setBackground(Color.yellow); setForeground(Color.BLUE); setFont(new Font("TimesRoman",Font.ITALIC,24)); } public void paint (Graphics g){ g.drawString("This is an image", 20, 20) ; g.drawImage(img, iw, ih,120,120, this); } } |