/* Une classe heritant de Frame pour implanter */ /* la fenetre de l'application de test */ /* de l'algorithme de Cohen-Sutherland */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Novembre 2009 */ import java.awt.*; import java.awt.event.*; public class CSFrame extends Frame implements WindowListener, ActionListener, MouseListener, MouseMotionListener { private MenuBar mb = new MenuBar(); private Menu m1 = new Menu("Fichier"); private MenuItem mq = new MenuItem("Quitter"); private Position2D pi = new Position2D(50.0F,70.0F); private Position2D pf = new Position2D(270.0F,200.0F); public CSFrame(String s) { super(); setTitle(s); addWindowListener(this); addMouseListener(this); addMouseMotionListener(this); setMenuBar(mb); mb.add(m1); m1.add(mq); mq.addActionListener(this); } public void paint(Graphics g) { Segment s = new Segment(pi,pf); Rectangle r = new Rectangle(new Position2D(80.0F,110.0F), new Position2D(250.0F,170.0F)); g.drawLine((int) s.pi.x,(int) s.pi.y, (int) s.pf.x,(int) s.pf.y); g.drawRect((int) r.ig.x,(int) r.ig.y, (int) (r.sd.x-r.ig.x),(int) (r.sd.y-r.ig.y)); Segment sc = r.clip(s); if ( sc != null ) { g.setColor(new Color(0.0F,0.0F,1.0F)); g.drawLine((int) sc.pi.x,(int) sc.pi.y, (int) sc.pf.x,(int) sc.pf.y); } } public void windowActivated(WindowEvent e) { } public void windowClosed(WindowEvent e) { } public void windowClosing(WindowEvent e) { quitter(); } public void windowDeactivated(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void quitter() { System.exit(0); } public void actionPerformed(ActionEvent e) { if ( e.getSource() == mq ) { quitter(); } } public void mouseClicked(MouseEvent e) { if ( e.getButton() == MouseEvent.BUTTON1 ) { pi = new Position2D(e.getX(),e.getY()); repaint(); } if ( e.getButton() == MouseEvent.BUTTON3 ) { pf = new Position2D(e.getX(),e.getY()); repaint(); } } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { if ( e.getButton() == MouseEvent.BUTTON1 ) { pi = new Position2D(e.getX(),e.getY()); repaint(); } if ( e.getButton() == MouseEvent.BUTTON3 ) { pf = new Position2D(e.getX(),e.getY()); repaint(); } } public void mouseReleased(MouseEvent e) { } public void mouseDragged(MouseEvent e) { if ( (e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0 ) { pi = new Position2D(e.getX(),e.getY()); repaint(); } if ( (e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0 ) { pf = new Position2D(e.getX(),e.getY()); repaint(); } } public void mouseMoved(MouseEvent e) { } }