How to Graph in Java

The Java language includes a "Graph()" class that imports a list of data points to set up in the initialization code. You can set up as many points as you need to render the graph, and the Java libraries handle the calculations and rendering on your Java form. The graphing class is useful when you want to create a visual representation of data input by your users.

Instructions

    • 1

      Open your preferred Java interpreter software and open the desktop project that contains the data you want to graph. Open the source code file to load the Java code editor.

    • 2

      Initialize the graphing component class. You must specify how many points you want to use for the graph. In this example, the code specifies five graph points for the class:

      Graph thegraph = new Graph(5, false);

    • 3

      Add the data points using numeric values. After you initialize the graph, you must specify each point in the order in which you want the points to display. The following code sets up the five data points:

      thegraph.insertEdge(new Edge(0, 1));
      thegraph.insertEdge(new Edge(0, 3));
      thegraph.insertEdge(new Edge(0, 4));
      thegraph.insertEdge(new Edge(0, 9));
      thegraph.insertEdge(new Edge(0, 11));

      The code above creates a line that starts at the "0,1" point and draws a diagonal line to the "0,11" point.

    • 4

      Display the graph. After you set up the points, the "show" function displays the graph in your Java app. Use the following code to display the graph:

      thegraph.graphDetails.show(thegraph);

Related Searches:

References

Comments

Related Ads

Featured