Programming: You will have 6 files all need to be located in the dir. named PA4:

Similar documents
CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

Chapter 7 Applets. Answers

TWO-DIMENSIONAL FIGURES

Graphics Applets. By Mr. Dave Clausen

2IS45 Programming

Unit 7: Event driven programming

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Lecture 7 A First Graphic Program And Data Structures & Drawing

Graphics Applets. By Mr. Dave Clausen

Object Orientated Programming in Java. Benjamin Kenwright

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Graphics -- To be discussed

Topic 7: Algebraic Data Types

An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

Practice Midterm 1. Problem Points Score TOTAL 50

9. APPLETS AND APPLICATIONS

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

CS 134 Programming Exercise 3:

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

1 Getting started with Processing

Summary. 962 Chapter 23 Applets and Java Web Start

Java Programming Lecture 6

8. Polymorphism and Inheritance

Lecture Static Methods and Variables. Static Methods

SIMPLE APPLET PROGRAM

Chapter 3 - Introduction to Java Applets

Java Coordinate System

2. (True/False) All methods in an interface must be declared public.

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

Here is a list of a few of the components located in the AWT and Swing packages:

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

CSE 11 Midterm Fall 2008

Chapter 6 Introduction to Defining Classes

Module 5 Applets About Applets Hierarchy of Applet Life Cycle of an Applet

The AWT Package, Graphics- Introduction to Images

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Chapter 12 Advanced GUIs and Graphics

IT101. Graphical User Interface

We are on the GUI fast track path

Chapter 1 GUI Applications

ITI Introduction to Computing II

Java 1.8 Programming

S.E. Sem. III [CMPN] Object Oriented Programming Methodology

Control Statements: Part Pearson Education, Inc. All rights reserved.

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

ITI Introduction to Computing II

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS 11 java track: lecture 3

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

AP CS Unit 12: Drawing and Mouse Events

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

CSC System Development with Java Introduction to Java Applets Budditha Hettige

Practice Midterm 1 Answer Key

Using the API: Introductory Graphics Java Programming 1 Lesson 8

G51PRG: Introduction to Programming Second semester Applets and graphics

Assignment 2. Application Development

Lecture Static Methods and Variables. Static Methods

Advanced Internet Programming CSY3020

M257 Past Paper Oct 2007 Attempted Solution

Programming Assignment 4 ( 100 Points )

1 Getting started with Processing

We will start our journey into Processing with creating static images using commands available in Processing:

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

Chapter 2 Exercise Solutions

Programmierpraktikum

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Programming graphics

Programming Assignment 2 (PA2) - DraggingEmoji & ShortLongWords

Chapter 24. Graphical Objects The GraphicalObject Class

Use the scantron sheet to enter the answer to questions (pages 1-6)

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

CSE115 Lab 4 Fall 2016

F I N A L E X A M I N A T I O N

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)


Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Example: Building a Java GUI

Chapter 14 Abstract Classes and Interfaces

Solutions to Chapter Exercises. GUI Objects and Event-Driven Programming

Graphics and Java 2D Introduction OBJECTIVES. One picture is worth ten thousand words.

Example: Building a Java GUI

class Shape { // Color of the shape. (Recall that class Color // is defined in package java.awt. Assume // that this class has been imported.

ICS 4U. Introduction to Programming in Java. Chapter 10 Notes

Name Section. CS 21a Introduction to Computing I 1 st Semester Final Exam

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

cs Java: lecture #5

More about GUIs GEEN163

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

CISC 1600, Lab 2.1: Processing

CS 201 Advanced Object-Oriented Programming Lab 3, Asteroids Part 1 Due: February 17/18, 11:30 PM

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Lab 3: Work with data (IV)

Transcription:

PROGRAMMING ASSIGNMENT 4: Read Savitch: Chapter 7 and class notes Programming: You will have 6 files all need to be located in the dir. named PA4: PA4.java ShapeP4.java PointP4.java CircleP4.java RectangleP4.java TriangleP4.java DUE: Turnin: Tuesday, May 14, 2002 at 11:00pm turninpa4 You will copy the 5 classes you wrote in PA3 to your PA4 directory, make some modifications to them to support drawing shapes, and write a PA4 class (an applet) to implement a simple graphics manager to draw the shapes in a cartesian plane. Be sure to compile with javac *.java to ensure all your files are compiled each time. There is a sample PA4 and PA4.html in the Assignments directory. ShapeP4 Add the following abstract method declaration to your converted ShapeP5: public abstract void draw( Graphics g, int xoffset, int yoffset ); draw() will be polymorphically invoked from class PA4 from within paint(). Graphics g is the reference to the Graphics context on which to draw (the same reference that is passed as a parameter to paint(). xoffset and yoffset are the relative offsets from the upper left corner of the applet where coordinate (0,0) is located in the cartesian plane. These are essentially one half of the current width and one half of the current height, respectively, of the applet. PointP4 No new members. Just convert PointP3 to PointP4. CircleP4 The CircleP4() no-arg constructor of this class will be rewritten to ask the user for the circle's center point's x and y coordinates and the circle's radius via separate JOptionPane.showInputDialog() queries. If the user presses the Cancel button on any input, skip this and the rest of the input and default the skipped input to 0. Keep asking for input if the input string is empty (zero length). You can assume only valid input will be entered. To implement the draw() method: make a copy of the center point, translate this copy relative to the current offsets: xoffset in the X direction and (yoffset - 2*this point's y) in the Y direction. This translates the center point of the circle to be drawn to the coordinates relative to the center of the applet which is (0,0) in the cartesian plane. Now we need to calculate the upper left corner of the bounding box that defines the circle. This is done by translating the same center point by radius in the X direction and radius in the Y direction. Now we are ready to draw the circle. Use drawoval( int x, int y, int width, int height ) from class Graphics. The x and y offsets are just the values of x and y of the translated point, and width and height (of the bounding box) are both radius*2. Use Math.round() to round the doubles to longs and then cast to ints. 1

RectangleP4 The RectangleP4() no-arg constructor of this class will be rewritten to ask the user for the rectangle's upper left corner point's x and y coordinates and the rectangle's length and width via separate JOptionPane.showInputDialog() queries. If the user presses the Cancel button on any input, skip this and the rest of the input and default the skipped input to 0. Keep asking for input if the input string is empty (zero length). You can assume only valid input will be entered. To implement the draw() method: make a copy of the upper left corner of the rectangle, translate this copy relative to the current offsets: xoffset in the X direction and (yoffset 2*this point's y) in the Y direction. This translates the upper left corner of the rectangle to be drawn to the coordinates relative to the center of the applet which is (0,0) in the cartesian plane. Now we are ready to draw the rectangle. Use drawrect(int x, int y, int width, int height) from class Graphics. The x and y offsets and the length and width are just the values of x, y, length, and width of the translated point. Use Math.round() to round the doubles to longs and then cast to ints. TriangleP4 The TriangleP4() no-arg constructor of this class will be rewritten to ask the user for the triangle's x and y coordinates for the three points via separate JOptionPane.showInputDialog() queries. If the user presses the Cancel button on any input, skip this and the rest of the input and default the skipped input to 0. Keep asking for input if the input string is empty (zero length). You can assume only valid input will be entered. To implement the draw() method: make a copy of the three points, translate these copies relative to the current offsets: xoffset in the X direction and (yoffset 2*this point's y) in the Y direction. This translates each point of the triangle to be drawn to the coordinates relative to the center of the applet which is (0,0) in the cartesian plane. Create two arrays of three ints each for the x and y coordinates of the points to draw the triangle. Use drawpolygon( int[] xpoints, int[] ypoints, int npoints ) from class Graphics. The x and y coordinates of the first point of the triangle should be in xpoints[0] and ypoints[0], respectively. The x and y coordinates of the second point of the triangle should be in xpoints[1] and ypoints[1], respectively. Likewise for the third point. PA4 public class PA4 extends JApplet implements ListSelectionListener public void init() {... } public void paint( Graphics g ) {... } public void valuechanged( ListSelectionEvent e ) {... } class PA4 is an applet that will drive a simple graphics manager for the shapes above. To select which shape to draw, PA4 will use a JList with the names of the three shapes to draw. To identify which shape is selected in the JList, we will use a ListSelectionListener and implement the valuechanged() method. Each time a shape is selected, put a reference to the newly created shape into an array of ShapeP4 objects. For purposes of this assignment, make the size of this array five elements. Remember: all instance variables must be private. init() The JList object will be aligned to the top right of the applet. Use FlowLayout with right alignment: Container c = getcontentpane(); c.setlayout( new FlowLayout( FlowLayout.RIGHT ) ); 2

Pass to the JList constructor an array of Strings with the names of the shapes as the array elements: Circle, Rectangle, and Triangle. Add this applet as the listener (event handler) for list selection events from the list of shape names. Set the selection mode to single selection. paint() list = new JList( shapenames ); list.addlistselectionlistener( this ); list.setselectionmode(listselectionmodel.single_selection); paint() will be called any time the applet needs to be drawn/redrawn. To redraw the lightweight GUI components (in this case just the JList object) on the applet after the applet has been cleared, call paint() in the superclass (JApplet). getwidth() and getheight() is used to get the current width and height of the applet. Half of these is the offset in the x and y direction where we can find the (0,0) coordinate of the cartesian plane on which all the shapes will be drawn. Remember, applets use the upper left corner of the applet as location (0,0) and use only positive x and y offsets from that (0,0) location. We will be using a cartesian plane in which (0,0) is the center point of the applet no matter how we change the dimension of the applet. Set the drawing color to black and draw the x and y axes as shown in the examples and the PA4 sample program. Place all +/- bounds at an inset of 10 pixels. A useful method g.getfontmetrics().stringwidth(str) should be used to determine the number of pixels wide a given string is in the current font. You should use this (and the inset) to determine where to draw the positive x offset/bounds of the X axis. Once the lines and values of the axes are drawn, change the drawing color to blue. Cycle through the array of ShapeP4 objects for the number of shapes in the array, and polymorphically invoke the draw() method similar to: shapes[i].draw( g, xoffset, yoffset ); valuechanged() valuechanged() is going to be called any time a change occurs on the list. Many list selection events can be generated from a single user action such as a mouse click. The getvalueisadjusting() method returns true if the user is still manipulating the selection. This particular program is interested only in the final result of the user's action, so the valuechanged() method updates the shapes[] array only if e.getvalueisadjusting() returns false and list.isselectionempty() is false. Within a try block, if the number of shapes in the array of shapes is less than the length of the array, do the following: Using shapestring = (String) list.getselectedvalue(); get the String value of the selected item in the list. Then use ShapeP4 shape = (ShapeP4) Class.forName( shapestring + "P4" ).newinstance(); shapes[numofshapes++] = shape; to dynamically create an object from the selected JList item and assign this shape to the next shapes array element. newinstance() will invoke the no-arg constructor for the given class. More details of this in class. Clear the selected item with list.clearselection() so it can capture the event of it being selected again as a changed value. Then repaint() to clear the applet and redisplay the coordinates and shapes. If the number of shapes in the array of shapes is not less than the length of the array, still clear the selection (list.clearselection()) and display the message "Shapes container is full -- cannot create new Shape!" to the status bar of the applet (using showstatus()). 3

Class.forName() and newinstance() can throw checked exceptions. We will discuss Exceptions in more detail soon. For this program, you can use a generic catch block to catch all Exceptions and display to the terminal the exception. The try-catch block described above will look something like this: try { // If the number of shapes in the array of shapes is less than the // the array's length. // Get the the value (String) of the selected list item. // Dynamically instantiate an instance of an object of this type with "P4" // appended for our shapes types in this PA and assign this reference to // the array of shapes incrementing the number of shapes now in the array. // Clear the selection so we can select that same list item again. // Call repaint() to update the graphics display. // Else if there isn't any room in the array of shapes for this selected shape // Clear the selection // Display the shapes container full message to the status bar of the applet // and the terminal. } catch( Exception e ) { System.out.println( e ); } Examples: appletviewer PA4.html Circle at (0,0) radius = 200 4

Rectangle at (-200,200) Triangle at (-150,150); width = 350; height = 400 (150,150); (100,-275) Change the dimension of the applet window 5

Add another Circle at (200,175) with radius = 150 Add another Rectangle at (-300,300) length = 55; width = 250 Then try to add another Circle. Note message in status bar. Applet menu Reload to start over. Be sure to test 1) having no input in an input dialog box and 2) hitting the Cancel button to indicate zero default values from that input and the rest of the inputs for that shape. 6