Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Solutions Manual: Chapter 4 Big Java, by Cay Horstmann 1 Review Exercises R4.1 An applet is a Java program that runs inside a browser. An application is a program that runs outside a browser. R4.3 Applets are designed to be embedded inside web pages, which are written in HTML. Thus, you need to supply an HTML page with instructions to load the applet and how large its display area should be. R4.5 The designers of Java did not want to inconvenience those programmers who had produced programs that used simple graphics from the Graphics class after they developed the newer, more powerful Graphics2D class, so they did not change the parameter of the paint method to Graphics2D. R4.7 You specify a text color simply by calling the setColor method of the graphics context before calling the drawString method. R4.9 In a monospaced font, all characters have the same width. In a proportionally spaced font, different characters can have different widths like l and m. R4.11 A logical font face is one of the font faces Serif SansSerif Monospaced Dialog DialogInput R4.13 The following classes are used in this chapter to draw graphical shapes: Rectangle Rectangle2D.Double Ellipse2D.Double Line2D.Double R4.15 Choose x-units from 0 to 4 and y-units from 1 to n, where n is the number of students in your class. R4.17 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann final double WIDTH = 20; final double HEIGHT = 30; // draw X Line2D.Double up = new Line2D.Double(0, HEIGHT, WIDTH, 0); Line2D.Double down = new Line2D.Double(0, 0, WIDTH, HEIGHT); g2.draw(up); g2.draw(down); // draw T Line2D.Double horiz = new Line2D.Double(0, 0, WIDTH, 0); Line2D.Double vert = new Line2D.Double(WIDTH / 2, 0, WIDTH / 2, HEIGHT); g2.draw(horiz); g2.draw(vert); R4.19 If x is 30, then we need to compute r * r - (x - a) * (x - a) Since a = b = r = 100, the value is 100 * 100 - 70 * 70 = 5100 The square root is 71.41. Therefore, y1 = b + root = 171.41 y2 = b - root = 28.59 Programming Exercises P4.1 ExP4_1.java import import import import import import import java.applet.Applet; java.awt.Font; java.awt.Color; java.awt.Graphics; java.awt.Graphics2D; java.awt.font.FontRenderContext; java.awt.geom.Rectangle2D; /** This applet draws a name in red centered inside a blue rectangle that is centered in the applet window. */ public class ExP4_1 extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; //select the font into the graphics context 2 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann final int HUGE_SIZE = 48; Font hugeFont = new Font("Serif",Font.BOLD,HUGE_SIZE); g2.setFont(hugeFont); String message ="Your Name"; //measure the string FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = hugeFont.getStringBounds(message,context); double yMessageAscent = -bounds.getY(); double yMessageDescent = bounds.getHeight() + bounds.getY(); double yMessageHeight = bounds.getHeight(); double xMessageWidth = bounds.getWidth(); //center the message in the window double xLeft = (getWidth() - xMessageWidth) / 2; double yTop = (getHeight() - yMessageHeight) / 2; double yBase = yTop + yMessageAscent; //draw the blue rectangle g2.setColor(Color.blue); final int EXTRA = 20; Rectangle2D.Double r = new Rectangle2D.Double(xLeft - EXTRA, yTop - EXTRA, xMessageWidth + 2 * EXTRA, yMessageHeight + 2 * EXTRA); g2.fill(r); //draw the name inside the rectangle g2.setColor(Color.red); g2.drawString(message,(float)xLeft,(float)yBase); } } P4.3 ExP4_3.java import import import import java.applet.Applet; java.awt.Color; java.awt.Graphics; java.awt.Graphics2D; /** This applet draws twelve strings, one each for the 12 standard colors 3 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann */ public class ExP4_3 extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; final float DISTANCE = 20; float yBase = DISTANCE / 2; g2.setColor(Color.black); g2.drawString("black", 0, yBase); yBase = yBase + DISTANCE; g2.setColor(Color.blue); g2.drawString("blue", 0, yBase); yBase = yBase + DISTANCE; g2.setColor(Color.cyan); g2.drawString("cyan", 0, yBase); yBase = yBase + DISTANCE; g2.setColor(Color.gray); g2.drawString("gray", 0, yBase); yBase = yBase + DISTANCE; g2.setColor(Color.darkGray); g2.drawString("darkGray", 0, yBase); yBase = yBase + DISTANCE; g2.setColor(Color.lightGray); g2.drawString("lightGray", 0, yBase); yBase = yBase + DISTANCE; g2.setColor(Color.green); g2.drawString("green", 0, yBase); yBase = yBase + DISTANCE; g2.setColor(Color.magenta); g2.drawString("magenta", 0, yBase); yBase = yBase + DISTANCE; g2.setColor(Color.orange); g2.drawString("orange", 0, yBase); yBase = yBase + DISTANCE; g2.setColor(Color.pink); g2.drawString("pink", 0, yBase); yBase = yBase + DISTANCE; g2.setColor(Color.red); g2.drawString("red", 0, yBase); yBase = yBase + DISTANCE; 4 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann g2.setColor(Color.yellow); g2.drawString("yellow", 0, yBase); yBase = yBase + DISTANCE; } } P4.5 ExP4_5.java import import import import import import java.applet.Applet; java.awt.Color; java.awt.Graphics; java.awt.Graphics2D; java.awt.geom.Ellipse2D; javax.swing.JOptionPane; /** This applet draws two solid circles: one in pink and one a custom colored purple. */ public class ExP4_5 extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; double radius = 50; // draw a pink circle Ellipse2D.Double circle = new Ellipse2D.Double(0, 0, 2 * radius, 2 * radius); g2.setColor(Color.pink); g2.fill(circle); // draw a custom colored purple circle Ellipse2D.Double circle2 = new Ellipse2D.Double(0, 2 * radius, 2 * radius, 2 * radius); g2.setColor(new Color(0.7F, 0, 0.7F)); g2.fill(circle2); } } P4.7 ExP4_7.java import import import import import java.applet.Applet; java.awt.Color; java.awt.Graphics; java.awt.Graphics2D; java.awt.geom.Ellipse2D; /** This applet fills the applet window with a large ellipse that resizes when the window is resized */ public class ExP4_7 extends Applet 5 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Ellipse2D.Double ellipse = new Ellipse2D.Double(0, 0, getWidth(), getHeight()); g2.setColor(Color.pink); g2.fill(ellipse); } } P4.9 House.java import import import import import import java.awt.Graphics; java.awt.Graphics2D; java.awt.geom.Rectangle2D; java.awt.geom.Ellipse2D; java.awt.geom.Line2D; java.awt.geom.Point2D; /** This class draws a house by allowing the user to specify houses of different sizes */ public class House { /** Constructor to initiate the bottom left corner, the width, and the height @param x the left corner coordinate @param y the bottom corner coordinate @param w the width of the house @param h the height of the house */ public House(int x, int y, int w, int h) { xleft = x; ybottom = y; width = w; height = h; } /** Draws the house @param g2 the graphics context */ public void draw(Graphics2D g2) { Rectangle2D.Double front = new Rectangle2D.Double(xleft, ybottom - width, width, width); 6 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann Rectangle2D.Double door = new Rectangle2D.Double(xleft + width / 5, ybottom - width / 2, width / 5, width / 2); Rectangle2D.Double window = new Rectangle2D.Double(xleft + width * 3 / 5, ybottom - width / 2, width / 5, width / 5); Line2D.Double roofLeft = new Line2D.Double(xleft, ybottom - width, xleft + width / 2, ybottom - height); Line2D.Double roofRight = new Line2D.Double(xleft + width, ybottom - width, xleft + width / 2, ybottom - height); g2.draw(front); g2.draw(door); g2.draw(window); g2.draw(roofLeft); g2.draw(roofRight); } private private private private int int int int xleft; ybottom; width; height; } ExP4_9.java import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; /** This applet displays a house by allowing the user to specify houses of different sizes */ public class ExP4_9 extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; House house1 = new House(100, 150, 100, 120); House house2 = new House(200, 200, 50, 70); House house3 = new House(260, 260, 20, 25); house1.draw(g2); house2.draw(g2); house3.draw(g2); } } 7 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann P4.11 LetterH.java import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; /** The letter H of the word HELLO */ public class LetterH { /** Constructs a letter H at the given x and y coordinate. @param x the x-coordinate @param y the y-coordinate */ public LetterH(double anX, double aY) { x = anX; y = aY; } /** Draws the letter H. @param g2 the graphics context */ public void draw(Graphics2D g2) { final int WIDTH = 30; final int HEIGHT = 50; Line2D.Double vert1 = new Line2D.Double(x, y, x, y + HEIGHT); Line2D.Double vert2 = new Line2D.Double(x + WIDTH, y, x + WIDTH, y + HEIGHT); Line2D.Double horiz = new Line2D.Double(x, y + HEIGHT / 2, x + WIDTH, y + HEIGHT / 2); g2.draw(vert1); g2.draw(vert2); g2.draw(horiz); } double x; double y; } LetterE.java import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; 8 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann /** The letter E of the word HELLO */ public class LetterE { /** Constructs a letter E at the given x and y coordinate. @param x the x-coordinate @param y the y-coordinate */ public LetterE(double anX, double aY) { x = anX; y = aY; } /** Draws the letter E. @param g2 the graphics context */ public void draw(Graphics2D g2) { final int WIDTH = 30; final int HEIGHT = 50; Line2D.Double horiz1 = new Line2D.Double(x, y, x + WIDTH, y); Line2D.Double horiz2 = new Line2D.Double(x, y + HEIGHT / 2, x + WIDTH, y + HEIGHT / 2); Line2D.Double horiz3 = new Line2D.Double(x, y + HEIGHT, x + WIDTH, y + HEIGHT); Line2D.Double vert = new Line2D.Double(x, y, x, y + HEIGHT); g2.draw(horiz1); g2.draw(horiz2); g2.draw(horiz3); g2.draw(vert); } double x; double y; } LetterL.java import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; /** The letter L of the word HELLO */ 9 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann public class LetterL { /** Constructs a letter L at the given x and y coordinate. @param x the x-coordinate @param y the y-coordinate */ public LetterL(double anX, double aY) { x = anX; y = aY; } /** Draws the letter L. @param g2 the graphics context */ public void draw(Graphics2D g2) { final int WIDTH = 30; final int HEIGHT = 50; Line2D.Double vert = new Line2D.Double(x, y, x, y + HEIGHT); Line2D.Double horiz = new Line2D.Double(x, y + HEIGHT, x + WIDTH, y + HEIGHT); g2.draw(vert); g2.draw(horiz); } double x; double y; } LetterO.java import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; /** The letter O of the word HELLO */ public class LetterO { /** Constructs a letter O at the given x and y coordinate. @param x the x-coordinate @param y the y-coordinate */ public LetterO(double anX, double aY) { x = anX; 10 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann y = aY; } /** Draws the letter O. @param g2 the graphics context */ public void draw(Graphics2D g2) { final int WIDTH = 30; final int HEIGHT = 50; Ellipse2D.Double e = new Ellipse2D.Double(x, y, WIDTH, HEIGHT); g2.draw(e); } double x; double y; } ExP4_11.java import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; /** This applet displays the word HELLO */ public class ExP4_11 extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; final int WIDTH = 30; final int SPACING = 5; double x = 0; double y = 0; LetterH h = new LetterH(x, y); h.draw(g2); x = x + WIDTH + SPACING; LetterE e = new LetterE(x, y); e.draw(g2); x = x + WIDTH + SPACING; LetterL l1 = new LetterL(x, y); l1.draw(g2); 11 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann x = x + WIDTH + SPACING; LetterL l2 = new LetterL(x, y); l2.draw(g2); x = x + WIDTH + SPACING; LetterO o = new LetterO(x, y); o.draw(g2); } } P4.13 PieSlice.java import java.awt.Graphics2D; import java.awt.geom.Arc2D; /** This class draws a pie chart with given information */ public class PieSlice { /** Constructs a pie chart @param aRadius radius of the circle @param aStartAngle starting angle of the slice @param anAngle angle of the slice @param name the name of the slice */ public PieSlice(double aRadius, double aStartAngle, double anAngle, String aName) { radius = aRadius; startAngle = aStartAngle; angle = anAngle; name = aName; } /** Draws the pie chart @param g2 the graphics content */ public void draw(Graphics2D g2) { Arc2D.Double arc = new Arc2D.Double(0, 0, 2 * radius, 2 * radius, startAngle, angle, Arc2D.PIE); g2.draw(arc); double stringAngle = Math.toRadians(startAngle + angle / 2); 12 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann g2.drawString(name, (float)(radius + radius * Math.cos(stringAngle)), (float)(radius + radius * Math.sin(stringAngle))); } private private private private double double double String radius; startAngle; angle; name; } PieChart.java import import import import java.awt.Graphics; java.awt.Graphics2D; java.awt.geom.Ellipse2D; java.awt.geom.Line2D; /** This class draws a pie chart with given information */ public class PieChart { /** Constructs a pie chart @param aTotalAngle total angle of the chart @param aRadius radius of the circle */ public PieChart(double aTotalAngle, double aRadius) { totalAngle = aTotalAngle; radius = aRadius; } /** Draws the pie chart @param g2 the graphics content @param name the name of the city @param fraction the fraction of the entire pie */ public void drawPie(Graphics2D g2, String name, double fraction) { double angle = fraction * 2 * Math.PI; Line2D.Double segment = new Line2D.Double(radius, radius, radius + radius * Math.cos(totalAngle), radius + radius * Math.sin(totalAngle)); g2.draw(segment); totalAngle += angle / 2; g2.drawString(name, (float)(radius + radius * Math.cos(totalAngle)), (float)(radius + radius * Math.sin(totalAngle))); 13 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann totalAngle += angle / 2; } private double totalAngle; private double radius; } ExP4_13.java import import import import import java.applet.Applet; java.awt.Graphics; java.awt.Graphics2D; java.awt.geom.Ellipse2D; java.awt.geom.Line2D; /** This applet displays a pie chart */ public class ExP4_13 extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; double xwidth = getWidth() - 1; double yheight = getHeight() - 1; double radius = Math.min(xwidth, yheight) / 2; final final final final double double double double GOLDEN_GATE = 4200; BROOKLYN = 1595; DELAWARE_MEMORIAL = 2150; MACKINAC = 3800; double total = GOLDEN_GATE + BROOKLYN + DELAWARE_MEMORIAL + MACKINAC; double startAngle = 0; double angle = 360 * GOLDEN_GATE / total; PieSlice slice1 = new PieSlice(radius, startAngle, angle, "Golden Gate"); startAngle = startAngle + angle; angle = 360 * BROOKLYN / total; PieSlice slice2 = new PieSlice(radius, startAngle, angle, "Brooklyn"); startAngle = startAngle + angle; angle = 360 * DELAWARE_MEMORIAL / total; PieSlice slice3 = new PieSlice(radius, startAngle, 14 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann angle, "Delaware Memorial"); startAngle = startAngle + angle; angle = 360 * MACKINAC / total; PieSlice slice4 = new PieSlice(radius, startAngle, angle, "Mackinac"); slice1.draw(g2); slice2.draw(g2); slice3.draw(g2); slice4.draw(g2); } } P4.15 Clock.java import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; /** This class draws a clock */ public class Clock { /** Constructs a clock @param anHour the hour hand of a clock @param aMinute the minute hand of a clock */ public Clock(int anHour, int aMinute) { hours = anHour; minutes = aMinute; } /** Draws a clock @param g2 the graphics content */ public void draw(Graphics2D g2) { final double RADIUS = 100; final double MIN_HAND_LENGTH = 90; final double HOUR_HAND_LENGTH = 80; Ellipse2D.Double circle = new Ellipse2D.Double(0, 0, 2 * RADIUS, 2 * RADIUS); g2.draw(circle); final double MINUTES_PER_HOUR = 60; final double MINUTES_PER_360 = 60 * 12; double minAngle = Math.PI / 2 - 2 * Math.PI 15 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann * minutes / MINUTES_PER_HOUR; // draw the minutes hand Line2D.Double minHand = new Line2D.Double(RADIUS, RADIUS, RADIUS + MIN_HAND_LENGTH * Math.cos(minAngle), RADIUS - MIN_HAND_LENGTH * Math.sin(minAngle)); double hourAngle = Math.PI / 2 - 2 * Math.PI * (hours * MINUTES_PER_HOUR + minutes) / MINUTES_PER_360; // draw the hours hand Line2D.Double hourHand = new Line2D.Double(RADIUS, RADIUS, RADIUS + HOUR_HAND_LENGTH * Math.cos(hourAngle), RADIUS - HOUR_HAND_LENGTH * Math.sin(hourAngle)); g2.draw(minHand); g2.draw(hourHand); } private int hours; private int minutes; } ExP4_15.java import import import import java.applet.Applet; java.awt.Graphics; java.awt.Graphics2D; javax.swing.JOptionPane; /** This applet displays a clock */ public class ExP4_15 extends Applet { public void init() { String input = JOptionPane.showInputDialog("Enter time as hh:mm"); hours = Integer.parseInt(input.substring(0, 2)); minutes = Integer.parseInt(input.substring(3, 5)); } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Clock clock = new Clock(hours, minutes); clock.draw(g2); } private int hours; 16 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann private int minutes; } P4.17 Car.java import import import import import import java.awt.Graphics2D; java.awt.Color; java.awt.geom.Ellipse2D; java.awt.geom.Line2D; java.awt.geom.Point2D; java.awt.geom.Rectangle2D; /** A car shape that can be positioned anywhere on the screen. */ public class Car { /** Constructs a car with a given top left corner. @param x the x-coordinate of the top left corner @param y the y-coordinate of the top left corner */ public Car(double x, double y, Color c) { xLeft = x; yTop = y; color = c; } /** Draws the car. @param g2 the graphics context */ public void draw(Graphics2D g2) { Rectangle2D.Double body = new Rectangle2D.Double(xLeft, yTop + 10, 60, 10); Ellipse2D.Double frontTire = new Ellipse2D.Double(xLeft + 10,yTop + 20, 10, 10); Ellipse2D.Double rearTire = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10); //the bottom of the windshield Point2D.Double r1 = new Point2D.Double(xLeft + //the front of the roof Point2D.Double r2 = new Point2D.Double(xLeft + //the rear of the roof Point2D.Double r3 = new Point2D.Double(xLeft + //the bottom of the rear window Point2D.Double r4 = new Point2D.Double(xLeft + Line2D.Double frontWindshield 10, yTop + 10); 20, yTop); 40, yTop); 50, yTop + 10); 17 Solutions Manual: Chapter 4 Big Java, by Cay Horstmann = new Line2D.Double(r1, r2); Line2D.Double roofTop = new Line2D.Double(r2, r3); Line2D.Double rearWindow = new Line2D.Double(r3, r4); g2.setColor(color); g2.draw(body); g2.draw(frontTire); g2.draw(rearTire); g2.draw(frontWindshield); g2.draw(roofTop); g2.draw(rearWindow); } private double xLeft; private double yTop; private Color color; } ExP4_17.java import import import import java.applet.Applet; java.awt.Color; java.awt.Graphics; java.awt.Graphics2D; /** This applet draws two cars each with a different color. */ public class ExP4_17 extends Applet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Car car1 = new Car(100, 100, Color.blue); Car car2 = new Car(200, 200, Color.red); car1.draw(g2); car2.draw(g2); } } 18