Lab 7 (Mar 12) - COMP-2001-001 (Obj-Orient Prgm Human Comp Int - 88652) PDF

Title Lab 7 (Mar 12) - COMP-2001-001 (Obj-Orient Prgm Human Comp Int - 88652)
Author Sahil Salma
Course Intro to Java
Institution Memorial University of Newfoundland
Pages 9
File Size 361.4 KB
File Type PDF
Total Downloads 53
Total Views 122

Summary

Professor: Donna Batton...


Description

4/18/2018

Lab 7 (Mar 12) - COMP-2001-001 (Obj-Orient Prgm Human Comp Int - 88652)

COMP-2001-001 (Obj-Orient Prgm Human Comp Int - 886…

Lab 7 (Mar 12)

Computer Science 2001 - Lab 7

Readings Chapter 10

Objectives From Textbook Chapter 10: To To To To

implement simple graphical user interfaces add buttons, text fields, and other components to a frame window handle events that are generated by buttons write programs that display simple drawings

Notes Some of the exercises in this lab may have been provided with the instructor's online resources which accompany the textbook (available from http://www.wiley.com/college/horstmann). The Java 8 API is available from: https://docs.oracle.com/javase/8/docs/api/ If you think an answer to an electronically tested question is incorrect or you are having trouble with a particular topic, please contact one of the lab assistants for help. When checking your answers online, you may click on the Check My Answers button after you answer each question or when you have finished answering all the questions for that section. It does not matter if you get answers wrong or send your answers many times since the results are not recorded. This may be particularly useful to do when filling in the tables in this lab. For example, you could click on Check My Answers to check your answers to the first row before going on to the next row. Please note that some questions may be marked as optional. You should complete all non-optional questions first before attempting the optional question(s) if you have time.

Lab Exercises

https://online.mun.ca/d2l/le/content/264716/viewContent/2451688/View

1/9

4/18/2018

Lab 7 (Mar 12) - COMP-2001-001 (Obj-Orient Prgm Human Comp Int - 88652)

Lab Exercises 1. Complete code for displaying a frame. [Chapter Reference: 10.1]

import javax.swing.____; public class TestFrameViewer { public static void main(String[] args) { ____ frame = new ____(); final int FRAME_WIDTH = 250; final int FRAME_HEIGHT = 250; frame.____(FRAME_WIDTH, FRAME_HEIGHT); frame.setTitle("A Test Frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.____(true); } }

Show Solution

2. Drawing a Triangle 2.1. Triangle component. [Chapter Reference: 10.4] Create a component (extends JComponent) that uses three Line2D.Double objects to draw a triangle that looks like this:

Show Solution

2.2. Viewer class for the triangle component. [Chapter Reference: 10.4] To show the component that draws a triangle you need a viewer class Write a viewer class that shows the triangle component

https://online.mun.ca/d2l/le/content/264716/viewContent/2451688/View

2/9

4/18/2018

Lab 7 (Mar 12) - COMP-2001-001 (Obj-Orient Prgm Human Comp Int - 88652)

class. Write a viewer class that shows the triangle component that you created above. Show Solution

3. Drawing a house. [Chapter Reference: 10.4] Use Rectangle and Line2D.Double objects to draw a house like this one:

What is the code for your component? Show Solution

4. Drawing colored tangent ellipses. [Chapter Reference: 10.4] Generate five circles with the following diameters, 40, 80, 120, 160, and 200, all tangent at a common point.

Draw each circle in a different color. What is the code for your component? Show Solution

5. Frames. 5.1. Creating a frame. [Chapter Reference: 10.1] Write a main program that displays a single frame with the title "My First Frame". Set the size to 800 by 800. Make the frame visible. Show Solution

5.2. Creating a frame that holds a panel. [Chapter Reference: 10.1] Create a panel by using the JPanel constructor and add it to the frame. Use Color.RED (a constant in the java.awt package) along with the setBackground() method in JPanel, to set the color of the https://online.mun.ca/d2l/le/content/264716/viewContent/2451688/View

3/9

4/18/2018

Lab 7 (Mar 12) - COMP-2001-001 (Obj-Orient Prgm Human Comp Int - 88652)

panel. Add a JButton and a JLabel to the panel before adding the panel to the frame. Display the results. Show Solution

5.3. Creating a custom frame class. [Chapter Reference: 10.1] The goal of this problem is to repackage most of the previous code as a class that extends JFrame. Build a class called MyCustomFrame that extends JFrame. The class should contain instance variables for the button and the label, as well as constants for the frame width and height. The constructor should call a private helper method, createComponents(), that instantiates the button, the label, and the panel. The helper method should also add the button and the label to the panel, and add the panel to the frame. After creating the components, the constructor should set the size of the frame. MyCustomFrameViewer.java (click to download) import javax.swing.JFrame; public class MyCustomFrameViewer { public static void main(String[] args) { MyCustomFrame frame = new MyCustomFrame(); frame.setTitle("My first frame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }

Show Solution

5.4. Adding a listener. [Chapter Reference: 10.2] Build a separate class called ClickListener that implements the interface. Add an actionPerformed() method that prints the message "Button was clicked." using System.out.println(). Create a ClickListener object and register the object with the button by invoking addActionListener(). ActionListener

Test the program by clicking the button. When the program is working correctly, convert the ClickListener class to an inner class in MyCustomFrame and test the program again. Show Solution

5.5. Adding a JTextField and a JTextArea. [Chapter https://online.mun.ca/d2l/le/content/264716/viewContent/2451688/View

4/9

4/18/2018

Lab 7 (Mar 12) - COMP-2001-001 (Obj-Orient Prgm Human Comp Int - 88652)

Reference: 10.3] Add a JTextField and a JTextArea to the frame. Initialize the field to an empty string and the text area to "||". Modify the program so that the contents of the field are appended to the area each time the user clicks the button. For example, if the user enters "xxx" in the field and clicks the button three times, the area contains "||xxx||xxx||xxx||". Show Solution

6. Creating a GUI application. 6.1. Adding code to display an open and closed door. [Chapter Reference: 10.4] In this problem we will use the Door class to create a GUI application to draw an open or closed door. The original version of Door class has been modified, slightly, to be easier to use in our GUI application. In the previous version, a message was printed each time the door was opened or closed. In this version, instead of printing a message, a String variable message is set and a getMessage method for the variable has been provided. Here is the revised code for the Door class: Door.java (click to download) import java.awt.*; public class Door { private String state; private String message;

// Open or closed

/** Constructor for objects of class Door. Pass in "open" or "closed" to represent the state of the Door object. @param aState the initial state of the Door object */ public Door(String aState) { // Initialize the state of the Door with one of two states: open, clo state = aState; message = ""; } /** A Boolean method that reports whether the state variable is "open". @return true if state equals "open" */ public boolean isOpen() https://online.mun.ca/d2l/le/content/264716/viewContent/2451688/View

5/9

4/18/2018

Lab 7 (Mar 12) - COMP-2001-001 (Obj-Orient Prgm Human Comp Int - 88652)

{ return state.equals("open"); } /** A Boolean method that reports whether the state variable is "closed". @return true if state equals "closed" */ public boolean isClosed() { return state.equals("closed"); } /** A mutator method that sets the state variable. @param newState the new value for the state variable */ public void setState(String newState) { state = newState; } /** An accessor method that gets the door's message variable. */ public String getMessage() { return message; } /** Sets the state of the Door to open if the door is closed. */ public void open() { if (state.equals("open")) { message = "The door is already open."; } else { state = "open"; message = "The door has been opened"; } } /** Sets the state of the Door to closed if the door is open. */ public void close() { if (state.equals("open")) { state = "closed"; message = "The door has been closed."; } else {

https://online.mun.ca/d2l/le/content/264716/viewContent/2451688/View

6/9

4/18/2018

Lab 7 (Mar 12) - COMP-2001-001 (Obj-Orient Prgm Human Comp Int - 88652)

{ message = "The door is already closed."; } } public String toString() { return "The door is " + state + "."; } }

We want to be able to draw the door in an open and closed state. Add two methods to the Door class for doing this. The methods should use the following signatures: public void drawOpenDoor(Graphics page) public void drawClosedDoor(Graphics page)

Notice that a Graphics object is passed as a parameter to each method. Review the available methods in the Graphics class and use its methods to draw representations of open and closed doors. (Hint: You can make any polygon using public void fillPolygon(int[] arrayOfXPoints, int[] arrayOfYPoints, int numberOfPoints), where the arrays contain the x- and y-values

of

the vertices (corners) in the polygon.) The first integer array that is passed to the fillPolygon method contains a list of the x-coordinates of the four vertices of the door in sequence (say upper left corner, upper right corner, lower right corner, lower left corner). The second integer array contains the y-coordinates of the same four vertices in the same sequence. In order to see your open and closed door, the Door object has to be associated with a JPanel. Then the paintComponent method of the JPanel can be used to invoke drawOpendoor and drawClosedDoor. Here is the code for TempDoorPanel which can be used as is to create and display a door inside a JFrame. You will still need a main method, which is supplied in the next step, in order to use the JPanel below. TempDoorPanel.java (click to download) import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TempDoorPanel extends JPanel { private Door door; public TempDoorPanel () { door = new Door("closed"); https://online.mun.ca/d2l/le/content/264716/viewContent/2451688/View

7/9

4/18/2018

Lab 7 (Mar 12) - COMP-2001-001 (Obj-Orient Prgm Human Comp Int - 88652)

door = new Door( closed ); setBackground (Color.blue); setPreferredSize (new Dimension(360, 400)); } public void paintComponent (Graphics page) { super.paintComponent(page); //door.drawOpenDoor(page); door.drawClosedDoor(page); } }

Finally, a JFrame object needs to be instantiated, and a TempDoorPanel needs to be instantiated and added to the JFrame. The JFrame is then packed and made visible. Here is the main program code which can be used to finally display your door. TempDoorRunner.java (click to download) import javax.swing.JFrame; public class TempDoorRunner { /** Creates the main frame of the program. */ public static void main(String[] args) { JFrame frame = new JFrame("Door"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TempDoorPanel panel = new TempDoorPanel(); frame.setSize(400,500); frame.add(panel); frame.setVisible(true); } }

What is your revised Door class? Show Solution

6.2. Adding buttons for opening and closing a door. [Chapter Reference: 10.4] We can begin to make the previous application interactive by adding buttons. The buttons can be clicked to open and close the door visually. Create a new class, DoorPanel, based on TempDoorPanel that adds two JButton references to the panel: one to open and one to close the door. Also add a JTextField reference to the panel so the message inside the Door class can be displayed. Modify the constructor to instantiate the JButton objects and the JTextField. Set the JT tFi ld to the current message in the door (Hint: use https://online.mun.ca/d2l/le/content/264716/viewContent/2451688/View

8/9

4/18/2018

Lab 7 (Mar 12) - COMP-2001-001 (Obj-Orient Prgm Human Comp Int - 88652)

Set the JTextField to the current message in the door. (Hint: use getMessage in Door and setText in JTextField.) Organize the buttons and text field on a separate panel. Choose a suitable layout manager to build the DoorPanel. Modify paintComponent in DoorPanel so that it uses conditional logic to test the state of the door. Invoke drawOpenDoor if the door is currently in an open state. Otherwise invoke drawClosedDoor. Finally, create a DoorRunner class based on TempDoorRunner to display the new JPanel. The buttons will not yet be responsive to clicks but you should be able to use the code to display an open or closed door, and you should see the buttons and text field. In the next part of this lab we will add code to activate button clicks. Show Solution Send to Binder

Download

Print

Acvity Details You have viewed this topic

Due March 12 at 4:50 PM

Last Visited Mar 12, 2018 5:05 PM

https://online.mun.ca/d2l/le/content/264716/viewContent/2451688/View

9/9...


Similar Free PDFs