I am trying to make a GUI that will plot 2 points in a line graph and draw a line segment from those two points.
I cannot find any examples in any of my Java textbooks, and almost every example that I try to search for is too complex to understand in the short amount of time that I have.
Is there anyway to explain graphing easily to a beginner in Java?
Use a Canvas as your drawing surface.
Use the drawLine() function to draw a line between X1,Y1 and X2,Y2.
There are lots of samples out there for this, such as:
http://mainline.brynmawr.edu/Courses/cs110/fall2003/Applets/CanvasExample/CanvasExample.html
or this:
http://www.java2s.com/Code/Java/2D-Graphics-GUI/Drawcanvaswithcolorandtext.htm
Laurence
In my personal experience, the easiest way to draw lines in a GUI is to use a paint(Graphics g) method. Within that, use g.drawLine(x1,y1,x2,y2); to draw the line.
import javax.swing.*;
public class JGraph extends JPanel
{
public void paint(Graphics g)
{
g.drawLine(x1,y1,x2,y2);
}
}
*On a side note, the coordinate system for this method starts at the top left of the GUI object of choice in the prior code.
How do you search??
How to draw lines in Java
Short example
And search the API
Point
Graphics
to draw ultimate lines I have figured out the following code:
public void drawUltimate(Graphics g, int x, int y, int x2, int y2){
int ex=0,ey=0,ex2=0,ey2=0, mx, my;
mx = x2-x;my = y2-y;
//drawLine(jPanel1.getGraphics(), x, y, x2, y2);
for (int i=0; i<100; i++){
ex += mx;
ey += my;
drawLine(jPanel1.getGraphics(), ex2+1, ey2-2, ex, ey);
ex2 =ex; ey2 =ey;
}
}