This is a lab I am working on. I think that I am on the last step. My problem/question is highlighted in bold.

1. Create a constructor that allows you to specify the 2 endpoints of the line being created. Leave the original constructor alone. Your program should have 2 constructors now.


2. Create an accessor method that returns the color of the line.


3. Create a mutator method that lets you change the (x, y) coordinates of the endpoints to newly specified values. Don't just change values by some constant, allow new values to be entered for each coordinate. Don't forget to redraw the line after you change it.


4. Create an accessor method that returns the length of the line as an integer (not a double).


5. Create a method that prints out the midpoint of a line (print it out, this method returns nothing: void). Use the following format (i.e. if I use your program to create a line from (10, 20) to (100, 120) this method should print exactly as follows):

The midpoint of the line from (10, 20) to (100, 120) is (55, 70).

Hint: The midpoint remains unchanged if you swap the endpoints.


6. Create a method that finds the slope of a line. The method will print out the slope if it is defined, otherwise it will print out the x-value of the line. Print the slope as a fraction int/int (i.e. do not do the division. Note: Since the origin is in the upper left corner with x increasing to the right and y increasing down the slope will be different from what you would expect. What is different? Fix your code so that lines rising from left to right have a positive slope and lines falling from left to right have a negative slope.) If the slope is not defined the method should return false. If it is defined the method returns true.
Here are several examples using different lines:

For a line with endpoints (0,0) and (100, 200), acceptable outputs are:
"Slope is : 200/-100"
or
"Slope is : -200/100"

For a line with endpoints (100,0) and (0,200), acceptable outputs are:
"Slope is : 200/100"
or
"Slope is : -200/-100"

For a line with endpoints (0,100) and (200,100), the acceptable output is:
"Slope is : 0"

For a line with endpoints (100,0) and (100,200), the acceptable output is:
"Slope is undefined: x = 100"

My problem is that is seems that for my line there is no "anchor". If I start with X1,Y1 in the top left and X2,Y2 in the bottom right. My problem is that when I enter new X,Y coordinates if the X2,Y2 numbers are smaller than X1,Y2 it just seems to flip places with X1,Y1 essentially keeping the line going from top left to bottom right.

I also have a sneaking suspicion that I didn't follow instruction three correctly.

This is all ran using the program BlueJ for a beginner programming class.

The area that I had to write into is blocked off by //------------------------

I am not supposed to change anything outside of the //--------------------'s

Here is my code

import java.awt.*;
import java.awt.geom.*;

/**
 * A line that can be manipulated and that draws itself on a canvas
 * 
 * @author Derek Green (a slightly modified copy of the Circle class written by Michael Kolling and David J. Barnes)
 * @version 1.0 (2/7/04)
 */
public class Line
{
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    private int midX;
    private int midY;
    private int slopeX;
    private int slopeY;
    private int length;
    private String color;
    private boolean isVisible;
    
    /**
     * Create a new line using default values.
     */
    public Line()
    {
        x1 = 0;
        y1 = 0;
        x2 = 10;
        y2 = 10;
        color = "blue";
        isVisible = false;
    }
    
//-------------------------------------------------------------------------------- 
//Cameron Hopkin 
//Lab 3 
//-------------------------------------------------------------------------------- 

    /** This is a constructor that allows a user to put in 2 endpoints
     * 
     */    
     public void lineTwo(int xOne, int yOne, int xTwo, int yTwo)
     {
         x1 = xOne;
         y1 = yOne;
         x2 = xTwo;
         y2 = yTwo;
     }
     
     /** Here we return the color of the line
      * 
      */
     public String getColor()
     {
         return color;
     }
     
     /** Here a person is able to overwrite the old end points.  This will also erase and draw the line.
      * 
      */
     public void newLine(int xNew1, int yNew1, int xNew2, int yNew2)
     {
         erase();
         x1+=x1+xNew1;
         y1+=y1+yNew1;
         x2+=x2+xNew2;
         y2+=y2+yNew2;
         draw();
     }
     
     /** This will return the length of the line it gets the length by taking the Highest value and subtracting the lowest value.
      * for instance if x1 is 0 and x2 is 10 then it would do 10-0 = 10.
      */
     public int getLength()
     {
         if (x1 < x2)
         {
             length = x2 - x1;
         }
         else
         {
             length = x1 -x2;
         }
         return length;
         
     }
     
     /** Basic formula here to find the midpoint of a line.  Add both Xs then divide by two for the X point and add both Ys then divide by
      * 2 to find the y point will look exactly as the formulas for midX and midY.  
      * Finally the information is printed to screen in the format request.  The midpoint of the line from (x,y) to (x,y) is (x,y).
      */
     public void midPoint()
     {
         midX = (x1+x2)/2;
         midY = (y1+y2)/2;
         
         System.out.println("The midpoint of the line from (" + x1 + "," + y1 +") to (" + x2 + "," + y2 + ") is (" + midX + "),(" + midY + ").");
     }
     
     /** Another basic math formula.  Slope is found by x1-x2/y1-y2.  In this case we dont want the computer to do an actual division so I seperated
      * the x and y coordinates into 2 seperate formula.  When going to print out the information I just print a / to the screen in the proper format which is
      * Y/X.  This follows the basic rule of finding M when you have 2 sets of coordinates M = x1-x2/y1-y2.  The conditional statements are checked against first.
      * These statements are checking to make sure x1-x2 and y1-y2 don't equal 0.  The reason for this is if X = 0 then the slope is going to only be whatever is left
      * in Y.  If Y = 0 the answer is undefined.  This information is then printed out in the format Slope is : Y / X  
      */
     public void slope()
     
     {
        slopeX = x1 - x2;
        slopeY = y1 - y2;
        
        if (slopeX != 0 && slopeY != 0)
        {
            System.out.println("Slope is : " + slopeY + "/" + slopeX + ".");
        }
        else if (slopeY == 0)
        {
            System.out.println("Slope is : " + slopeY + ".");    
        }
           
        else if (slopeX == 0)
        {
           System.out.println("Slope is undefined: x= " + x1 + "."); 
        }
     }







//-------------------------------------------------------------------------------- 


    
        
    /**
     * Make this line visible, if already visible do nothing.
     */
    public void makeVisible()
    {
        isVisible = true;
        draw();
    }

    /**
     * Make this line invisible, if already visible do nothing.
     */
    public void makeInvisible()
    {
        erase();
        isVisible = false;
    }
    
    /**
     * Move the line 20 pixels to the right without deformation.
     */
    public void moveRight()
    {
        moveHorizontal(20);
    }

    /**
     * Move the line 20 pixels to the left without deformation.
     */
    public void moveLeft()
    {
        moveHorizontal(-20);
    }

    /**
     * Move the line 20 pixels up without deformation
     */
    public void moveUp()
    {
        moveVertical(-20);
    }

    /**
     * Move the line 20 pixels down without deformation.
     */
    public void moveDown()
    {
        moveVertical(20);
    }

    /**
     * Move the line horizontally by 'distance' pixels without deformation.
     */
    public void moveHorizontal(int distance)
    {
        erase();
        x1 += distance;
        x2 += distance;
        draw();
    }

    /**
     * Move the line vertically by 'distance' pixels without deformation.
     */
    public void moveVertical(int distance)
    {
        erase();
        y1 += distance;
        y2 += distance;
        draw();
    }

    /**
     * Slowly move the line horizontally by 'distance' pixels without deformation.
     */
    public void slowMoveHorizontal(int distance)
    {
        int delta;

        if(distance < 0) 
        {
            delta = -1;
            distance = -distance;
        }
        else 
        {
            delta = 1;
        }

        for(int i = 0; i < distance; i++)
        {
            x1 += delta;
            x2 += delta;
            draw();
        }
    }

    /**
     * Slowly move the line vertically by 'distance' pixels without deformation.
     */
    public void slowMoveVertical(int distance)
    {
        int delta;

        if(distance < 0) 
        {
            delta = -1;
            distance = -distance;
        }
        else 
        {
            delta = 1;
        }

        for(int i = 0; i < distance; i++)
        {
            y1 += delta;
            y2 += delta;
            draw();
        }
    }
 
     /**
     * Change the color. Valid colors are "red", "yellow", "blue", "green",
     * "magenta" and "black".
     */
    public void changeColor(String newColor)
    {
        color = newColor;
        draw();
    }

    /**
     * Draw the line.
     */
    private void draw()
    {
        if(isVisible) {
            Canvas canvas = Canvas.getCanvas();
            canvas.draw(this, color, new Line2D.Double(x1, y1, x2, y2));
            canvas.wait(10);
        }
    }

    /*
     * Erase the line on screen.
     */
    private void erase()
    {
        if(isVisible) {
            Canvas canvas = Canvas.getCanvas();
            canvas.erase(this);
        }
    }
}

What I am looking for is a general direction and not necessarily the actual code snippet I may need.

Basically how do I keep X1, Y1 and the top spot on a line and X2, Y2 as bottom.

Like \ /.
As it is now my program just draws \ no matter the input of X1, Y1 and X2, Y2.

I can change the color, length, starting position. Move, slow move, etc but I cant get X1,Y1 to stay up top.

Hope this helps explain my predicament better.

You're right - you didn't follow number 3 correctly. Your code is adding the new coordinates to the old when all it needs to do is set them

x1 = xNew1;
         y1 = yNew1;
         x2 = xNew2;
         y2 = yNew2;

You are right. For some reason though it wasn't actually adding xNew1 to the old value in x1 so I left it alone.

As I said my biggest problem is my second post there.

I will correct that += code for step three just for clarity sake though. Thanks!

Well, the draw method will draw between the two points you enter, so perhaps part of the issue is actually with the coords you are supplying, but perhaps you are looking for something like

int left = Math.min(x1, x2);
int top = Math.min(y1, y2);
int right = Math.max(x1, x2);
int bottom = Math.max(y1, y2);
canvas.draw(this, color, new Line2D.Double(left, top, right, bottom));

??

The actual problem was with the coordinates I was given AND the picture examples provided.

The pictures were \ / when they should have just been \\ for the values given.

Thanks for your time and help though.

Also my length of the line equation should have been

public int getLength()
     {
         
         int lengthA = (((x2 - x1)*(x2-x1))+((y2 - y1)*(y2-y1)));
         length = (int)(Math.sqrt(lengthA));
         
         return length;
     }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.