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();