/* LogoMaker -- As developed in the 9:30 CSE142 lecture, 4/16/2004, but * completed and with comments. * Note: The class has been renamed LogoMakerII * * Indentation is 2 or 3; a preferred value is 4. Set this in DrJava via * Edit/Preferences/Miscellaneous */ import uwcse.io.Input; import uwcse.graphics.GWindow; import uwcse.graphics.Triangle; import uwcse.graphics.TextShape; import java.awt.Color; import javax.swing.JColorChooser; /** The LogoMaker class helps a user design a logo. * The logo is displayed in a GWindow. There is only one logo visible. * Some of its characteristics can be changed, in particular: * the letter being displayed, * the size (area) of the triangle, * the color of the triangle, and * the color of the letter. * Objects of type LogoMaker can display a logo on a GWindow, * and also allow the user to select colors, choose the size * of the triangle, and specify a letter. * The location of the logo on the window cannot be changed by the user * (the LogoMaker puts it somewhere appropriate). * * It is ambiguous in the instructions whether the "letter" must have * the Java type of char, or whether it could be a String containing * a single character. This implementation assumes the latter. * */ public class LogoMakerII { //----------------- INSTANCE VARIABLES ----------------------- /** A window where logos are displayed. */ private GWindow window; /** The current color of the triangle part of the logo. */ private Color triangleColor; /** The current color of the letter part of the logo. */ private Color letterColor; /** The area of the triangle, in pixels. */ private double area; /** The window coordinates of the upper left corner of the triangle. */ private int ulX, ulY; /** The letter to be displayed as part of the logo. */ private String letter; /** Create a LogoMaker with default values for everything, and display * the initial logo. */ public LogoMakerII () { window = new GWindow("Logo Maker"); area = 3000.0; //specified by the instructions letterColor = Color.BLUE; //specified by the instructions triangleColor = Color.YELLOW; //specified by the instructions ulX = 50; //a rather arbitrary location high up on the window ulY = 20; letter = "X"; //specified by the instructions paintLogo(); // Show the default logo } /* Following is a more elegant way to write the no-parameter constructor. * (Not explained in the textbook until Chapter 10). * public LogoMakerII () { //parameter values are defaults specified in the instructions this(Color.BLUE, Color.YELLOW, 3000.0, "X"); } */ /** Create a logo, using parameter values for colors, area, and letter, * and display the logo using those values. * @param letColor the initial color of the letter * @param triColor the initial color of the triangle * @param initialArea the initial area (in pixels) of the triangle. * @param initialLetter the initial letter to be displayed. */ public LogoMakerII(Color letColor, Color triColor, double initialArea, String initialLetter) { letterColor = letColor; triColor = triColor; area = initialArea; letter = initialLetter; window = new GWindow("Logo Maker"); ulX = 50; //a rather arbitrary location high up on the window ulY = 20; paintLogo(); } /** paints the logo using the current values for colors, size, etc. * Any past logo is erased first. */ public void paintLogo() { window.erase(); double base = Math.sqrt(2 * area); //The triangle is contained within a square bounding box, //so base and height are equal. The casts to int are // needed since Triangle expects ints instead of double Triangle aTri = new Triangle((int)ulX, (int)ulY, (int)(ulX + base), (int)(ulY), (int) (ulX + base/2), (int)(ulY + base), triangleColor, true //fill ); window.add(aTri); // -20 in the following is an ad-hoc adjustment for the text height TextShape aText = new TextShape(letter, (int) (ulX + base/2), (int) (ulY + base/2)-20, letterColor); window.add(aText); } /** Lets the user select the color for the letter, * and then repaints the logo. */ public void changeLetterColor() { JColorChooser chooser = new JColorChooser(); letterColor = chooser.showDialog(null, "Pick a color for the letter", null); paintLogo(); } /** Lets the user select the color for the triangle, * and then repaints the logo. */ public void changeTriangleColor() { JColorChooser chooser = new JColorChooser(); triangleColor = chooser.showDialog(null, "Pick a color for the triangle", null); paintLogo(); } /** Lets the user specify the size (as pixels) of the triangle, and then repaints the logo. */ public void changeArea() { Input ibox = new Input(); area = ibox.readDoubleDialog("Enter a new area: "); paintLogo(); } /** Lets the user select a new letter, * and then repaints the logo. OPTIONAL METHOD. */ public void changeLetter() { Input cBox = new Input(); letter = cBox.readStringDialog("Choose a letter for the logo " + "(anything more than one letter will not be displayed correctly)"); paintLogo(); } } //end class