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`