CSIS 10A Assignment 14 SOLUTIONS

Similar documents
OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

public void mouseexited (MouseEvent e) setminute(getminute()+increment); 11.2 public void mouseclicked (MouseEvent e) int x = e.getx(), y = e.gety();

AP CS Unit 12: Drawing and Mouse Events

Applet which displays a simulated trackball in the upper half of its window.

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Computer Science II - Test 2

Programmierpraktikum

G51PRG: Introduction to Programming Second semester Applets and graphics

CSC System Development with Java Introduction to Java Applets Budditha Hettige

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

AP Computer Science Unit 13. Still More Graphics and Animation.

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More

Chapter 14: Applets and More

Chapter 14: Applets and More

Some classes and interfaces used in this chapter from Java s original graphics capabilities and from the Java2D API.

Exam: Applet, Graphics, Events: Mouse, Key, and Focus

Advanced Java Unit 6: Review of Graphics and Events

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

PESIT Bangalore South Campus

Graphics Applets. By Mr. Dave Clausen

Java Applet Basics. Life cycle of an applet:

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

CS 106A, Lecture 14 Events and Instance Variables

Lecture 5: Java Graphics

CS 106A, Lecture 14 Events and Instance Variables

Unit 7: Event driven programming

Graphics and Java2D. Objectives

Windows and Events. created originally by Brian Bailey

Advanced Internet Programming CSY3020

Graphics Applets. By Mr. Dave Clausen

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

ISO-2022-JP (JIS ) 1 2. (Windows95/98 MacOS ) Java UNICODE UNICODE. Java. .java.java.txt native2ascii. .java

H212 Introduction to Software Systems Honors

Lecture 3: Java Graphics & Events

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

TWO-DIMENSIONAL FIGURES

CS2110. GUIS: Listening to Events

The AWT Event Model 9

Graphical User Interfaces 2

: 15. (x, y) (x, y ) (x, y) ( x x = 1 y = y ) (x = x y y = 1) (12.1.1)

cs Java: lecture #5

GUI Event Handling 11. GUI Event Handling. Objectives. What is an Event? Hierarchical Model (JDK1.0) Delegation Model (JDK1.1)

1 Definitions & Short Answer (5 Points Each)

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox

CS2110. GUIS: Listening to Events

Computer Science 210: Data Structures. Intro to Java Graphics

CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions. Anonymous functions. Anonymous functions. Anonymous functions

Java - Applets. public class Buttons extends Applet implements ActionListener

Which of the following syntax used to attach an input stream to console?

Görünt Görün ülerin ölçeklenmesi ve yerle y ştirilmesi ş

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

Chapter 1 GUI Applications

CS 335 Graphics and Multimedia. Image Manipulation

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Assignment 8B SOLUTIONS

Java Coordinate System

Java - Applets. C&G criteria: 1.2.2, 1.2.3, 1.2.4, 1.3.4, 1.2.4, 1.3.4, 1.3.5, 2.2.5, 2.4.5, 5.1.2, 5.2.1,

Java for Interfaces and Networks (DT3010, HT10)

Advanced Java Programming

EXCEPTIONS & GUI. Errors are signals that things are beyond help. Review Session for. -Ankur Agarwal

FirstSwingFrame.java Page 1 of 1

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

CIS 162 Project 1 Business Card Section 04 (Kurmas)

I/O Framework and Case Study. CS151 Chris Pollett Nov. 2, 2005.

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

at 1.000,5.289 ljust "JLabel" at 3.438,5.289 ljust " JPanel" at 2.125,6.914 ljust "JFrame" at 2.250,9.039 ljust "content pane" at 2.125,7.

9. APPLETS AND APPLICATIONS

CSIS 10A Assignment 7 SOLUTIONS

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

File: GFace.java /* * File: GFace.java * This class implements a face as a GCompound. */

Graphic User Interfaces. - GUI concepts - Swing - AWT

CSIS 10A PRACTICE FINAL EXAM Name Closed Book Closed Computer 3 Sheets of Notes Allowed

Event Driven Programming

Fall 2011 Final Test

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

Events Chris Piech CS106A, Stanford University. Piech, CS106A, Stanford University

Yvon s Checker game: Begin Board class

THE UNIVERSITY OF AUCKLAND

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

Side Trip into Java: Enumeration Types. CS61B Lecture #36. Making Enumerals Available Elsewhere. Enum Types in Java. import static checkers.piece.

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Dr. Hikmat A. M. AbdelJaber

User interfaces and Swing

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

What Is an Event? Some event handler. ActionEvent. actionperformed(actionevent e) { }

CSIS 10A Practice Final Exam Solutions

Chapter 1 Introduction to Java

Programming graphics

Chapter 12 Advanced GUIs and Graphics

China Jiliang University Java. Programming in Java. Java Applets. Java Web Applications, Helmut Dispert

Chapter 7 Applets. Answers

Come & Join Us at VUSTUDENTS.net

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping.

Contents. 18 Algorithms Case study: network search The search algorithm Case study: automatic language recognition 57

Appendix G Navigation and Collision Detection for Web-Based 3D Experiment

SIMPLE APPLET PROGRAM

Advanced Java Programming (17625) Event Handling. 20 Marks

Transcription:

CSIS 10A Assignment 14 SOLUTIONS Read: Chapter 14 Choose and complete any 10 points from the problems below, which are all included in the download file on the website. Use BlueJ to complete the assignment, then export JAR file and upload to the server using your pass code. You may do more than 10 points of work but the max award will be 11 points. Part I: Exam Prep Exercises-- 7 points total To be answered in the Exercises file Find the Error Problems in the Exercises File 2. public void paint(graphics g) drawline(0, 0, 100, 100); XXXXXX should be g.drawline 4. public class MyPanel extends JPanel XXXXX should be extends JApplet public MyPanel() XXXXX should be public void init() // Constructor code... public void paint(graphics g)

// paint method code... 5. private class MyMouseListener implements MouseListener public void mouseclicked(mouseevent e) mouseclicks += 1; XXXXXX must have all methods required by the MouseListener interface ( such as mousepressed, mousereleased, mouseentered, mouseexited) Algorithm Workbench a) g.drawrect(50,75,100,200); b) g.fillrect(10,90,300,100); c) g.setcolor(color.blue); g.drawoval(10,25,100,50); d) g.setcolor(color.red); g.drawline(0,5,150,175); e) g.setfont( new Font("Georgia",Font.BOLD,20)); g.drawstring("greetings Earthling"); f) int [] x = 10,10,50,50; int [] y = 10,25,25,10; g.drawpolygon(x,y,4); // it's a rectangle Timer clock = new Timer(500, new MyTimerListener()); Part 2: Programming Challenges 3 points each

1) Create a class P1FollowMe that solves the following problem: import java.awt.*; import javax.swing.*; import java.awt.event.*; /** * Class FollowMe - write a description of the class here * * @author Abraham Lopez * @version 5-3-12 */ public class P1FollowMe extends JApplet private int currentx = 250; private int currenty = 250; public void init() addmouselistener (new MyMouseListener()); addmousemotionlistener(new MyMouseMotionListener()); public void paint (Graphics g) super.paint(g); g.drawstring ("Hello", currentx, currenty); g.setfont (new Font ("SansSerif", Font.BOLD, 72)); private class MyMouseListener implements MouseListener public void mouseclicked (MouseEvent e) public void mousepressed (MouseEvent e) public void mousereleased (MouseEvent e) public void mouseentered (MouseEvent e) currentx = e.getx(); currenty = e.gety(); repaint (); public void mouseexited (MouseEvent e) currentx = 250; currenty = 250; repaint ();

private class MyMouseMotionListener implements MouseMotionListener public void mousedragged(mouseevent e) public void mousemoved(mouseevent e) currentx = e.getx(); currenty = e.gety(); repaint (); 2) Create a class called P2House that solves the following problem: P2House.java import java.awt.*; import javax.swing.*; import java.awt.event.*; /** * Class P2House - write a description of the class here * * @author Abraham Lopez * @version 5-3-12 */ public class P2House extends JApplet private boolean dooropen; private boolean rightwindowopen; private boolean leftwindowopen; private int xpos; private int ypos; public void init () dooropen = true;

rightwindowopen = true; leftwindowopen = true; addmouselistener (new MyMouseListener()); public void paint (Graphics g) int [] roofx = 50, 250, 450; int [] roofy = 100, 10, 100; super.paint(g); g.drawpolygon (roofx, roofy, 3); g.drawrect (100, 100, 300, 150); g.drawarc (270, 200, 10, 10, 0, 360); if (dooropen) g.fillrect (215, 135, 75, 115); else g.drawrect (215, 135, 75, 115); if (leftwindowopen) g.fillrect (150, 135, 25, 25); g.fillrect (125, 135, 25, 25); g.fillrect (150, 160, 25, 25); g.fillrect (125, 160, 25, 25); else g.drawrect (150, 135, 25, 25); g.drawrect (125, 135, 25, 25); g.drawrect (150, 160, 25, 25); g.drawrect (125, 160, 25, 25); if (rightwindowopen) g.fillrect (350, 135, 25, 25); g.fillrect (325, 135, 25, 25); g.fillrect (350, 160, 25, 25); g.fillrect (325, 160, 25, 25); else g.drawrect (350, 135, 25, 25); g.drawrect (325, 135, 25, 25); g.drawrect (350, 160, 25, 25); g.drawrect (325, 160, 25, 25); private class MyMouseListener extends MouseAdapter public void mousepressed (MouseEvent e)

public void mouseclicked (MouseEvent e) xpos = e.getx(); ypos = e.gety(); if ((xpos >= 215 && xpos <= 285) && (ypos >= 135 && ypos <= 250)) dooropen =!dooropen; if ((xpos >= 125 && xpos <= 175) && (ypos >= 135 && ypos <= 185)) leftwindowopen =!leftwindowopen; if ((xpos >= 325 && xpos <= 375) && (ypos >= 135 && ypos <= 185)) rightwindowopen =!rightwindowopen; repaint(); 3) Create a class called P3PolygonDrawer that solves the following problem: import java.awt.*; import javax.swing.*; import java.awt.event.*; /** * Class PolygonDrawer - write a description of the class here * * @author Abraham Lopez * @version 5-4-12 */ public class P3PolygonDrawer extends JApplet int count = 0; // keeps track of number of points entered by clicking mouse private int [] xpoints; private int [] ypoints; private int xvalue; private int yvalue; public void init () xpoints = new int [6]; ypoints = new int [6]; addmouselistener (new MyMouseListener ()); public void paint (Graphics g)

super.paint(g); g.drawpolygon (xpoints, ypoints, count); private class MyMouseListener extends MouseAdapter public void mouseclicked (MouseEvent e) public void mousepressed (MouseEvent e) xvalue = e.getx(); // save the mouse click point yvalue = e.gety(); if (count < xpoints.length) xpoints[count] = xvalue; // store the point of the mouse click in // the array ypoints[count] = yvalue; count++; // add one to count for next time if (count == xpoints.length) // once we get all the points, repaint repaint(); 4) Create a class called P4GridFiller that solves the following problem: P4GridFiller.java import java.awt.*; import javax.swing.*; import java.awt.event.*; /** * Class P2House - write a description of the class here * * @author Abraham Lopez * @version 5-3-12 */ public class P4GridFiller extends JApplet private boolean firstsquare; private boolean secondsquare; private boolean thirdsquare; private boolean fourthsquare; private int xpos; private int ypos; public void init () firstsquare = false;

secondsquare = false; thirdsquare = false; fourthsquare = false; addmouselistener (new MyMouseListener()); public void paint (Graphics g) super.paint(g); g.drawrect (100, 100, 100, 100); g.drawrect (200, 100, 100, 100); g.drawrect (100, 200, 100, 100); g.drawrect (200, 200, 100, 100); if (firstsquare) g.setcolor (Color.red); g.fillarc(100, 100, 100, 100, 0, 360); if (secondsquare) g.setcolor (Color.blue); g.fillarc(200, 100, 100, 100, 0, 360); if (thirdsquare) g.setcolor (Color.yellow); g.fillarc(100, 200, 100, 100, 0, 360); if (fourthsquare) g.setcolor (Color.green); g.fillarc(200, 200, 100, 100, 0, 360); private class MyMouseListener extends MouseAdapter public void mousepressed (MouseEvent e) public void mouseclicked (MouseEvent e) xpos = e.getx(); ypos = e.gety(); if ((xpos >= 100 && xpos <= 200) && (ypos >= 100 && ypos <= 200)) firstsquare =!firstsquare; if ((xpos >= 200 && xpos <= 300) && (ypos >= 100 && ypos <= 200)) secondsquare =!secondsquare; if ((xpos >= 100 && xpos <= 200) && (ypos >= 200 && ypos <= 300)) thirdsquare =!thirdsquare; if ((xpos >= 200 && xpos <= 300) && (ypos >= 200 && ypos <= 300))

fourthsquare =!fourthsquare; repaint();