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 1 of 2 12 LastLast
Results 1 to 25 of 32

Thread: Java Programming Blue J 0_o

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

    Default Java Programming Blue J 0_o

    So i'm trying "trying" to code something which creates a House. Below is the method i'm trying to use, however i keep getting this error message "that the operator i've used here cannot be used for this type of value (the square 1 line gets highlighted). Why does this happen? Furthermore would i have to make a different constructor for each of the shape lines? if so can i differentiate between square 1 and 2 so the user can automatically (Without entering anything) can build the onewindowhouse?

    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();
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,155
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Java Programming Blue J 0_o

    i keep getting this error message
    Please copy and post the full text of the error message.

    a different constructor for each of the shape lines
    You would need to create a new instance of each of the lines by using the new statement with the class's constructor.

    differentiate between square 1 and 2
    The variable names: square1 and square2 would allow you to differentiate between the two objects.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Programming Blue J 0_o

    Constructor Square in Square class cannot be applied to given types;
    required:no arguments;
    found:int,int,int,java.lang.string;
    reason:actual and formal arguments differ in length


    So for the second point, the below code would suffice for line one. I can't seem to replicate the second line in the same square class though.

     
     public Square()
        {
            size = 110;
            xPosition = 60;
            yPosition = 90;
            color = "red";
            isVisible = false;
     
        }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,155
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Java Programming Blue J 0_o

    required:no arguments;
    Do you understand what that says? If you want to pass any arguments to the constructor you need to define a constructor to take what you want to pass.

    replicate the second line in the same square class
    Can you explain where the first line is? How is a line defined in an instance of the Square class?
    How would more than one line be defined in one instance of the class?

    My understanding about a square is that it has 4 equal sides and a location. I have no idea where a line would be.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Programming Blue J 0_o

    Sorry for using 'line' i was meaning the second line in the onewindowhouse method.

    basically how would i write the below method into the square class? so when the user starts the programme the 2 squares are placed with the specified attributes. (so the user doesn't have to input any values, if this can be achieved?)

    i've only been at this for a few weeks, so forgive me for appearing to be slightly idiotic.
    square1 = new Square(110, 60, 90, "red");
    square2 = new Square(30, 70, 110, "blue");

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,155
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Java Programming Blue J 0_o

    using 'line' i was meaning the second line in the onewindowhouse method.
    Sorry, I don't know what you mean by a line when you are talking about a square.
    What is the "first line" or a second line in a square?

    how would i write the below method into the square class
    What you have posted looks like calls to the class's constructor, not to a method in the class.
    See the tutorial about class constructors:
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    the user doesn't have to input any values
    Your posted code provides values to the Square class's constructor that the user did not enter:
    square1 = new Square(110, 60, 90, "red");
    square2 = new Square(30, 70, 110, "blue");
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Programming Blue J 0_o

    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();
        }
        /**
         * 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();
        }
     
     
    }

    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()
        {
            size = 110;
            xPosition = 60;
            yPosition = 90;
            color = "red";
            isVisible = false;
     
     
         }
     
     
        /**
         * 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);
            }
        }
    }


    The above is all the code that i'm using and getting the error already stated with. I don't know i'm lost, it's late, "sadface.jpg". I'll come back to this in the morning, but thanks for help man.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,155
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Java Programming Blue J 0_o

    Read the tutorial on how to write and use constructors.
    If you don't understand my answer, don't ignore it, ask a question.

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

    gy1992 (November 24th, 2012)

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

    Default Re: Java Programming Blue J 0_o

    I will do thanks

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,155
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Java Programming Blue J 0_o

    Back tomorrow,
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Programming Blue J 0_o

    Hey, so new day new problem. I'm pretty sure i've gotten the constructors correct and i'm no longer getting the previous the error. However my code compiles without any error, but when i go and click on the void createOneWindowHouse (on the mypicture object) i get the error message below popping up.

    Java.Lang.nullPointerException:
    null;

    /**
     * Write a description of class MyPicture here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class MyPicture
    {
     
            public 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();
        }
        /**
         * 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 
         * error message popping up on this code, jumps to this each time i try to run the createOneWindoeHouse method.
         */
        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.*;
     
    /**
     * 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)
        {
            height = 50;
            width = 150;
            xPosition = 114;
            yPosition = 40;
            color = "green";
            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);
            }
        }
    }

    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()
        {
         size = 110;
         xPosition = 60;
         yPosition = 90;
         isVisible = false;
         color = "red";
         }
     
         public Square(int size, int x, int y, String color)
         {
             size= 30;
             xPosition = 70;
             yPosition = 110;
             isVisible = false;
             color = "blue";
     
            }
     
     
     
        /**
         * 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.*;
    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()
        {
            diameter = 30;
            xPosition = 20;
            yPosition = 60;
            color = "blue";
            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);
            }
        }
    }


    All the code i've used is above.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,155
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Java Programming Blue J 0_o

    i get the error message below popping up.
    You need to be sure the full text of the error message is displayed by calling the printStackTrace() method in the catch block. The error message will show the line number where the error happened.
    Java.Lang.nullPointerException:
    You should copy and paste the full text of the error message. What you posted is NOT the correct text from the message. The spelling is wrong. The correct spelling:
    java.lang.NullPointerException
    You should copy and paste, not make it up.

    A NullPointerException happens when a statement with a variable with a null value is executed. Look at the error message, find the line number and then look at that statement and find what variable has a null value. Then backtrack in the code to see why that variable does not have a valid non-null value.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Programming Blue J 0_o

    I wouldn't know how to do use the printStackTrace() in bluej.

    if(colorString.equals("red"))

    It highlights this bit of code each time the error happens.

    --- Update ---

    java.lang.NullPointerException
    	at Canvas.setForegroundColor(Canvas.java:124)
    	at Canvas$ShapeDescription.draw(Canvas.java:216)
    	at Canvas.redraw(Canvas.java:167)
    	at Canvas.draw(Canvas.java:104)
    	at Square.draw(Square.java:185)
    	at Square.makeVisible(Square.java:46)
    	at MyPicture.createOneWindowHouse(MyPicture.java:44)
    java.lang.NullPointerException
    	at Canvas.setForegroundColor(Canvas.java:124)
    	at Canvas$ShapeDescription.draw(Canvas.java:216)
    	at Canvas.redraw(Canvas.java:167)
    	at Canvas.draw(Canvas.java:104)
    	at Square.draw(Square.java:185)
    	at Square.makeVisible(Square.java:46)
    	at MyPicture.createOneWindowHouse(MyPicture.java:44)
    java.lang.NullPointerException
    	at Canvas.setForegroundColor(Canvas.java:124)
    	at Canvas$ShapeDescription.draw(Canvas.java:216)
    	at Canvas.redraw(Canvas.java:167)
    	at Canvas.draw(Canvas.java:104)
    	at Square.draw(Square.java:185)
    	at Square.makeVisible(Square.java:46)
    java.lang.NullPointerException
    	at Canvas.setForegroundColor(Canvas.java:124)
    	at Canvas$ShapeDescription.draw(Canvas.java:216)
    	at Canvas.redraw(Canvas.java:167)
    	at Canvas.draw(Canvas.java:104)
    	at Square.draw(Square.java:187)
    	at Square.makeVisible(Square.java:48)
    	at MyPicture.createOneWindowHouse(MyPicture.java:44)
    java.lang.NullPointerException
    	at Canvas.setForegroundColor(Canvas.java:124)
    	at Canvas$ShapeDescription.draw(Canvas.java:216)
    	at Canvas.redraw(Canvas.java:167)
    	at Canvas.draw(Canvas.java:104)
    	at Square.draw(Square.java:187)
    	at Square.makeVisible(Square.java:48)
    	at MyPicture.createOneWindowHouse(MyPicture.java:44)
    java.lang.NullPointerException
    	at Canvas.setForegroundColor(Canvas.java:124)
    	at Canvas$ShapeDescription.draw(Canvas.java:216)
    	at Canvas.redraw(Canvas.java:167)
    	at Canvas.draw(Canvas.java:104)
    	at Square.draw(Square.java:187)
    	at Square.makeVisible(Square.java:48)
    	at MyPicture.createOneWindowHouse(MyPicture.java:44)
    java.lang.NullPointerException
    	at Canvas.setForegroundColor(Canvas.java:124)
    	at Canvas$ShapeDescription.draw(Canvas.java:216)
    	at Canvas.redraw(Canvas.java:167)
    	at Canvas.draw(Canvas.java:104)
    	at Square.draw(Square.java:187)
    	at Square.makeVisible(Square.java:48)
    	at MyPicture.createOneWindowHouse(MyPicture.java:44)
    java.lang.NullPointerException
    	at Canvas.setForegroundColor(Canvas.java:124)
    	at Canvas$ShapeDescription.draw(Canvas.java:216)
    	at Canvas.redraw(Canvas.java:167)
    	at Canvas.draw(Canvas.java:104)
    	at Square.draw(Square.java:187)
    	at Square.makeVisible(Square.java:48)
    	at MyPicture.createOneWindowHouse(MyPicture.java:44)
    java.lang.NullPointerException
    	at Canvas.setForegroundColor(Canvas.java:124)
    	at Canvas$ShapeDescription.draw(Canvas.java:216)
    	at Canvas.redraw(Canvas.java:167)
    	at Canvas.draw(Canvas.java:104)
    	at Square.draw(Square.java:187)
    	at Square.makeVisible(Square.java:48)
    	at MyPicture.createOneWindowHouse(MyPicture.java:44)
    java.lang.NullPointerException
    	at Canvas.setForegroundColor(Canvas.java:124)
    	at Canvas$ShapeDescription.draw(Canvas.java:216)
    	at Canvas.redraw(Canvas.java:167)
    	at Canvas.draw(Canvas.java:104)
    	at Square.draw(Square.java:187)
    	at Square.makeVisible(Square.java:48)
    	at MyPicture.createOneWindowHouse(MyPicture.java:44)
    java.lang.NullPointerException
    	at MyPicture.<init>(MyPicture.java:36)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    	at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    	at bluej.runtime.ExecServer$3.run(ExecServer.java:741)
    java.lang.NullPointerException
    	at MyPicture.<init>(MyPicture.java:36)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    	at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    	at bluej.runtime.ExecServer$3.run(ExecServer.java:741)
    java.lang.NullPointerException
    	at Canvas.setForegroundColor(Canvas.java:124)
    	at Canvas$ShapeDescription.draw(Canvas.java:216)
    	at Canvas.redraw(Canvas.java:167)
    	at Canvas.draw(Canvas.java:104)
    	at Square.draw(Square.java:187)
    	at Square.makeVisible(Square.java:48)
    	at MyPicture.createOneWindowHouse(MyPicture.java:46)
    java.lang.NullPointerException
    	at Canvas.setForegroundColor(Canvas.java:124)
    	at Canvas$ShapeDescription.draw(Canvas.java:216)
    	at Canvas.redraw(Canvas.java:167)
    	at Canvas.draw(Canvas.java:104)
    	at Square.draw(Square.java:187)
    	at Square.makeVisible(Square.java:48)
    	at MyPicture.createOneWindowHouse(MyPicture.java:46)

    this is the error in the console that appears.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,155
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Java Programming Blue J 0_o

    java.lang.NullPointerException
    at Canvas.setForegroundColor(Canvas.java:124)
    Look at line 124 in the Canvas class, find the variable with the null value and then backtrack in the code to find out why that variable does not have a non-null value.

    Where is the Canvas class defined? Java SE has a class by that name. Your class should NOT use the same name as the Java SE class because that is confusing for anyone reading the code.

    how to do use the printStackTrace() in bluej.
    That method belongs to the object passed to the catch block. You need to call it in the catch block. There should be thousands of code samples if you do a search.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Programming Blue J 0_o

    Is there a way to quickly select line 124?

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,155
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Java Programming Blue J 0_o

    Your IDE should help you know which line of a program you are looking at.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Programming Blue J 0_o

    Sorry i thought i had copied it already, this is the code that it highlights for me

    if(colorString.equals("red"))
    graphic.setColor(Color.red);


    --- Update ---

    I used a
    if (graphic == null) throw new NullPointerException();


    It now instead of creating anything highlights that in the canvas class.

    --- Update ---

    So some progress finally.

    I'm getting the squares and triangle to appear, however the sizes and color are still incorrect 0_o. It seems that using this.size ect seems to help.

     
    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 = false;
     
         }
     
         public Square(int size, int x, int y, String color)
         {
             this.size= 30;
             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.*;
    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 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 = "color";
            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);
            }
        }
    }

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,155
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Java Programming Blue J 0_o

    Did you find what variable had the null value and then change the code so it would not have a null value?

    the sizes and color are still incorrect
    What size and color is the code using? Where does it get those values from? What values do you expect for those two variables?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Programming Blue J 0_o

    yea it was the color value, so i changed it to "red", "green" ect. That problem seems to have been solved anyway. Now my problem seems to be actually getting the program to not pick just the second constructor of the square code below.






        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 = false;
     
         }
     
         public Square(int size, int x, int y, String color)
         {
             this.size= 30;
             this. xPosition = x;
             this. yPosition = y;
             this.isVisible = false;
             this.color = "blue";
     
            }


    --- Update ---

    it's just taking the second square constructor, the public Square (int size, int x, int y, string color)

     
    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()
        {
         size = 110;
         xPosition = 60;
         yPosition = 90;
         color = "red";
         isVisible = false;
     
         }
     
         public Square(int size, int x, int y, String color)
         {
             this.size= 30;
             this. xPosition = 70;
             this. yPosition = 110;
             this.isVisible = false;
             this.color = "blue";
     
            }

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,155
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Java Programming Blue J 0_o

    getting the program to not pick just the second constructor of the square code below.
    The compiler will pick the constructor that matches the arguments used in the new statement.

    If you want a different constructor to be called, change the arguments in the new statement and add a constructor to the class that has those args.

    There is a conceptual problem with the way the second constructor is coded. It is ignoring ALL the arguments that are passed to it and uses hardcoded literals. It should be saving the args that are passed to it in the class variables so that each new object has the values passed to it by the caller.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    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 i have to change the second constructor in the Square class?

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,155
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Java Programming Blue J 0_o

    Yes, that constructor should use the args passed to it to define the values in the object being created.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Programming Blue J 0_o

    Do i need to add anything else to the MyPicture class? or should i keep each constructor separated in their own shape class?

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,155
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Java Programming Blue J 0_o

    Do i need to add anything else to the MyPicture class?
    That depends on what the class is supposed to do.

    should i keep each constructor separated in their own shape class?
    I'm not sure what you are asking. Each class needs some constructors so that it can be used as needed. The number of constructors depends on what the class is used for.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Java Programming Blue J 0_o

    The class is supposed to allow for methods to be run of it so a picture can be drawn. I'm still getting the same problem, i can't seem to figure out how to separate 2 constructors in the same Square class, so that the mypicture class can have on method created to allow for a house to be drawn.

    --- Update ---

    This is the instruction i have to carry out. It's annoying because once i have this down i can get the rest of the excercise done in time for tommorow because it's all just the same adding new methods.

    "Our new class doesn’t draw very much. We can change that by adding the following method:
    /**
         * Builds a house with 1 window on the canvas. 
         * In order to do this it has to instantiate (gives initial values)
         * to some of its attributes.
         * This is done by using constructors from the appropriate classes.
         * These shape objects are then made visible by using their classes'
         * makeVisible() methods.
         */    
        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();
        }
    This method will only compile and work if you have appropriate constructors in the Square and Triangle classes. If you don’t, add them to the relevant classes. Notes from lab 2 should help. The values given as parameters should give you a house. The order for the parameters is as follows:
      Square: size, xPosition, yPosition, color
      Triangle: height, width, xPosition, yPosition, color

    If not already there, add the appropriate constructors to the Square & Triangle classes.
    Add the above class, with comments, to the new class.
    Compile
    Test

    --- Update ---

    To add pressure to pressure already, if i don't nail this i'll fail my course 0_o literally fail it.

Page 1 of 2 12 LastLast

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