Hi guys, I am fairly new to JAVA and currently I am trying to do a couple of pre-set graphs. My first task is to create a graph of y=x^2.

The code below is what i have come up till now. I am sure that i need help in the for loop. I know that i need to do a for loop but im not understanding the process.

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JApplet;

public class graphic extends JApplet{

	int noOfPoints = 100000;
	int xPoints = 0;
	int yPoints = 0;

       public void paint(Graphics gr)
       {

             Graphics2D g = (Graphics2D)gr;
             
             g.drawLine(575, 325, 825, 325); //drawing x-axis line
             g.drawLine(700, 200, 700, 450); //drawing y-axis line
           
             g.drawString("x-axis", 575, 335);//Label for x-axis
             g.drawString("y-axis", 705, 200); //Label for y-axis
   		   	              
             for (int i=-200;i<=500;i++) //Random figures please explian
		{ 		
		         xPoints = (int) (Math.pow(i,2));
			yPoints = (int) (Math.pow(i+1,2));
					
			g.drawLine(i,i,i,i); //Need help here as well
		}

             
           
       }
}

Thanks & Regards

almefab

What do you mean by "Random figures please explian"? If you dont know how a for loop works there are plenty of examples, have a read through some (http://download.oracle.com/javase/tutorial/java/nutsandbolts/for.html)

as for the drawline function, the simplest and best explination is in the java documentation:

drawLine(int x1, int y1, int x2, int y2) -
Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system.


Are you drawing the graph in line-segments, or pixel by pixel? Your increment of i++ (ie increasing by 1) implies you are drawing each pixel in which case you dont want to use drawLine (see the post above)

Hey guys thanks for the help posted above. Problem was solved by doing graphics.drawLine( pointX, pointY, pointX[i+1], pointY[i+1]