You are on page 1of 7

VE DUONF TRON import java.awt.*; import java.applet.*; import java.awt.event.

*; public class VD13 extends Applet { DieuKhien dk; // lop con panel Ve ve; // lop con cua lp Canvans public void init() { setLayout(new BorderLayout()); ve =new Ve(0); add("Center", ve); add("South", dk = new DieuKhien(ve)); } public void destroy() { remove(dk); remove(ve); } public void start() { dk.setEnabled(true); } public void stop() { dk.setEnabled(false); } public void processEvent(AWTEvent e) { if (e.getID() == Event.WINDOW_DESTROY) { System.exit(0); } } public static void main(String s[]){ VD13 cs = new VD13(); cs.init(); Frame f= new Frame(" VE DUONG TRON"); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0);}}); f.setSize(400,400); f.add("Center",cs); f.show(); } } class Ve extends Canvas implements MouseListener, MouseMotionListener { int loaihinh=0; VeHinh h; Point p=new Point(0,0); Point p1=new Point(0,0); int m=1;

public Ve(int l){ addMouseListener(this); } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { addMouseMotionListener(this); p.x = e.getX(); p.y = e.getY(); repaint(); e.consume(); } public void mouseReleased(MouseEvent e) { // removeMouseMotionListener(this); // p1.x = e.getX(); // p1.y = e.getY(); // // } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { m=2; p1.x = e.getX(); p1.y = e.getY(); repaint(); e.consume(); } public void mouseMoved(MouseEvent e) { m=1; p.x=0;p.y=0;p1.x=0;p1.y=0; } repaint(); e.consume();

public void paint(Graphics g) { //if(m==1) {h= new Tron(p.x, p.y,2); h.draw(g);} if ((m==2)&& (loaihinh==2)) { h= new Tron(p.x, p.y,(int) (Math.sqrt((p.x-p1.x)*(p.x-p1.x)+(p.y-p1.y)*(p.y-p1.y)))); h.draw(g); }

} } class DieuKhien extends Panel implements ActionListener { Ve v; int hinh; public DieuKhien(Ve d) { v=d; Button b1 = new Button ("Duong tron"); Button b5 = new Button ("Thoat"); add(b1); add(b5); b1.addActionListener(this); b5.addActionListener(this); b1.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { v.loaihinh=2; v.repaint(); } } ); b5.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } } );

} public void actionPerformed(ActionEvent e){ }

} abstract class VeHinh { static int loaihinh; abstract void draw(Graphics g); } class Tron extends VeHinh { public int x,y,r; public Tron (int x, int y,int r){ this.x=x; this.y=y; this.r=r; } public void draw(Graphics g) {

g.setColor(Color.red); g.drawArc(x-r,y-r,r*2,r*2,0,360); } } VE HINH // ve hinh tron, ve tiep import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.*;

public class VD13_1 extends Applet { DieuKhienVe dk; // lop con panel DoHoa ve; // lop con cua lp Canvans public void init() { setLayout(new BorderLayout()); DoHoa ve =new DoHoa(); add("Center", ve); add("South", dk = new DieuKhienVe(ve)); } public void destroy() { remove(dk); remove(ve); } public void start() { dk.setEnabled(true); } public void stop() { dk.setEnabled(false); } public void processEvent(AWTEvent e) { if (e.getID() == Event.WINDOW_DESTROY) { System.exit(0); } } public static void main(String s[]){ VD13_1 cs = new VD13_1(); cs.init(); Frame f= new Frame(" DEN GIAO THONG"); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0);}});

f.setSize(400,400); f.add("Center",cs); f.show(); } } class DoHoa extends Canvas implements MouseListener, MouseMotionListener { int loaiHinh; VeHinh h; Point pDau,pCuoi; Vector hinh = new Vector(); public DoHoa(){ addMouseListener(this); } public void set_LoaiHinh( int lh){ loaiHinh=lh; } public void clear(){ hinh.removeAllElements(); repaint(); } public void undo(){ if (hinh.size()>0) { hinh.setSize(hinh.size()-1); repaint(); } } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { addMouseMotionListener(this); pDau=e.getPoint(); hinh.addElement(pDau); e.consume(); } public void mouseDragged(MouseEvent e){ pCuoi=e.getPoint(); switch(loaiHinh) { case 1: hinh.setElementAt(new DoanThang(pDau, pCuoi),hinh.size()-1); break; case 2: hinh.setElementAt(new ChuNhat(pDau,Math.abs(pDau.xpCuoi.x),Math.abs(pDau.y-pCuoi.y)),hinh.size()-1); break; case 3: int r = (int)(Math.sqrt((pDau.x-pCuoi.x)*(pDau.x-pCuoi.x)+(pDau.ypCuoi.y)*(pDau.y-pCuoi.y))); hinh.setElementAt(new Tron(pDau.x, pDau.y,r),hinh.size()-1); break; } repaint();

e.consume(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } public void paint(Graphics g) { g.setColor(Color.red); for(int i=0; i<hinh.size();++i){ ((Hinh) hinh.elementAt(i)).draw(g);} g.drawString(" "+hinh.size(),200,200); } } class DieuKhienVe extends Panel implements ActionListener { DoHoa v; Button b1,b2,b3,b4,b5,b6; public DieuKhienVe(DoHoa d) { v=d; b1 = new Button ("Duong Thang"); add(b1); b1.addActionListener(this); b2 = new Button ("Chu nhat"); add(b2); b2.addActionListener(this); b3 = new Button ("Hinh tron"); add(b3); b3.addActionListener(this); b4 = new Button ("Undo"); add(b4); b4.addActionListener(this); b5 = new Button ("clear"); add(b5); b5.addActionListener(this); b6 = new Button ("Exit"); add(b6); b6.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource()==b1) { v.set_LoaiHinh(1); } else if(e.getSource()==b2) { v.set_LoaiHinh(2);

} else if(e.getSource()==b3) { v.set_LoaiHinh(3); }else if(e.getSource()==b4){ v.undo(); }else if(e.getSource()==b5){ v.clear(); }else if(e.getSource()==b6){ System.exit(0);} } }

abstract class Hinh { static int loaihinh; abstract void draw(Graphics g); } class DoanThang extends Hinh { public Point pd,pc; public DoanThang (Point x, Point y){ pd=x; pc=y; } public void draw(Graphics g) { g.drawLine(pd.x,pd.y,pc.x,pc.y); } } class ChuNhat extends Hinh { public Point p; public int d,r; public ChuNhat (Point p,int x, int y){ this.p=p; d=x; r=y; } public void draw(Graphics g) { g.drawRect(p.x,p.y,d,r); } } class Tron extends Hinh { public int x,y,r; public Tron (int x, int y,int r){ this.x=x; this.y=y; this.r=r; } public void draw(Graphics g) { g.drawArc(x-r,y-r,r*2,r*2,0,360); } }

You might also like