# painting math function graph

• 09-04-2011, 12:12 AM
batia
painting math function graph
Hello,

If anybody has experienced working with edu.hws packages.

Explanation of problem:
With this package i can draw graphs of mathematical functions such as x**2, exp{x}, etc...

In math function f(x): X --> Y, where X is domain.
The problem is that domain is always (-infinity; infinity).
Can Domain be changed to [-10,10] or other

For instance it could be used drawing such function with if condition:

F(x) = (x, when x <= 0; 3*x, when 0 < x <= 3; x**2, when x>3)

Or could be given any alternatives.

Example code for drawing functions:
Code:

```import java.awt.Color; import java.awt.Container; import javax.swing.JFrame; import edu.hws.jcm.awt.ExpressionInput; import edu.hws.jcm.data.Function; import edu.hws.jcm.data.Parser; import edu.hws.jcm.data.Variable; import edu.hws.jcm.draw.Axes; import edu.hws.jcm.draw.CoordinateRect; import edu.hws.jcm.draw.DisplayCanvas; import edu.hws.jcm.draw.DrawBorder; import edu.hws.jcm.draw.Graph1D; public class TestGraph extends DisplayCanvas{                         TestGraph(){                 super();                                 CoordinateRect coords = new CoordinateRect(-10,10,-1,10);                 addCoordinateRect(coords);                                 Parser parser = new Parser(1023);                 Variable x = new Variable("x");                 parser.add(x);                                 ExpressionInput input = new ExpressionInput("x", parser);                 Function func = input.getFunction(x);                 Graph1D graph1 = new Graph1D(func);                 input = new ExpressionInput("x**2", parser);                 Function func1 = input.getFunction(x);                                 Graph1D graph2 = new Graph1D(func1);                                                add( new Axes("x","f(x)") );                                 add( graph1 );                 add( graph2 );                 add( new DrawBorder(Color.darkGray, 2) );                        }                 public static void main(String [] args){                 JFrame frame = new JFrame();                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                 frame.setSize(500,500);                 Container con= frame.getContentPane(); // inherit main frame                 con.setBackground(Color.white);        // paint background                 con.add(new TestGraph());                 frame.setVisible(true);                    } }```
• 09-04-2011, 02:20 AM
Fubarable
What does the documentation for the library say, for this is your best resource with which to answer this and similar questions?
• 09-04-2011, 05:02 PM
batia
Re: painting math function graph
Quote:

Originally Posted by Fubarable
What does the documentation for the library say, for this is your best resource with which to answer this and similar questions?

No i did not find answer in documentation. So i was willing to find it here.

Or if you see alternatives which library should i use for paint, that help also would be helpful.
• 09-04-2011, 06:00 PM
DarrylBurke
Re: painting math function graph
You could take a look at JFreeChart

db
• 09-11-2011, 06:39 PM
batia
Re: painting math function graph
Thank you,

But that is not what I'm looking for.
• 09-17-2011, 09:58 PM
batia
Re: painting math function graph
Ok, i finally found the answer. Library creators shared it.

Here is the answer, how to do that.

Code:

```import java.awt.Color; import java.awt.Container; import javax.swing.JFrame; import edu.hws.jcm.awt.ExpressionInput; import edu.hws.jcm.data.Function; import edu.hws.jcm.data.Parser; import edu.hws.jcm.data.Variable; import edu.hws.jcm.draw.Axes; import edu.hws.jcm.draw.CoordinateRect; import edu.hws.jcm.draw.DisplayCanvas; import edu.hws.jcm.draw.DrawBorder; import edu.hws.jcm.draw.Graph1D; public class TestGraph2 extends DisplayCanvas{                         TestGraph2(){                 super();                                 CoordinateRect coords = new CoordinateRect(-10,10,-1,10);                 addCoordinateRect(coords);                                 Parser parser = new Parser(1023);                 Variable x = new Variable("x");                 parser.add(x);                                 ExpressionInput input = new ExpressionInput([COLOR="#0000FF"]"0<= x and x <= 2 ? x**2"[/COLOR], parser);                 Function func= input.getFunction(x);                                 Graph1D graph = new Graph1D(func);                                                add( new Axes("x","f(x)") );                                 add( graph );                 add( new DrawBorder(Color.darkGray, 2) );                        }                 public static void main(String [] args){                 JFrame frame = new JFrame();                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                 frame.setSize(500,500);                 Container con= frame.getContentPane(); // inherit main frame                 con.setBackground(Color.white);        // paint background                 con.add(new TestGraph2());                 frame.setVisible(true);                    } }```