CHAPTER 7 Graphics This chapter is designed to introduce the basic drawing abilities provided by the Graphics class, and explore the use of color and fonts. It is mostly concerned with the mechanics of the drawing routines, some of which (like drawArc) are not intuitive. Note that this chapter does not discuss Graphical User Interfaces (GUIs) or the event model necessary to create them. Those topics are covered in Chapter 10. Students can, however, draw pictures and even create animations without delving into true GUIs. We find a small digression into graphics at this point a welcome activity for students. Unlike most other chapters, this one can be skipped, postponed, or left for reading as desired. We often cover some of the basic ideas, then leave the rest for reading. Exercises 7-9 Exactly how many colors can you define in a Java program? Why might you not be able to make use of all of them? One can define 16,777,216 (256 * 256 * 256) colors in Java, because each color is defined by three components (red, green, and blue) and each component can have 256 different values. Whether or not you could make use of all of the colors would depend on the hardware capabilities of your system, specifically the capabilities of your monitor and your video card. 7-10 Write a Java declaration that creates a new Color object that represents a shade of purple. Explain how this is accomplished. Color purple = new Color (128, 0, 128); This color was created by using the Color constructor with the three integer parameters. Each parameter gives the red, green, and blue component of the color, respectively. Each parameter may have a value from 0 to 255. In this case, only red and blue was used, each with a value of 128. 7-11 Define the color you created in Problem 7-10 using the Color constructor that accepts floating point values. Not provided. 7-12 Explain XOR mode and how it can be used to erase shapes once they are drawn. Not provided. 7-13 Write four different drawArc method invocations that will draw the bottom half of a circle that has a radius of 10 pixels. Make sure each call draws the same arc in the same position. page.drawArc (10, 10, 20, 20, 180, 180); 85 page.drawArc (10, 10, 20, 20, -180, 180); page.drawArc (10, 10, 20, 20, 0, -180); page.drawArc (10, 10, 20, 20, 360, -180); where page is a previously declared Graphics object. 7-14 Describe the relationship between an arc and an oval as they are drawn in Java. Explain the relationship of these shapes to a rounded rectangle. Give specific examples other than those found in this chapter. An arc can be thought of as a part of an oval. Or, an oval is an arc that sweeps an entire 360° cycle. An oval, or more precisely a portion of an oval (an arc) is used to describe the shape of the corner of a rounded rectangle. The following Java code will print two identical ovals in the same location: page.drawArc (150, 10, 100, 50, 0, 360); page.drawRoundRect (10, 10, 100, 50, 100, 50); Programming Projects 7-15 Design and implement an applet that draws a red stop sign with black letters. //******************************************************************* // // Stop_Sign.java Programming Project Applet // // Authors: Lewis and Loftus // // Classes: Stop_Sign // //******************************************************************* import java.awt.*; public class Stop_Sign extends java.applet.Applet { private int[] xlist = {10, 10, 60, 110, 160, 160, 110, 60, 10}; private int[] ylist = {60, 110, 160, 160, 110, 60, 10, 10, 60}; //=========================================================== // Draws a red stop sign with black letters. //=========================================================== public void paint(Graphics page){ page.setColor (Color.red); page.fillPolygon (xlist, ylist, 9); 86 Chapter 7 Graphics page.setColor (Color.black); page.setFont (new Font ("Helvetica", Font.BOLD, 24)); page.drawString ("S T O P", 45, 90); } } // method paint // class Stop_Sign 7-16 Design and implement an applet that draws a bullseye by drawing five concentric circles centered around two lines forming a crosshair. Fill every other circle with a different color. //******************************************************************* // // Bullseye.java Programming Project Applet // // Authors: Lewis and Loftus // // Classes: Bullseye // //******************************************************************* import java.awt.*; public class Bullseye extends java.applet.Applet { private final int THICKNESS= 40, NUM_CIRCLES = 5; //=========================================================== // Draws a bullseye with a crosshair. //=========================================================== public void paint (Graphics page) { int location, circle_size; for (int count = 1; count <= NUM_CIRCLES; count++) { // determine color of circle if (count % 2 != 0) page.setColor(Color.red); else page.setColor(Color.yellow); 87 // locate then draw the next circle location = THICKNESS / 2 * count; circle_size = (NUM_CIRCLES + 1 - count) * THICKNESS; page.fillOval (location, location, circle_size, circle_size); } // draw the crosshair page.setColor(Color.black); page.drawLine(20, 120, 220, 120); page.drawLine(120, 20, 120, 220); } } // method paint // class Bullseye 7-17 Design and implement an applet that draws a square using the drawRoundRect method. //******************************************************************* // // Square.java Programming Project Applet // // Authors: Lewis and Loftus // // Classes: Square // //******************************************************************* import java.awt.*; public class Square extends java.applet.Applet { //=========================================================== // Draws a square using the drawRoundRect method. //=========================================================== public void paint (Graphics page) { page.drawRoundRect (20, 20, 200, 200, 0, 0); } } // method paint // class Square 7-18 Design and implement an applet that draws a round rectangle using only lines and arcs. 88 Chapter 7 Graphics //******************************************************************* // // Rounded.java Programming Project Applet // // Authors: Lewis and Loftus // // Classes: Rounded // //******************************************************************* import java.awt.*; public class Rounded extends java.applet.Applet { //=========================================================== // Draws a rounded rectangle using only lines and arcs. //=========================================================== public void paint (Graphics page){ // draw the top left arc page.drawArc (20, 20, 40, 20, 180, -90); // draw the top right arc page.drawArc (130, 20, 40, 20, 90, -90); // draw the bottom left arc page.drawArc (20, 100, 40, 20, 180, 90); // draw the bottom right arc page.drawArc (130, 100, 40, 20, 270, 90); // draw left side page.drawLine (20, 30, 20, 110); // draw top side page.drawLine (40, 20, 150, 20); // draw right side page.drawLine (170, 30, 170, 110); // draw bottom side page.drawLine (40, 120, 150, 120); 89 } } // method paint // class Rounded 7-19 Design and implement an applet that appears to draw a circle divided into eight equal "pie wedge" sections. Color each section with a different color. Hint: use the fillArc method. //******************************************************************* // // Pie.java Programming Project Applet // // Authors: Lewis and Loftus // // Classes: Pie // //******************************************************************* import java.awt.*; public class Pie extends java.applet.Applet { //=========================================================== // Draws a pie with multi-colored wedges. //=========================================================== public void paint (Graphics page) { page.setColor (Color.red); page.fillArc (10, 10, 200, 200, 0, 45); page.setColor (Color.yellow); page.fillArc (10, 10, 200, 200, 45, 45); page.setColor (Color.green); page.fillArc (10, 10, 200, 200, 90, 45); page.setColor (Color.blue); page.fillArc (10, 10, 200, 200, 135, 45); page.setColor (Color.gray); page.fillArc (10, 10, 200, 200, 180, 45); page.setColor (Color.cyan); page.fillArc (10, 10, 200, 200, 225, 45); page.setColor (Color.black); 90 Chapter 7 Graphics page.fillArc (10, 10, 200, 200, 270, 45); page.setColor (Color.pink); page.fillArc (10, 10, 200, 200, 315, 45); } } // method paint // class Pie 7-20 Design and implement an applet that defines a class called My_Graphics that contains a method called my_drawRoundRect, which implements the equivalent functionality as the drawRoundRect method. Instantiate and test your class. //******************************************************************* // // Rounded2.java Programming Project Applet // // Authors: Lewis and Loftus // // Classes: Rounded2 // My_Graphics // //******************************************************************* import java.awt.*; //------------------------------------------------------------------// // Class Rounded2 contains a paint method that tests a special // graphics method. // // Methods: // // public void paint (Graphics page) // //------------------------------------------------------------------public class Rounded2 extends java.applet.Applet { //=========================================================== // Creates a My_Graphics object and tests the method // called my_drawRoundRect. //=========================================================== public void paint (Graphics page) { 91 My_Graphics mine = new My_Graphics(); mine.my_drawRoundRect (page, 20, 20, 150, 100, 75, 25); } } // method paint // class Rounded2 //------------------------------------------------------------------// // Class My_Graphics represents a personalized class of graphics // routines. // // Methods: // // public void my_drawRoundRect (Graphics page, int x, int y, // int width, int height, int arc_width, int arc_height) // //------------------------------------------------------------------class My_Graphics { //=========================================================== // Draws a rounded rectangle. It is essentially equivalent // to Graphics.drawRoundRect. //=========================================================== public void my_drawRoundRect (Graphics page, int x, int y, int width, int height, int arc_width, int arc_height) { // // // // x & y are the coordinates of the top left corner. width & height are the width & height of the rectangle. arc_width & arc_height are the width & height of the oval that creates each corner of the round rectangle. // draw the top left arc page.drawArc (x, y, arc_width, arc_height, 180, -90); // draw the top right arc page.drawArc (x + width - arc_width, y, arc_width, arc_height, 90, -90); // draw the bottom left arc page.drawArc (x, y + height - arc_height, arc_width, arc_height, 180, 90); 92 Chapter 7 Graphics // draw the bottom right arc page.drawArc (x + width - arc_width, y + height - arc_height, arc_width, arc_height, 270, 90); // draw left side page.drawLine (x, y + arc_height/2, x, y + height - arc_height/2); // draw top side page.drawLine (x + arc_width/2, y, x + width - arc_width/2, y); // draw right side page.drawLine (x + width, y + arc_height/2, x + width, y + height - arc_height/2); // draw bottom side page.drawLine (x + arc_width/2, y + height, x + width - arc_width/2, y + height); } } // method my_drawRoundRect // class My_Graphics 7-21 Design and implement an applet that simulates the actions of a traffic light. Use the drawRect method to create the background of the traffic light and the drawOval method to create the three lamps. Apply a dark initial color to each lamp, then in a sequence that simulates the normal operation of a traffic light, change the color of the lamps. Repeat the sequence several times. Not provided. 7-22 Design and implement an applet that simulates a yo-yo moving up and down its string. Allow the yo-yo to eventually come to rest at the string’s full length. Use the Bouncing_Ball applet as a guide. Not provided. 7-23 Design and implement an applet that simulates the swinging movement of a pendulum. Not provided. 7-24 A clacker is a desktop toy that has five metal balls suspended on wires and lined up in a row. When one ball on one end is raised and released, it strikes the other balls, which sends the ball on the other end bouncing away. When that ball returns, the original ball rebounds in the same way. This process continues, with the bouncing balls moving less distance each time, until they eventually come to rest. Design and implement an applet that simulates the movement of a clacker. Not provided. 93 7-25 Modify the program in Problem 7-24 so that the clacker movement is simulated as two balls are raised and released, which causes the two balls on the other end to respond accordingly. Not provided. 7-26 Design and implement an applet in which a rubber ball appears to bounce along a floor from left to right. Use the setXORMode method to erase the ball as it moves. Store the coordinates of the ball’s path in an array. //******************************************************************* // // Rubber_Ball.java Programming Project Applet // // Authors: Lewis and Loftus // // Classes: Rubber_Ball // //******************************************************************* import java.awt.*; public class Rubber_Ball extends java.applet.Applet { // number of times the ball will bounce back & forth private final int REVOLUTIONS = 5; private int iterations; private int[] xlist = {0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180}; //=========================================================== // Sets up key characteristics of the animation. //=========================================================== public void init() { resize (200, 200); // set up numer of iterations for entire animation iterations = (REVOLUTIONS * (2 * xlist.length - 2)) + 1; } // method init //=========================================================== // Performs a simple animation of a ball bouncing between // two walls. 94 Chapter 7 Graphics //=========================================================== public void paint (Graphics page) { int size = 20, y = 100, count = 0, increment = 1; page.setXORMode(getBackground()); page.setColor(Color.red); for (int index = 1; index <= iterations; index++) { // draw ball page.fillOval (xlist[count], y, size, size); // pause for (int pause = 0; pause < 50000; pause++) ; // erase ball page.fillOval (xlist[count], y, size, size); count += increment; if (count == 0 || count == (xlist.length) - 1) increment *= -1; } } } // method paint // class Rubber_Ball 7-27 Design and implement an applet that simulates an aerial view of a ball rolling around a rectangular box and rebounding off of the walls. Allow the simulation to run until the ball has rebounded 25 times. Not provided. 95 96 Chapter 7 Graphics