0

My task is to draw a coordinate system using JPanel. I then need to graph a given equation using line segments. Reequirements being: 0.0<=x<=1.0 -- 10 hash marks and 4 hash marks on the y axis -- -0.1, -0.5, 0.5, 1.0

This is my first time programming in Java, and I am having issues using the draw line method correctly. I want to draw the coordinate system in a way that will be helpful to graph. I know once I have the graph draw correctly I will be able to use a loop to graph the equation.

Here is what I have so far.

public class Curve extends JPanel
{

    // initialize variables 
    private int choice; // users choice which function to pick 
    private int lines; // number of lines for curve 

    public Curve(int choice, int lines)
    {
        this.choice = choice; // assign choice in instance variable choice
        //this.x = x; // assigns x instance variable x 
        this.lines = lines; // assigns lines in instance variable lines
    } // end Curve constructor 

    // method to draw x,y axis and ticks
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        int x = getWidth()/2;
        int y = getHeight()/2;
        int tic = getWidth() / 100;
        g.translate(x, y);
        g.setColor (Color.black);
        //g.drawLine (200, 50, 50, 50);


        g.drawLine (0, 0, x, 0);
        g.drawLine (0, -y+50, 0, y-50);

        for (int k = -x/7; k <= x; k += 33)
            g.drawLine (k, tic, k, -tic);

        // draw the tic marks along the y axis
        for (int k = -y+20; k <= y-20; k += 50)
          g.drawLine (-tic , k, tic, k);
    }



    // method to pick function based on user's choice
    public void draw()
    {
        switch ( choice ) // takes in user's choice and plugs into function
        // question 
        {
            case 1: // function 1: y = 1.0 - x: x = int1 
                break;
            case 2: 
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;

        } // end switch statement
    } // end method
} // end class Curve`
mKorbel
  • 109,107
  • 18
  • 130
  • 305
User9193
  • 57
  • 1
  • 11
  • `"...and I am having issues using the draw line method correctly."` -- please describe your issues in detail. Please include anything that will help us to understand your problems. – Hovercraft Full Of Eels Sep 13 '14 at 16:39
  • Also, I would not hard-code or use "magic numbers" within my `g.drawLine(...)` method calls. Instead, I'd probably calculate the numbers, perhaps based on the size of the JPanel's drawing area. – Hovercraft Full Of Eels Sep 13 '14 at 17:04
  • 1
    @Hovercraft Full Of Eels hello:-) – mKorbel Sep 13 '14 at 19:40
  • You might look a this [example](http://stackoverflow.com/a/9373195/230513) or this [example](http://stackoverflow.com/a/20107935/230513) using `JFreeChart`. – trashgod Sep 14 '14 at 02:41

0 Answers0