How to Build a Graph in Java
The Java programming language includes a graph library that makes it convenient for you to set up a graph element in your Java desktop forms. You must define the number you want to input into the graphing classes, then the Java program calculates the graphing structure and layout. The class libraries are included with the Java language, but you must define the graph libraries in the source code headers.
Instructions
-
-
1
Open the Java compiler you want to use to create the graph. Open the source code file in the editor.
-
2
Create an instance of the graph class library to gain access to the Java graphing functions. Add the following code to the file:
Graph graphing = new Graph(3);
In this example, a three-point graph instance is created.
-
-
3
Add the x-y coordinates for each point in the graph. In this example, three points were initialized. The following code sets up the x-y coordinates:
graphing.insertEdge(new Edge(0, 1));
graphing.insertEdge(new Edge(0, 7));
graphing.insertEdge(new Edge(0, 10));The code sets up a linear, diagonal line that moves from left to right.
-
4
Display the graph. Use the "Show" function in the graph class library to display the new graph. Add the following code to display the graph on the form:
graphing.graphDetails.show(graphing);
-
1