Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 32 of 32

Thread: Java Programming Blue J 0_o

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Programming Blue J 0_o

    how to separate 2 constructors in the same Square class
    You need to decide on these questions:
    What is each constructor supposed to do?
    What arguments need to be passed to each constructor?

    When you have answers to those questions, you should be able to write the code for the constructors and the code to call each constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Junior Member
    Join Date
    Nov 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Programming Blue J 0_o

    So yay, fixed it right before i went to work.

    basically i kept making an error with
    this.color = "color";

    instead what i should of used was
    this.color = color;


    So all of that for "". As you can probably tell i was rather annoyed with myself. Would you be able to check my code over one last time to see if everything is okay (i promise i know i must be a pain in "something")

    /**
     * Write a description of class MyPicture here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class MyPicture
    {
     
            private Square square1;
            private Square square2;
            private Square square3;
            private Square square4;
            private Circle circle1;
            private Triangle triangle1;
     
     
        /**
         * Constructor for objects of class MyPicture
         * 
         */
     
     
        /**
         * Instantiates (creates) a new MyPicture object. This is a default constructor
         * that only gives values to one of the object attributes - a circle
         * @param  y   a sample parameter for a method
         * @return     the sum of x and y 
         * 
         */
     
        public MyPicture()
        { 
     
            circle1 = new Circle();
            circle1.changeColor("yellow");
            circle1.makeVisible();
            square1 = new Square(110, 60, 90, "black");
            square2 = new Square(30, 70, 110, "blue");
            triangle1 = new Triangle(50, 150, 114, 40, "green");
     
     
     
     
     
        }
        /**
         * creates a one Window House
         */
        public void createOneWindowHouse(){
            square1 = new Square(110, 60, 90, "red");
            square2 = new Square(30, 70, 110, "blue");
            triangle1 = new Triangle(50, 150, 114, 40, "green");
            square1.makeVisible();
            square2.makeVisible();
            triangle1.makeVisible();
        }
     
     
     
     
     
     
     
       }

    import javax.swing.*;
    import java.awt.*;
    import java.util.List;
    import java.util.*;
     
    /**
     * Canvas is a class to allow for simple graphical drawing on a canvas.
     * This is a modification of the general purpose Canvas, specially made for
     * the BlueJ "shapes" example. 
     *
     * @author: Bruce Quig
     * @author: Michael Kölling (mik)
     *
     * @version: 1.6 (shapes)
     */
    public class Canvas
    {
        // Note: The implementation of this class (specifically the handling of
        // shape identity and colors) is slightly more complex than necessary. This
        // is done on purpose to keep the interface and instance fields of the
        // shape objects in this project clean and simple for educational purposes.
     
        private static Canvas canvasSingleton;
     
        /**
         * Factory method to get the canvas singleton object.
         */
        public static Canvas getCanvas()
        {
            if(canvasSingleton == null) {
                canvasSingleton = new Canvas("BlueJ Shapes Demo", 300, 300, 
                        Color.white);
            }
            canvasSingleton.setVisible(true);
            return canvasSingleton;
        }
     
        //  ----- instance part -----
     
        private JFrame frame;
        private CanvasPane canvas;
        private Graphics2D graphic;
        private Color backgroundColour;
        private Image canvasImage;
        private List objects;
        private HashMap shapes;
     
        /**
         * Create a Canvas.
         * @param title  title to appear in Canvas Frame
         * @param width  the desired width for the canvas
         * @param height  the desired height for the canvas
         * @param bgClour  the desired background colour of the canvas
         */
        private Canvas(String title, int width, int height, Color bgColour)
        {
            frame = new JFrame();
            canvas = new CanvasPane();
            frame.setContentPane(canvas);
            frame.setTitle(title);
            canvas.setPreferredSize(new Dimension(width, height));
            backgroundColour = bgColour;
            frame.pack();
            objects = new ArrayList();
            shapes = new HashMap();
        }
     
        /**
         * Set the canvas visibility and brings canvas to the front of screen
         * when made visible. This method can also be used to bring an already
         * visible canvas to the front of other windows.
         * @param visible  boolean value representing the desired visibility of
         * the canvas (true or false) 
         */
        public void setVisible(boolean visible)
        {
            if(graphic == null); {
                // first time: instantiate the offscreen image and fill it with
                // the background colour
                Dimension size = canvas.getSize();
                canvasImage = canvas.createImage(size.width, size.height);
                graphic = (Graphics2D)canvasImage.getGraphics();
                graphic.setColor(backgroundColour);
                graphic.fillRect(0, 0, size.width, size.height);
                graphic.setColor(Color.black);
            }
            frame.setVisible(visible);
        }
     
        /**
         * Draw a given shape onto the canvas.
         * @param  referenceObject  an object to define identity for this shape
         * @param  color            the color of the shape
         * @param  shape            the shape object to be drawn on the canvas
         */
         // Note: this is a slightly backwards way of maintaining the shape
         // objects. It is carefully designed to keep the visible shape interfaces
         // in this project clean and simple for educational purposes.
        public void draw(Object referenceObject, String color, Shape shape)
        {
            objects.remove(referenceObject);   // just in case it was already there
            objects.add(referenceObject);      // add at the end
            shapes.put(referenceObject, new ShapeDescription(shape, color));
            redraw();
        }
     
        /**
         * Erase a given shape's from the screen.
         * @param  referenceObject  the shape object to be erased 
         */
        public void erase(Object referenceObject)
        {
            objects.remove(referenceObject);   // just in case it was already there
            shapes.remove(referenceObject);
            redraw();
        }
     
        /**
         * Set the foreground colour of the Canvas.
         * @param  newColour   the new colour for the foreground of the Canvas 
         */
        public void setForegroundColor(String colorString)
        {
            if(colorString.equals("red"))
                graphic.setColor(Color.red);
            else if(colorString.equals("black"))
                graphic.setColor(Color.black);
            else if(colorString.equals("blue"))
                graphic.setColor(Color.blue);
            else if(colorString.equals("yellow"))
                graphic.setColor(Color.yellow);
            else if(colorString.equals("green"))
                graphic.setColor(Color.green);
            else if(colorString.equals("magenta"))
                graphic.setColor(Color.magenta);
            else if(colorString.equals("white"))
                graphic.setColor(Color.white);
            else
                graphic.setColor(Color.black);
        }
     
        /**
         * Wait for a specified number of milliseconds before finishing.
         * This provides an easy way to specify a small delay which can be
         * used when producing animations.
         * @param  milliseconds  the number 
         */
        public void wait(int milliseconds)
        {
            try
            {
                Thread.sleep(milliseconds);
            } 
            catch (Exception e)
            {
                // ignoring exception at the moment
            }
        }
     
        /**
         * Redraw ell shapes currently on the Canvas.
         */
        private void redraw()
        {
            erase();
            for(Iterator i=objects.iterator(); i.hasNext(); ) {
                ((ShapeDescription)shapes.get(i.next())).draw(graphic);
            }
            canvas.repaint();
        }
     
        /**
         * Erase the whole canvas. (Does not repaint.)
         */
        private void erase()
        {
            Color original = graphic.getColor();
            graphic.setColor(backgroundColour);
            Dimension size = canvas.getSize();
            graphic.fill(new Rectangle(0, 0, size.width, size.height));
            graphic.setColor(original);
        }
     
     
        /************************************************************************
         * Inner class CanvasPane - the actual canvas component contained in the
         * Canvas frame. This is essentially a JPanel with added capability to
         * refresh the image drawn on it.
         */
        private class CanvasPane extends JPanel
        {
            public void paint(Graphics g)
            {
                g.drawImage(canvasImage, 0, 0, null);
            }
        }
     
        /************************************************************************
         * Inner class CanvasPane - the actual canvas component contained in the
         * Canvas frame. This is essentially a JPanel with added capability to
         * refresh the image drawn on it.
         */
        private class ShapeDescription
        {
            private Shape shape;
            private String colorString;
     
            public ShapeDescription(Shape shape, String color)
            {
                this.shape = shape;
                colorString = color;
            }
     
            public void draw(Graphics2D graphic)
            {
                setForegroundColor(colorString);
                graphic.fill(shape);
            }
        }
     
    }

    import java.awt.*;
    import java.awt.geom.*;
     
    /**
     * A circle that can be manipulated and that draws itself on a canvas.
     * 
     * @author  Michael Kölling and David J. Barnes
     * @version 1.0  (15 July 2000)
     */
     
    public class Circle
    {
        private int diameter;
        private int xPosition;
        private int yPosition;
        private String color;
        private boolean isVisible;
     
        /**
         * Create a new circle at default position with default color.
         */
        public Circle()
        {
            this.diameter = 30;
            this.xPosition = 20;
            this.yPosition = 60;
            this.color = "blue";
            this.isVisible = false;
        }
     
        /**
         * Make this circle visible. If it was already visible, do nothing.
         */
        public void makeVisible()
        {
            isVisible = true;
            draw();
        }
     
        /**
         * Make this circle invisible. If it was already invisible, do nothing.
         */
        public void makeInvisible()
        {
            erase();
            isVisible = false;
        }
     
        /**
         * Move the circle a few pixels to the right.
         */
        public void moveRight()
        {
            moveHorizontal(20);
        }
     
        /**
         * Move the circle a few pixels to the left.
         */
        public void moveLeft()
        {
            moveHorizontal(-20);
        }
     
        /**
         * Move the circle a few pixels up.
         */
        public void moveUp()
        {
            moveVertical(-20);
        }
     
        /**
         * Move the circle a few pixels down.
         */
        public void moveDown()
        {
            moveVertical(20);
        }
     
        /**
         * Move the circle horizontally by 'distance' pixels.
         */
        public void moveHorizontal(int distance)
        {
            erase();
            xPosition += distance;
            draw();
        }
     
        /**
         * Move the circle vertically by 'distance' pixels.
         */
        public void moveVertical(int distance)
        {
            erase();
            yPosition += distance;
            draw();
        }
     
        /**
         * Slowly move the circle horizontally by 'distance' pixels.
         */
        public void slowMoveHorizontal(int distance)
        {
            int delta;
     
            if(distance < 0) 
            {
                delta = -1;
                distance = -distance;
            }
            else 
            {
                delta = 1;
            }
     
            for(int i = 0; i < distance; i++)
            {
                xPosition += delta;
                draw();
            }
        }
     
        /**
         * Slowly move the circle vertically by 'distance' pixels.
         */
        public void slowMoveVertical(int distance)
        {
            int delta;
     
            if(distance < 0) 
            {
                delta = -1;
                distance = -distance;
            }
            else 
            {
                delta = 1;
            }
     
            for(int i = 0; i < distance; i++)
            {
                yPosition += delta;
                draw();
            }
        }
     
        /**
         * Change the size to the new size (in pixels). Size must be >= 0.
         */
        public void changeSize(int newDiameter)
        {
            erase();
            diameter = newDiameter;
            draw();
        }
     
        /**
         * Change the color. Valid colors are "red", "yellow", "blue", "green",
         * "magenta" and "black".
         */
        public void changeColor(String newColor)
        {
            color = newColor;
            draw();
        }
     
        /*
         * Draw the circle with current specifications on screen.
         */
        private void draw()
        {
            if(isVisible) {
                Canvas canvas = Canvas.getCanvas();
                canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, 
                        diameter, diameter));
                canvas.wait(10);
            }
        }
     
        /*
         * Erase the circle on screen.
         */
        private void erase()
        {
            if(isVisible) {
                Canvas canvas = Canvas.getCanvas();
                canvas.erase(this);
            }
        }
    }

    import java.awt.*;
     
    /**
     * A square that can be manipulated and that draws itself on a canvas.
     * 
     * @author  Michael Kölling and David J. Barnes
     * @version 1.0  (15 July 2000)
     */
     
    public class Square
    {
        public int size;
        public int xPosition;
        public int yPosition;
        public String color;
        public boolean isVisible;
     
        /**
         * Create a new square at default position with default color.
         */
        public Square(int x, int y, String color)
        {
         this.size = size;
         this.xPosition = x;
         this.yPosition = y;
         this.color = "red";
         this.isVisible = true;
     
        }
     
         public Square(int size, int x, int y, String color)
           {  
             this.size = size;
             this.xPosition = x;
             this.yPosition = y;
             this.isVisible = false;
             this.color = color;
     
            }
     
     
     
     
        /**
         * Make this square visible. If it was already visible, do nothing.
         */
        public void makeVisible()
        {
            isVisible = true;
            draw();
        }
     
        /**
         * Make this square invisible. If it was already invisible, do nothing.
         */
        public void makeInvisible()
        {
            erase();
            isVisible = false;
        }
     
        /**
         * Move the square a few pixels to the right.
         */
        public void moveRight()
        {
            moveHorizontal(20);
        }
     
        /**
         * Move the square a few pixels to the left.
         */
        public void moveLeft()
        {
            moveHorizontal(-20);
        }
     
        /**
         * Move the square a few pixels up.
         */
        public void moveUp()
        {
            moveVertical(-20);
        }
     
        /**
         * Move the square a few pixels down.
         */
        public void moveDown()
        {
            moveVertical(20);
        }
     
        /**
         * Move the square horizontally by 'distance' pixels.
         */
        public void moveHorizontal(int distance)
        {
            erase();
            xPosition += distance;
            draw();
        }
     
        /**
         * Move the square vertically by 'distance' pixels.
         */
        public void moveVertical(int distance)
        {
            erase();
            yPosition += distance;
            draw();
        }
     
        /**
         * Slowly move the square horizontally by 'distance' pixels.
         */
        public void slowMoveHorizontal(int distance)
        {
            int delta;
     
            if(distance < 0) 
            {
                delta = -1;
                distance = -distance;
            }
            else 
            {
                delta = 1;
            }
     
            for(int i = 0; i < distance; i++)
            {
                xPosition += delta;
                draw();
            }
        }
     
        /**
         * Slowly move the square vertically by 'distance' pixels.
         */
        public void slowMoveVertical(int distance)
        {
            int delta;
     
            if(distance < 0) 
            {
                delta = -1;
                distance = -distance;
            }
            else 
            {
                delta = 1;
            }
     
            for(int i = 0; i < distance; i++)
            {
                yPosition += delta;
                draw();
            }
        }
     
        /**
         * Change the size to the new size (in pixels). Size must be >= 0.
         */
        public void changeSize(int newSize)
        {
            erase();
            size = newSize;
            draw();
        }
     
        /**
         * Change the color. Valid colors are "red", "yellow", "blue", "green",
         * "magenta" and "black".
         */
        public void changeColor(String newColor)
        {
            color = newColor;
            draw();
        }
     
        /*
         * Draw the square with current specifications on screen.
         */
        private void draw()
        {
            if(isVisible) {
                Canvas canvas = Canvas.getCanvas();
                canvas.draw(this, color,
                        new Rectangle(xPosition, yPosition, size, size));
                canvas.wait(10);
            }
        }
     
        /*
         * Erase the square on screen.
         */
        private void erase()
        {
            if(isVisible) {
                Canvas canvas = Canvas.getCanvas();
                canvas.erase(this);
            }
        }
    }

    import java.awt.*;
     
    /**
     * A triangle that can be manipulated and that draws itself on a canvas.
     * 
     * @author  Michael Kölling and David J. Barnes
     * @version 1.0  (15 July 2000)
     */
     
    public class Triangle
    {
        private int height;
        private int width;
        private int xPosition;
        private int yPosition;
        private String color;
        private boolean isVisible;
     
        /**
         * Create a new triangle at default position with default color.
         */
        public Triangle(int height, int width, int x, int y, String color)
        {
            this.height = height;
            this.width = width;
            this.xPosition = x;
            this.yPosition = y;
            this. color = "green";
            this.isVisible = false;
        }
     
        /**
         * Make this triangle visible. If it was already visible, do nothing.
         */
        public void makeVisible()
        {
            isVisible = true;
            draw();
        }
     
        /**
         * Make this triangle invisible. If it was already invisible, do nothing.
         */
        public void makeInvisible()
        {
            erase();
            isVisible = false;
        }
     
        /**
         * Move the triangle a few pixels to the right.
         */
        public void moveRight()
        {
            moveHorizontal(20);
        }
     
        /**
         * Move the triangle a few pixels to the left.
         */
        public void moveLeft()
        {
            moveHorizontal(-20);
        }
     
        /**
         * Move the triangle a few pixels up.
         */
        public void moveUp()
        {
            moveVertical(-20);
        }
     
        /**
         * Move the triangle a few pixels down.
         */
        public void moveDown()
        {
            moveVertical(20);
        }
     
        /**
         * Move the triangle horizontally by 'distance' pixels.
         */
        public void moveHorizontal(int distance)
        {
            erase();
            xPosition += distance;
            draw();
        }
     
        /**
         * Move the triangle vertically by 'distance' pixels.
         */
        public void moveVertical(int distance)
        {
            erase();
            yPosition += distance;
            draw();
        }
     
        /**
         * Slowly move the triangle horizontally by 'distance' pixels.
         */
        public void slowMoveHorizontal(int distance)
        {
            int delta;
     
            if(distance < 0) 
            {
                delta = -1;
                distance = -distance;
            }
            else 
            {
                delta = 1;
            }
     
            for(int i = 0; i < distance; i++)
            {
                xPosition += delta;
                draw();
            }
        }
     
        /**
         * Slowly move the triangle vertically by 'distance' pixels.
         */
        public void slowMoveVertical(int distance)
        {
            int delta;
     
            if(distance < 0) 
            {
                delta = -1;
                distance = -distance;
            }
            else 
            {
                delta = 1;
            }
     
            for(int i = 0; i < distance; i++)
            {
                yPosition += delta;
                draw();
            }
        }
     
        /**
         * Change the size to the new size (in pixels). Size must be >= 0.
         */
        public void changeSize(int newHeight, int newWidth)
        {
            erase();
            height = newHeight;
            width = newWidth;
            draw();
        }
     
        /**
         * Change the color. Valid colors are "red", "yellow", "blue", "green",
         * "magenta" and "black".
         */
        public void changeColor(String newColor)
        {
            color = newColor;
            draw();
        }
     
        /*
         * Draw the triangle with current specifications on screen.
         */
        private void draw()
        {
            if(isVisible) {
                Canvas canvas = Canvas.getCanvas();
                int[] xpoints = { xPosition, xPosition + (width/2), xPosition - (width/2) };
                int[] ypoints = { yPosition, yPosition + height, yPosition + height };
                canvas.draw(this, color, new Polygon(xpoints, ypoints, 3));
                canvas.wait(10);
            }
        }
     
        /*
         * Erase the triangle on screen.
         */
        private void erase()
        {
            if(isVisible) {
                Canvas canvas = Canvas.getCanvas();
                canvas.erase(this);
            }
        }
    }

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Programming Blue J 0_o

    What happens when you compile and execute the program? Does it do what it is supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Junior Member
    Join Date
    Nov 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Programming Blue J 0_o

    Yes, it makes a basic basic one window house. I hate it.

    Next thing is to make another method which allows the user to change the roof color, and to add another window.

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Programming Blue J 0_o

    That's how a lot of programs progress. They start simple and have features added.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    gy1992 (November 26th, 2012)

  7. #31
    Member
    Join Date
    Jan 2011
    Location
    Phoenix
    Posts
    49
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default Re: Java Programming Blue J 0_o

    Hey man. Norm has been doing a great job with helping you.

    With the constructors, you are using what is known as method overloading. You are creating multiple constructors within the same class. In order for the compiler to decide which one to use it looks at the line which calls it. It looks at the parameters for which that line is looking for.

    i.e.
    method();
    method(int);

    the first one will look for method with no arguments and the second one will look for one with 1 int argument, if none is found then an exception is thrown.

    in your case, you are basically using:

    method(int);
    method(int);

    which will always call the same method.

    that is method overloading in a nutshell.

  8. The Following User Says Thank You to that_guy For This Useful Post:

    gy1992 (November 26th, 2012)

  9. #32
    Junior Member
    Join Date
    Nov 2012
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Programming Blue J 0_o

    So yea, everything is working ect. Do Any of you have any words of wisdom for a newbie? I mean norm you seem to be like a demi god with java programming, did you struggle at any point even for a bit when you first started learning?

    /**
     * User can use methods to call on constructors 
     * to build various shapes and change colours.
     * Interger methods are also explored.
     * 
     * 
     * @Gregory Yates
     * 
     * 
     */
    public class MyPicture
    {
     
            private Square square1;
            private Square square2;
            private Square square3;
            private Square square4;
            private Circle circle1;
            private Triangle triangle1;
     
     
        /**
         * Constructor for objects of class MyPicture
         * 
         */
     
     
        /**
         * Creates a default circle(look at circle class for its state)
         * Creates a square and triangle (look at both shapes classes for info on state)
         * 
         */
     
        public MyPicture()
        { 
     
            circle1 = new Circle();
            circle1.changeColor("yellow");
            circle1.makeVisible();
            square1 = new Square(110, 60, 90, "red");
            square2 = new Square(30, 70, 110, "blue");
            square3= new Square( 30, 120, 105, "blue");
            triangle1 = new Triangle(50, 150, 114, 40, "green");
     
     
     
     
     
        }
        /**
         * creates a one Window House, using the square and triangle states to form a picture.
         */
        public void createOneWindowHouse(){
            square1 = new Square(110, 60, 90, "red");
            square2 = new Square(30, 70, 110, "blue");
            triangle1 = new Triangle(50, 150, 114, 40, "green");
            square1.makeVisible();
            square2.makeVisible();
            triangle1.makeVisible();
     
        }
        /**
         * Creates a two window house method, allowing the user to alternate between different
         * settings. Adding a new square shape to act as a window.
         */
     
        public void createTwoWindowHouse(){
            square1 = new Square(110, 60, 90, "red");
            square2= new Square(30, 70, 110, "blue");
            square3= new Square(30, 120, 105, "blue");
            triangle1= new Triangle(50, 150, 114, 40, "green");
            square1.makeVisible();
            square2.makeVisible();
            square3.makeVisible();
            triangle1.makeVisible();
        }
     
     
     
     
       /*
        * Allows the user to change the triangle (roof) color
        * 
        */
       public void changeRoofColour(String color){
     
            triangle1.changeColor(color);
        }
     
        /*
         * Adds a brilliant looking porch to the house, using a new Square shape. 
         */
     
        public void createPorch(){
            square4 = new Square(40, 160, 150, "red");
            square4.makeVisible();
        }
     
        /*
         * A interger choice
         * One Window House picture is assigned to "1"
         * Two Window House picture is assigned to "2"
         * If user enters any number lower then 1 or higher then 2, a error message is called.
         */
        public void chooseAHouse(int choice){
            if (choice == 1){
                    createOneWindowHouse();
            }
     
            if (choice == 2){
                    createTwoWindowHouse();
                }
     
           if ((choice < 1) || (choice > 2)){
                System.out.println("Error! input must be either 1 or 2");
            }
     
        }
     
     
     
     
         /*
          * Users can change triangle color(roof color) selecting a interger
          * red is assigned to 1
          * blue is assigned to 2
          * magenta is assigned to 3
          * black is assigned to 4
          * yellow is assigned to 5
          * green is assigned to 6
          */
     
         public void changeRoofColor (int choice){
            if (choice == 1){
                triangle1.changeColor("red");
            }
     
             if (choice == 2){
               triangle1.changeColor("blue");
            }
     
            if (choice == 3){
                triangle1.changeColor("magenta");
            }
     
             if (choice == 4){
                 triangle1.changeColor("black");
                }
     
             if (choice == 5){
                 triangle1.changeColor("yellow");
                }
     
               if (choice == 6){
                   triangle1.changeColor("green");
     
                }
            }
        }







    import java.awt.*;
     
    /**
     * A square that can be manipulated and that draws itself on a canvas.
     * 
     * @author  Michael Kölling and David J. Barnes
     * @version 1.0  (15 July 2000)
     */
     
    public class Square
    {
        public int size;
        public int xPosition;
        public int yPosition;
        public String color;
        public boolean isVisible;
     
        /**
         * Create a new square at default position with default color.
         */
        public Square()
        {
         this.size = 110;
         this.xPosition = 60;
         this.yPosition = 90;
         this.color = "red";
         this.isVisible = true;
     
        }
     
         public Square(int size, int x, int y, String color)
           {  
             this.size = size;
             this.xPosition = x;
             this.yPosition = y;
             this.isVisible = false;
             this.color = color;
     
            }
     
     
     
     
        /**
         * Make this square visible. If it was already visible, do nothing.
         */
        public void makeVisible()
        {
            isVisible = true;
            draw();
        }
     
        /**
         * Make this square invisible. If it was already invisible, do nothing.
         */
        public void makeInvisible()
        {
            erase();
            isVisible = false;
        }
     
        /**
         * Move the square a few pixels to the right.
         */
        public void moveRight()
        {
            moveHorizontal(20);
        }
     
        /**
         * Move the square a few pixels to the left.
         */
        public void moveLeft()
        {
            moveHorizontal(-20);
        }
     
        /**
         * Move the square a few pixels up.
         */
        public void moveUp()
        {
            moveVertical(-20);
        }
     
        /**
         * Move the square a few pixels down.
         */
        public void moveDown()
        {
            moveVertical(20);
        }
     
        /**
         * Move the square horizontally by 'distance' pixels.
         */
        public void moveHorizontal(int distance)
        {
            erase();
            xPosition += distance;
            draw();
        }
     
        /**
         * Move the square vertically by 'distance' pixels.
         */
        public void moveVertical(int distance)
        {
            erase();
            yPosition += distance;
            draw();
        }
     
        /**
         * Slowly move the square horizontally by 'distance' pixels.
         */
        public void slowMoveHorizontal(int distance)
        {
            int delta;
     
            if(distance < 0) 
            {
                delta = -1;
                distance = -distance;
            }
            else 
            {
                delta = 1;
            }
     
            for(int i = 0; i < distance; i++)
            {
                xPosition += delta;
                draw();
            }
        }
     
        /**
         * Slowly move the square vertically by 'distance' pixels.
         */
        public void slowMoveVertical(int distance)
        {
            int delta;
     
            if(distance < 0) 
            {
                delta = -1;
                distance = -distance;
            }
            else 
            {
                delta = 1;
            }
     
            for(int i = 0; i < distance; i++)
            {
                yPosition += delta;
                draw();
            }
        }
     
        /**
         * Change the size to the new size (in pixels). Size must be >= 0.
         */
        public void changeSize(int newSize)
        {
            erase();
            size = newSize;
            draw();
        }
     
        /**
         * Change the color. Valid colors are "red", "yellow", "blue", "green",
         * "magenta" and "black".
         */
        public void changeColor(String newColor)
        {
            color = newColor;
            draw();
        }
     
        /*
         * Draw the square with current specifications on screen.
         */
        private void draw()
        {
            if(isVisible) {
                Canvas canvas = Canvas.getCanvas();
                canvas.draw(this, color,
                        new Rectangle(xPosition, yPosition, size, size));
                canvas.wait(10);
            }
        }
     
        /*
         * Erase the square on screen.
         */
        private void erase()
        {
            if(isVisible) {
                Canvas canvas = Canvas.getCanvas();
                canvas.erase(this);
            }
        }
    }

Page 2 of 2 FirstFirst 12

Similar Threads

  1. android programming vs game programming using java
    By vgoel38 in forum Android Development
    Replies: 4
    Last Post: September 8th, 2012, 05:48 PM
  2. About Java Programming
    By millions in forum Member Introductions
    Replies: 0
    Last Post: June 30th, 2012, 09:06 AM
  3. Replies: 0
    Last Post: December 12th, 2011, 03:17 PM
  4. blue J libraries (System)
    By columbian in forum Java IDEs
    Replies: 1
    Last Post: May 19th, 2011, 05:40 AM
  5. Is java a power full programming language? can java do this?
    By Jhovarie in forum Java Theory & Questions
    Replies: 5
    Last Post: March 2nd, 2011, 02:02 PM

Tags for this Thread