Pls assist me in graph programming in java, how to draw graphs, scale graph and other stuffs of graph

Recommended Answers

All 2 Replies

Here is all help you need

Here is the Java tutorials: http://java.sun.com/docs/books/tutorial/ (And in Java's case the official is the best one of all!)
And here is the 2D tutorial: http://java.sun.com/docs/books/tutorial/2d/index.html

To draw graphs, you can do 2 things:
- Search a 3rd party (or maybe included official) component that can do it for you. This is not my case, so others will be able to help more.
- Do it yourself:
In this case, my setup looks like this:

myImageIcon = new ImageIcon();
JLabel label = new JLabel(myImageIcon);
jScrollPane1.getViewport().add(label);
label.addMouseListener(new myListener());
myImageIcon.setImage(myImage);
myGraph = new meowGraph(myImage, [...]);
[...]
myGraph.RenderData([...])

Here I use a jScrollPane which will help in case the graph would be bigger than the drawing area. In case it's not, the scroll bars won't appear. Then I add the listener onto the JLabel, which is inside the scrolling area, so I get the coordinates relative to my image even when it's scrolled. Then I create my component called meowGraph (for demonstration only :P) that takes the instance of a drawing surface it can use. Then setup is completed, I can push data into it whenever I want, and it'll render it.

And the code that does the rendering:

public meowGraph
{
  public meowGraph(BufferedImage Target [...]) {
    this.Target = Target;
  }

  public void RenderArea(int x, int y) {
    Graphics2D g = this.Target.createGraphics();
    g.setColor(new Color(1.0f, 0.0f, 0.0f));
    g.fill(new Rectangle2D.Double(ColumnWidth * x, RowHeight * y, ColumnWidth, RowHeight));
  }
}

The main thing here is that I access the rendering context by the createGraphics call. Note that the Target property is the one you've passed by the constructor in the 1st block of code.
With this as a startoff, you can instantly use anything you learn in the 2D tutorial. (You are free to use any other setup too, this one is here just because copypaste was easier for me.) Happy coding!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.