Hi,Please i need some help in my java program,it's purpose is to enter 2 equations and it solves them graphically,the problem is that the graph has a glitch with the x and y axis,plz can someone help me so that all the equations are solved correctly..
here's my source code:

import java.awt.*;
import javax.swing.*;
import java.util.Scanner;

public class Main1
{
  public static void main(String args[])
  {  
    Scanner sc = new Scanner(System.in);
    System.out.println("Please Enter 1st Constraint Equation In Terms Of aX + bY = c");
    System.out.println("Enter Value Of 'a'");
    int a = sc.nextInt();
    System.out.println("Enter Value Of 'b'");
    int b = sc.nextInt();
    System.out.println("Enter Value Of 'c'");
    int c = sc.nextInt();
    
    System.out.println("Equation :  " + a + "x + " + b + "y = " + c);
    
    GlobalVariables.result = ( c / b );
    GlobalVariables.result2 = ( c / a );
    
    System.out.println("First Point ( 0 , " + GlobalVariables.result + " )");
    System.out.println("Second Point ( " + GlobalVariables.result2 + " , 0 )");
    
    System.out.println("Please Enter 2nd Constraint Equation In Terms Of dX + eY = f");
    System.out.println("Enter Value Of 'd'");
    int d = sc.nextInt();
    System.out.println("Enter Value Of 'e'");
    int e = sc.nextInt();
    System.out.println("Enter Value Of 'f'");
    int f = sc.nextInt();
    
    System.out.println("Equation :  " + d + "x + " + e + "y = " + f);
    
    GlobalVariables.result3 = ( f / e );
    GlobalVariables.result4 = ( f / d );
    
    System.out.println("First Point ( 0 , " + GlobalVariables.result3 + " )");
    System.out.println("Second Point ( " + GlobalVariables.result4 + " , 0 )");

    JFrame frame = new JFrame("Linear Equation Solver");
    frame.setSize(500,500);
    frame.setLocation(250,100);
    frame.setLayout(null);
    
    Draw i = new Draw();
    frame.add(i);
    frame.setContentPane(i);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    
  }  
}

class GlobalVariables
{
  public static int result;
    
  public static int result2;
  public static int result3;
  public static int result4;   
}

class Draw extends JPanel
{
  public void paintComponent(Graphics g)
  {
    Graphics2D g2 = (Graphics2D)g;
    g2.drawLine(0,GlobalVariables.result*20,GlobalVariables.result2*20,0);
    g2.drawLine(0,GlobalVariables.result3*20,GlobalVariables.result4*20,0);
    g2.drawLine(0,280,500,280);
    g2.drawLine(80,0,80,500);
    g2.rotate(Math.PI / 2 , 20, 200);
    
    
    g2.drawLine(0,GlobalVariables.result*20,GlobalVariables.result2*20,0);
    g2.drawLine(0,GlobalVariables.result3*20,GlobalVariables.result4*20,0);
    
  }
}

Recommended Answers

All 5 Replies

Can you explain what you mean by glitch, not everyone is ready to download&run your program

i know that the origin point (0,0) in java begins from the upper left corner....i want to draw the graph as we draw it on paper...with (0,0) in the middle of the frame...how can i do this ??

Use basic math to translate your values to pixel coordinates. You are just drawing on a rectangle of getHeight() and getWidth() dimensions. (Hint: the middle is just [getWidth()/2, getHeight()/2] and y' = getHeight()-y )

Or translate the graphics context to the origin you want and invert the y scale on your Graphics2D reference.

http://java.sun.com/docs/books/tutorial/2d/index.html

i finished this program successfully,but one last question...can the function g2.drawline() take parameters as float and not int...because some graphs the points are(0,0.5) but it's drawn as(0,0)...how can i enter the parameters in drawline() as float ??

Pixels cannot be floats, you need to convert them to int screen coordinates.

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.