Bài giảng An Introduction to Computer Science Using Java - Chapter 11 Java AWT Part I: Mouse Events (Optional)

Tài liệu Bài giảng An Introduction to Computer Science Using Java - Chapter 11 Java AWT Part I: Mouse Events (Optional): Chapter 11 Java AWT Part I: Mouse Events (Optional)Lecture Slides to AccompanyAn Introduction to Computer Science Using Java (2nd Edition)byS.N. Kamin, D. Mickunas, E. ReingoldChapter PreviewIn this chapter we will:introduce event driven programmingshow how a program can respond to mouse events (e.g. clicks and mouse movements)demonstrate how to implement a listener interfaceshow how mouse events can be used to build highly interactive programsAbstract Windowing Toolkit (AWT)Part of the Java distribution, but not part of the Java language itselfAWT is library of classes and methods used by Java programmers that supported are by the basic Java runtime librariesEven though this chapter is labeled optional, it is not possible to be a complete Java programmer without understanding the AWT Java EventsEvents are occurrences outside of the program to which the program must respond (e.g. user key press)Event managers are methods that first catch the eventListener classes send messages to an ev...

ppt11 trang | Chia sẻ: honghanh66 | Lượt xem: 806 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Bài giảng An Introduction to Computer Science Using Java - Chapter 11 Java AWT Part I: Mouse Events (Optional), để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Chapter 11 Java AWT Part I: Mouse Events (Optional)Lecture Slides to AccompanyAn Introduction to Computer Science Using Java (2nd Edition)byS.N. Kamin, D. Mickunas, E. ReingoldChapter PreviewIn this chapter we will:introduce event driven programmingshow how a program can respond to mouse events (e.g. clicks and mouse movements)demonstrate how to implement a listener interfaceshow how mouse events can be used to build highly interactive programsAbstract Windowing Toolkit (AWT)Part of the Java distribution, but not part of the Java language itselfAWT is library of classes and methods used by Java programmers that supported are by the basic Java runtime librariesEven though this chapter is labeled optional, it is not possible to be a complete Java programmer without understanding the AWT Java EventsEvents are occurrences outside of the program to which the program must respond (e.g. user key press)Event managers are methods that first catch the eventListener classes send messages to an event manager requesting to be notified when a particular message occursEvent managers send messages to listeners notifying them when a particular event occursCreating Mouse Click ListenersImport the java.awt.event packageIn the class header, add the words implements MouseListenerSend the addMouseListerner(this) message to the mouse event manager (should be done in the constructor method)Define methods mouseClicked, mousePressed, mouseReleased, mouseEntered, mouseExited Mouse Click Listener Templateimport java.awt.event.*;public class classname implements MouseListener { // include manager.addMouseListener(this); // in some initialization method public void mouseClicked (MouseEvent e) { } public void mousePressed (MouseEvent e) { } public void mouseReleased (MouseEvent e) { } public void mouseEntereded (MouseEvent e) { } public void mouseExited (MouseEvent e) { } }Finding Mouse LocationInside mouseClicked the argument e of type MouseEvent can be used to find the location of the mouse cursorThe call e.getX( ) returns the value of the horizontal coordinate of the mouse cursor positionThe call e.getY( ) returns the value of the vertical coordinate of the mouse cursor positionCreating Mouse Motion ListenersImport the java.awt.event packageIn the class header, add the words implements MouseMotionListenerSend the message addMouseMotionListerner(this) to the mouse event manager (should be done in the constructor method)Define methods mouseMoved and mouseDraggedMouse Motion Listener Templateimport java.awt.event.*;public class classname implements MouseMotionListener { // include manager.addMouseListener(this); // in some initialization method public void mouseMoved (MouseEvent e) { } public void mouseDragged (MouseEvent e) { } }Mouse MotionmouseMoved will be called each time the mouse makes a significantly large movementmouseDragged will be called instead if thre mouse button is pressed and the mouse makes a significantly large movementListening to All Mouse EventsTwo catch all seven mouse events use the following class headerpublic class classname implements MouseListener, MouseMotionListener {You then need to define all seven mouse methods mouseMoved, mouseDragged, mouseClicked, mousePressed, mouseReleased, mouseEntered, mouseExited

Các file đính kèm theo tài liệu này:

  • pptchapter11_2837.ppt
Tài liệu liên quan