Results 1 to 6 of 6
  1. #1
    batia is offline Member
    Join Date
    Feb 2011
    Location
    Lithuania
    Posts
    16
    Rep Power
    0

    Default 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:
    Java 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);	    
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

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

  3. #3
    batia is offline Member
    Join Date
    Feb 2011
    Location
    Lithuania
    Posts
    16
    Rep Power
    0

    Default Re: painting math function graph

    Quote Originally Posted by Fubarable View Post
    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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: painting math function graph

    You could take a look at JFreeChart

    db

  5. #5
    batia is offline Member
    Join Date
    Feb 2011
    Location
    Lithuania
    Posts
    16
    Rep Power
    0

    Default Re: painting math function graph

    Thank you,

    But that is not what I'm looking for.

  6. #6
    batia is offline Member
    Join Date
    Feb 2011
    Location
    Lithuania
    Posts
    16
    Rep Power
    0

    Default Re: painting math function graph

    Ok, i finally found the answer. Library creators shared it.

    Here is the answer, how to do that.

    Java 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);	    
    	}
    }

Similar Threads

  1. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 09:00 PM
  2. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 06:23 AM
  3. Painting
    By xael in forum New To Java
    Replies: 6
    Last Post: 09-06-2010, 05:10 AM
  4. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  5. math.random function help
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 03:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •