You are on page 1of 7

ng Frame;

t.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } });

<html> <applet code = ImageDemo width = 150 height = 150> <param name = "image" value = cafe.gif"> </applet> </html>

import java.awt.*; import java.applet.*; public class Simple extends Applet { String str; g.drawImage(img,20,20,this); public void init() { str = "Java is interesting!"; img = getImage(getCodeBase(),cafe.gif"); /* String imagename = getParameter("image"); img = getImage(getCodeBase(),imagename);*/ } public void paint(Graphics g) { g.drawString(str, 70, 80); g.drawImage(img,20,20,this); } } <HTML> <HEAD> <TITLE>The first my Web</TITLE> </HEAD> <APPLET code="Simple.class" width=200 height=200 > </APPLET> </HTML> import java.awt.*; import java.awt.event.*; import java.applet.*; public class Simple extends Applet implements ActionListener {Label lbl1, lbl2, lblKq; TextField txt1, txt2; Button btnAccept, btnThoat; public void init() {this.setLayout(new GridLayout(6,2)); //Chhin th6 dng, 2 ct lbl1 = new Label("Nguoi thu nhat:"); // Nhn thnht this.add(lbl1); txt1 = new TextField(); // vn bn thnht this.add(txt1); lbl2 = new Label("Nguoi thu hai:"); // Nhn thhai this.add(lbl2); txt2 = new TextField(); // vn bn thhai this.add(txt2); lblKq = new Label(); // Nhn kt qu this.add(lblKq); this.add(new Label()); // Cc nt nhn

btnAccept = new Button("Accept"); // Nt Accept btnAccept.addActionListener(this); this.add(btnAccept); btnThoat = new Button("Thoat"); // Nt thot btnThoat.addActionListener(this); this.add(btnThoat); } /* Phng thc xl skin ntc nhn */ public void actionPerformed(ActionEvent ae){ String kq=""; if(ae.getSource() == btnAccept) // Cng hai kq = txt1.getText() + " v " + txt2.getText(); if(ae.getSource() == btnThoat) // Thot khi chng trnh System.exit(0); // Thayi ni dung kt qu lblKq.setText("Chao mung 2 ban " + kq); repaint(); // Vli cci tng} } } } } }

setLayout(new FlowLayout()); setBounds(100,100,300,300); addMouseListener(this); addMouseMotionListener(this); this.setVisible(true); public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { x1 = e.getX(); y1 = e.getY(); public void mouseMoved(MouseEvent e) {} public void mouseReleased(MouseEvent e) { x2 = e.getX(); y2 = e.getY(); repaint(); public void mouseEntered(MouseEvent e) {} public void mouseDragged(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void paint(Graphics g) { g.drawRect(x1, y1, x2-x1, y2-y1); x2 = 0; y2 = 0; } }

import java.awt.*; import java.applet.*; import java.awt.event.*; public class Mousey extends Applet implements MouseListener, MouseMotionListener { int x1, y1, x2, y2; public void init() {

repaint(); } public void mouseReleased(MouseEvent e) { x3 = e.getX(); x4 = e.getY(); repaint(); import java.applet.*; import java.awt.*; import java.awt.event.*; public class Painting extends Applet implements ActionListener, MouseListener { Button bdraw = new Button("Draw Rectangle"); int count = 0,x1,x2,x3,x4; public void init() { BorderLayout border = new BorderLayout(); setLayout(border); add(bdraw, BorderLayout.PAGE_END); bdraw.addActionListener(this); addMouseListener(this); this.setVisible(true); } public void mouseClicked(MouseEvent e) {} public void mousePressed(MouseEvent e) { x1 = e.getX(); x2 = e.getY(); } public void mouseMove(MouseEvent e) { x3 = e.getX(); x4 = e.getY(); } } } public void paint(Graphics g) { if(count == 1) { g.drawRect(x1,x2,(x3-x1),(x4-x2)); x3 = x4 = 0; } } } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if("Draw Rectangle".equals(str)) { count = 1; repaint( );

import java.applet.*; import java.awt.*; public class vehinh extends Applet { int hinh; Button nut; public void init(){ hinh=0; nut= new Button("Hinh tiep: "); add(nut); } public void paint(Graphics g) { int num =5; switch (hinh){ case 0: g.drawLine(35,160, 70, 150); break; case 1: g.drawRect(35,160, 70, 150); break; case 2: g.drawRoundRect(35,160, 70, 150,90,200); break; case 3: g.drawOval(20,50, 170, 250); break; case 4: g.drawArc(35,160, 70, 150,210,150); break; } } public boolean action(Event e, Object o){ ++hinh; if (hinh==5) hinh=0; repaint(); return true; } }

/* Font import java.applet.*; import java.awt.*; public class FontDemo extends Applet { public void paint(Graphics g) { String quote = "Attitude is the minds paintbrush; it can color any situation "; Font objFont = new Font("Georgia",Font.ITALIC,16); g.setFont(objFont); g.drawString(quote,20,20); } }

import java.applet.*; import java.awt.*; public class TextCentre extends Applet { public void paint(Graphics g) { String myquote = "Happiness is an attitude."; Font objFont = new Font("Times New Roman" , Font.BOLD|Font.ITALIC , 24); FontMetrics fm = getFontMetrics(objFont); g.setFont(objFont); int numx = (getSize().width m.stringWidth(myquote))/2; int numy = getSize().height/2; g.drawString(myquote,numx,numy); } }

=============Wai, notify==========

} }; Runnable b = new Runnable() { @Override public void run() { System.out.println("BBBBB"); } }; Runnable c = new Runnable() { @Override public void run() { System.out.println("CCCCCC" ); } }; Thread ta = new Thread(a, "threadA"); ta.start(); Thread.sleep(1000); Thread tb = new Thread(b, "threadB"); tb.start(); Thread.sleep(1000); Thread tc = new Thread(c, "threadC"); tc.start(); Thread.sleep(1000); System.out.println("This is the main thread"); } } ============synchronized======= public class ex { public static void main(String[] args) throws Exception { final ex tus = new ex(); Runnable runA = new Runnable() { public void run() { tus.doAction(3); } }; Thread ta = new Thread(runA, "threadA"); ta.start(); Thread.sleep(2000);

public class Wait_Noti extends Thread { public static void main(String args[]) throws Exception { Thread notiThread = new Wait_Noti(); notiThread.start(); synchronized (notiThread) { notiThread.wait(7000); } System.out.println("The wait is over"); } public void run() { System.out.println(" Click enter to stop waiting thread"); try { System.in.read(); System.out.println("Stop"); } catch (java.io.IOException ioe) { System.out.println(" Over"); } synchronized (this) { this.notifyAll(); } } }
=========Not Runnable==============

public class ex { public static void main(String[] args) throws InterruptedException { Runnable a = new Runnable() { @Override public void run() { System.out.println("AAAAA") ;

Runnable runB = new Runnable() { public void run() { tus.doAction(7); } }; Thread tb = new Thread(runB, "threadB"); tb.start(); } public synchronized void doAction(int val) { int taskID; String thrName = Thread.currentThread().getName() ; System.out.println(thrName + ":entering doAction()"); taskID = val; System.out.println(thrName + ":doAction() " + taskID); try { Thread.sleep(1000); } catch (InterruptedException x) { } System.out.println(thrName + ": doAction() sleep after taskID "+ taskID); System.out.println(thrName + ": leaving performATask() " + taskID); } } public class Synch_ex { public class Product { private int view = 0; public boolean isRemainingview; public synchronized int get() { while (isRemainingview == false) { try { wait(); } catch (InterruptedException e) { } } isRemainingview = false;

System.out.println("\tConsumer get " + this.view); notifyAll(); return view; } public synchronized void set(int piview) { while (isRemainingview == true) { try { wait(); } catch (InterruptedException e) { } } this.view = piview; isRemainingview = true; System.out.println("Producer set " + this.view); notifyAll(); } } public class Producer extends Thread { private Product sp; Producer(Product pProduct) { sp = pProduct; } public void run() { for (int i = 0; i < 10; i++) { sp.set(i); try { Thread.sleep(500); } catch (InterruptedException e) { } } } } public class Consumer extends Thread { private Product sp; Consumer(Product pProduct) { sp = pProduct; }

public void run() { for (int i = 0; i < 10; i++) { sp.get(); try { Thread.sleep(500); } catch (InterruptedException e) { } } } } public static void main(String[] args) { Synch_ex t = new Synch_ex(); Product s = t.new Product(); Producer p = t.new Producer(s); Consumer c = t.new Consumer(s); p.start(); c.start(); } } =======Interrupt========== public class ex extends Thread { public void run() { System.out.println("I feel sleepy. Wake me in eight hours"); try { Thread.sleep(1000); System.out.println("That was a nice nap"); } catch (InterruptedException ie) { System.err.println("Just five more minutes...."); } } public static void main(String args[]) throws java.io.IOException { Thread sleepy = new ex(); sleepy.start(); System.out.println("Press enter to interrupt the thread"); System.in.read(); sleepy.interrupt(); } }

You might also like