0

I have a school assignment for CompSci where we need to create a graph for linear equations. This graph should have the axes drawn, and it should have two lines that are drawn from two equations. I have the graph itself working correctly, but I can't seem to figure out how to set the coordinates of the lines right. Currently I am using the following code to create and send the coordinates to my graph:

    m.print("Slope as decimal: ");
    //gets slope of line. This will be changed so it will accept fractions like Rise/Run but not until the rest of the functionality works.
    double lslope = m.readDouble();
    //gets y intercept as B
    m.print("B as decimal: ");
    double lb = m.readDouble();

    //corresponds to the first coordinate
    double x11=0;
    double y11 = lslope*x11-lb*50;

    //corresponds to the second coordinate
    double x21=1000;
    double y21 = lslope*x21-lb*50;

    //outputs the coordinates of the two points just for the user's conveinience
    m.println(""+x11+"\n"+y11+"\n"+x21+"\n"+y21);

    //wingraph is a JFrame
    wingraph graph = new wingraph("Graph");
    graph.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    graph.setSize(1000,1000);
    graph.setResizable(false);

    //the second set of 4 numbers will be a second line, but to get the graph working I'm just focusing on using one set. The setCoords method just sets the value of 8 coordinates inside a JFrame class which paints the X and Y axis in black, and then paints two lines with the specified coordinates in red.
    graph.setCoords(x11, y11, x21, y21, 1000,1000,1000,1000);

    //displays the graph
    graph.setVisible(true);

By inverting the slope the user entered, I can get the line to work correctly, but only if lslope is equal to 1. How could I manipulate the integers correctly so that my graph is displayed well whether the slope has decimals, is any real number, or is positive or negative?

If it helps, the code for the JFrame I'm using is:

    public class wingraph extends JFrame{

        public wingraph(String title){
            super(title);
            setLayout(new FlowLayout());
        }
        double xCoord1 = 0, yCoord1 = 0, xCoord2 = 0, yCoord2 = 0, xCoord12 = 0, yCoord12 = 0, xCoord22 = 0, yCoord22 = 0;
        public void setCoords(double x1, double y1, double x2, double y2, double x12, double y12, double x22, double y22){
            xCoord1 = x1;
            yCoord1 = y1;
            xCoord2 = x2;
            yCoord2 = y2;
            xCoord12 = x12;
            yCoord12 = y12;
            xCoord22 = x22;
            yCoord22 = y22;
        }        
        public void paint(Graphics g){
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            g.setColor(Color.BLACK);
            g.drawLine(this.getWidth()/2, 0, this.getWidth()/2, this.getHeight());
            g.drawLine(0, this.getHeight()/2, this.getWidth(), this.getHeight()/2);
            g.drawString("0",this.getWidth()/2-10,this.getHeight()/2-5);
            g.setColor(Color.red);
            g.drawLine((int) xCoord1,(int) yCoord1,(int) xCoord2,(int) yCoord2);
            g.drawLine((int) xCoord12,(int) yCoord12,(int) xCoord22,(int) yCoord22);
        }
}

Any help is appreciated.

Edit

Examples of my code running is this:

https://ibb.co/mVGFff

https://ibb.co/iMwbmL

https://ibb.co/mD080f

https://ibb.co/ezNct0

These all have issues since I need them all to be centered on the axis and the correct format such as:

https://ibb.co/cWstY0

https://ibb.co/goi0D0

I apologize if this is still unclear, but I am new to Java and am not all that familiar with how to explain exactly what I want other than to graph 2 lines with the equation y = mx + b on a Cartesian plane.

Edit 2

If the user inputs 1 for the lslopeand 0 for b, I want the x and y variables to be:

    x11 = 0;
    y11 = 1000;
    x21 = 1000;
    y21 = 0;

If lslope = 2 and b = 5 I want the x and y variables to be:

    x11 = 0
    y11 = 745;
    x21 = 1000;
    y21 = 245;

If lslope = -1 and b = 10 I want the x and y variables to be:

    x11 = 0
    y11 = 240;
    x21 = 1000;
    y21 = 740;
  • 1
    Your actual question is unclear to me, and appears to be quite broad. Can you be more specific? Some side recommendations: 1) Never draw directly in the JFrame's paint method but rather better in a JPanel's paintcomponent method. 2) Call the super's corresponding painting method in your override. 3) Your code is using lots of "magic" numbers, and this will make debugging and enhancement difficult. 3) It would probably be better to separate the view from the logic parts of your code into separate classes, and to bridge the two with a "translation" of some sort, something that... – Hovercraft Full Of Eels Oct 17 '18 at 02:19
  • 1
    ... translates logic points to image points and visa versa. Check out [Lesson: Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/index.html): introductory tutorial to Swing graphics – Hovercraft Full Of Eels Oct 17 '18 at 02:19
  • You may also want to investigate "model-view-controller". This way you could separate the model/data from the view/ui, so the view simply becomes responsible for deciding "how" the model should be rendered – MadProgrammer Oct 17 '18 at 02:24
  • 1
    I'd also consider having a look at [`Line2D`](https://docs.oracle.com/javase/10/docs/api/java/awt/geom/Line2D.html), which supports `double` values – MadProgrammer Oct 17 '18 at 02:36
  • @HovercraftFullOfEels I've made some edits to my question in the hopes that it might be a bit more clear. Sorry if it doesn't help. –  Oct 17 '18 at 02:43
  • 1
    Welcome to SO. To make your code [mcve] and clearer I would suggest to remove `m` (which is not defined) and hard code data (like `lslope`, `lb`, `x11` ) with values that do not produce the desired output. Please state what is the desired output. – c0der Oct 17 '18 at 05:40
  • Side notes: since `x11` is 0 `double y11 = lslope*x11-lb*50;` is essentially `double y11 = -lb*50;` . Also see [Java Naming Conventions](https://www.geeksforgeeks.org/java-naming-conventions/). – c0der Oct 17 '18 at 05:42

0 Answers0