DOC PREVIEW
CORNELL CS 211 - Lecture Notes

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 25 Listening to buttons and mice Quotes by Tony HoareLecture 25 Listening to buttons and miceReview of lecture 23:Basic Hierarachy of GUI classesContainer: superclass of all Components that can hold other components.PowerPoint PresentationSlide 7Slide 8Slide 9Slide 10Slide 111Lecture 25 Listening to buttons and miceQuotes by Tony HoareThere are two ways of constructing a software design: (1) make it so simple that there are obviously no deficiencies and (2) make it so complicated that there are no obvious deficiencies.Inside every large program is a small program trying to get out.The unavoidable price of reliability is simplicity.I was eventually persuaded of the need to design programming notations so as to maximize the number of errors that cannot be made, or if made, can be reliably detected at compile time.You cannot teach beginners top-down program-ming because they don't know which end is up.2Lecture 25 Listening to buttons and miceReading material is the same as the previous lecture.The programs we demo today are on the website. Download them and experiment with them!Look at java.awt and javax.swing as examples of how one designs a large system of classes. This is not the only way to do GUIs. This is how Java does it.3Review of lecture 23:Lecture 24. We:(1) Looked at different components, like JButton, JPanel.(2) Saw three different layout managers:1. FlowLayout2. BorderLayout3. BoxLayoutThey are used to lay out components as they are added to a container.(3) Saw how to add components to a container. End with pack() and show().4Basic Hierarachy of GUI classesA Component generally has aposition and size, can be painted,can receive input events.Canvas: rectangular componentwith methods for painting it.Subclass it and override methodpaint()Component: abstractContainerWindowFrameJFrameJComponentJPanelJButtonJCheckBoxJComboBoxJLabelJRadioBoxJTextAreaJTextFieldJFileChooserCanvasDialogJDialog5Container: superclass of all Components that can hold other components.Components are generally added to a Container c:c.add(new JButton(“yes”));c.add(new JButton(“no”), “north”);Basic top-level containers: JWindow: top-level window with no borderJFrame: top-level window with border, menubarJDialog: top-level window used for a dialogJPanel, primary use: as a container of other components. Allows one to organize objects into a unit, often to simplify layouts. See this on the next slides.JPanel, secondary use: paint on it with graphics commands --lines, circles, text, etc. (instead of using class Canvas). ContainerWindow JComponentJPanelJFrameJDialog6Listening to a buttonpublic class ButtonDemo1 extends JFrame implements ActionListener { private JButton westButton= new JButton("west"); private JButton eastButton= new JButton("east"); /** Constructor: invisible frame: title t, 2 buttons */ public ButtonDemo1(String t) { super(t); Container cp= getContentPane(); cp.add(westButton,BorderLayout.WEST); cp.add(eastButton,BorderLayout.EAST); westButton.setEnabled(false); eastButton.setEnabled(true); westButton.addActionListener(this); eastButton.addActionListener(this); pack(); } public void actionPerformed(ActionEvent e) { boolean b= eastButton.isEnabled(); eastButton.setEnabled(!b); westButton.setEnabled(b); }}1. write procedure actionPerformed2. implement ActionListener3. Register this object with the button7public class ButtonDemo2 extends JFrame implements ActionListener { private JButton westButton= new JButton("west"); private JButton eastButton= new JButton("east"); /** Constructor: inv frame with title t, two buttons */ public ButtonDemo2(String t) { super(t); Container cp= getContentPane(); cp.add(westButton,BorderLayout.WEST); cp.add(eastButton,BorderLayout.EAST); westButton.setEnabled(false); eastButton.setEnabled(true); westButton.addActionListener(this); eastButton.addActionListener(new BeListener()); } public void actionPerformed(ActionEvent e) { boolean b= eastButton.isEnabled(); eastButton.setEnabled(!b); westButton.setEnabled(b); } private class BeListener implements ActionListener { public void actionPerformed(ActionEvent e) { boolean b= eastButton.isEnabled(); eastButton.setEnabled(!b); westButton.setEnabled(b); } }}listener foreastButtonlistener forwestButton,in its own inner classdifferent listeners for different buttons8Processing mouse clicks/** A red or green square, possibly with a pink disk */public class Square extends JPanel { public static final int HEIGHT= 50; // height and public static final int WIDTH= 50; // width, square private int x, y; // Coordinates of square on board private bool hasDisk= false; // = "square has pink disk" public Square(int x, int y) { // a square at (x,y) this.x= x; this.y= y; setPreferredSize(new Dimension(WIDTH,HEIGHT)); this.addMouseListener(new MouseEvents()); } public void paint(Graphics g) { // paint this square if ((x+y)%2 == 0) { g.setColor(Color.green); } else { g.setColor(Color.red); } g.fillRect(0,0,WIDTH-1,HEIGHT-1); if (hasDisk) { g.setColor(Color.pink); g.fillOval(7,7,WIDTH-14,HEIGHT-14); } g.setColor(Color.black); g.drawRect(0,0,WIDTH-1,HEIGHT-1); g.drawString("("+x+", "+y+")", 10, 5+HEIGHT/2); } // continued on next slide //9 // Class Square (continued) // /** Complement the "has pink disk" property */ public void complementDisk() { hasDisk= !hasDisk; repaint(); // Don’t call paint(Graphics) directly } // instead, call inherited method repaint() /** Remove pink disk (if present) */ public void clearDisk() { hasDisk= false; repaint(); } /** Contains methods that process mouse events */ public class MouseEvents extends MouseInputAdapter { /** Complement the "has pink disk" property */ public void mouseClicked(MouseEvent e) { complementDisk(); } }}API class MouseInputAdapter has several methods for processing mouse clicks, mouse presses, mouse releases, etc. We override only one of them here. That’s how we get the program to listen to mouse events.10/** Demo use of a mouse listener.Instance is a JFrame that is a 2x2 grid of squares, each of class Square, and a button with title "reset". Clicking an empty square draws a pink disk on it. Clicking again removes it. Clicking button "reset" removes all pink disks.This class listens to clicks of the button.Clicks on a square are listened to in class Square. */public class MouseDemo extends JFrame { Box bl= new


View Full Document

CORNELL CS 211 - Lecture Notes

Documents in this Course
B-Trees

B-Trees

10 pages

Hashing

Hashing

3 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?