import uwcse.graphics.*; import uwcse.io.*; import java.awt.*; import javax.swing.*; /** Makes a logo consisting of a letter centered inside a triangle. */ public class LogoMaker { // Window and window properties private GWindow logoWindow; private int windowWidth = 400; private int windowHeight = 400; private int xCenter = windowWidth/2; private int yCenter = windowHeight/2; // Circle and circle properties private Oval circle; private Color circleColor; private int diameter; // Triangle and triangle properties private Triangle logoTriangle; private Color triangleColor; private double triangleArea; private double sideLength; private double x1,y1; private double x2,y2; private double x3,y3; // Letter and letter properties private String letter; private TextShape logoLetter; private Color letterColor; private Font letterFont; private int letterSize; /** * The default constructor sets the triangle area to 3000, the triangle * color to yellow, the letter to 'X' and the letter color to blue. */ public LogoMaker() { triangleColor = Color.YELLOW; triangleArea = 3000; letter = "X"; letterColor = Color.BLUE; // see the comment below initialize(); } /** * Constructor that accepts input for desired logo attributes * @param Color letterColor - the desired color of the letter * @param Color triangleColor - the desired triangle color * @param double triangleArea - the desired triangle area * @param String the letter */ public LogoMaker(Color letterColor, Color triangleColor, double triangleArea, String letter) { this.triangleColor = triangleColor; this.triangleArea = triangleArea; this.letter = letter; this.letterColor = letterColor; //see the comment below initialize(); } /** * This method exsists because there are several places in this class * where the same actions need to be performed. For example, once a * few variables are set in the constructors above, everything that is * done with those variables is the same. Rather than rewriting the * same code over several times (which increases the chances that a * mistake will be made and makes it harder to make changes), it is * better to put all that code into a method and use the method. */ private void initialize() { windowWidth = 400; windowHeight = 400; xCenter = windowWidth/2; yCenter = windowHeight/2; logoWindow = new GWindow("LogoMaker", windowWidth, windowHeight); circleColor = Color.RED; diameter = (Math.min(windowWidth, windowHeight) - 40); circle = new Oval((windowWidth-diameter)/2, (windowHeight-diameter)/2, diameter, diameter, circleColor, true); computeTrianglePoints(); letterSize = 300; paintLogo(); } /** * Again, the actions in this method need to be performed several times * throughout this class, so it makes sense to "wrap up" all these actions * and give that group of actions a name so that we can easily perform * these actions without a lot of typing. That's what a method is for! */ private void computeTrianglePoints() { /* I am going to make my triangle. I want this to be perfectly centered, * but I'm rusty on the whole geometry thing. . . google to the rescue! * * http://mathworld.wolfram.com/EquilateralTriangle.html * * so the area is given by * area = .25*sqrt(3)*sideLength*sideLength * so, * sideLength = sqrt(area/(.25*sqrt(3))) * * I want an equilateral triangle positioned like so: * __ * \/ * * so I want to define three points based on the center of my circle. * from the website above I know R = .3333*sqrt(3)*base * * so the bottom point will be R*[0,-1], the upper right point will be * R*[cos(30),sin(30)] and the upper left point will be R*[-cos(30),sin(30)] * (but java uses radians so replace 30 with Math.PI/3) */ sideLength = Math.sqrt(triangleArea/(.25*Math.sqrt(3))); double R = (1.0/3.0)*Math.sqrt(3.0)*sideLength; x1 = (R * 0.0) + xCenter; y1 = (R * 1.0) + yCenter; x2 = (R * Math.cos(Math.PI/6.0)) + xCenter; y2 = -(R * Math.sin(Math.PI/6.0)) + yCenter; //negative is up! x3 = (R * -Math.cos(Math.PI/6.0)) + xCenter; y3 = -(R * Math.sin(Math.PI/6.0)) + yCenter; //again. . . negative up! } void paintLogo() { logoWindow.erase(); logoTriangle = new Triangle((int)x1,(int)y1, (int)x2,(int)y2, (int)x3,(int)y3, triangleColor, true); letterFont = new Font("courier", Font.PLAIN, letterSize); logoLetter = new TextShape(letter, (int)(xCenter-letterSize/3.5), (int)(yCenter-letterSize/1.5), letterColor, letterFont); logoLetter.setColor(letterColor); logoWindow.add(circle); logoWindow.add(logoTriangle); logoWindow.add(logoLetter); } /** Change the color of the letter. */ void changeLetterColor() { JColorChooser colorChooser = new JColorChooser(); letterColor = colorChooser.showDialog(null,null,null); paintLogo(); } /** Change the color of the triangle. */ void changeTriangleColor() { JColorChooser colorChooser = new JColorChooser(); triangleColor = colorChooser.showDialog(null,null,null); paintLogo(); } /** Change the area of the triangle. */ void changeTriangleArea() { Input inputHandler = new Input(); triangleArea = inputHandler.readDoubleDialog("Enter the desired are of the triangle:"); computeTrianglePoints(); paintLogo(); } }