Results 1 to 4 of 4
  1. #1
    alphablitzer is offline Member
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default How to plot graph in java mathematical equations

    I'm confused in creating graphs here in java.
    My professor is not teaching anything here. And we have to do it ourselves. I hope someone could help me

    y = x^2 , y = sin x and y = mx + b ; m = 1,2 and 3 , b = 0, 1 and 2

    x ranges from 0 to 100 at increments of 1

    please help

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: How to plot graph in java mathematical equations

    Well, as an example you can print a sine wave vertically on any ascii console. Think about the min and maximum range of a sine function. Then think how to convert that to a range of 0 to n where n is your line width.

    Now just iterate over the Math.sin function for a couple of intervals of 2*PI. You can go from 0 to 4*pi in whatever increment you like. You can just print out the values first to see what they look like. I recommend converting them to integers. Then think how you could print out characters which would show the shape of the sine wave.

    Give it a try and come back to the forum for help.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    alphablitzer is offline Member
    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Default Re: How to plot graph in java mathematical equations

    Java Code:
    import javax.swing.JFrame;
    import java.awt.*;
    import java.awt.Color;
    
    
    public final class Sine extends JFrame
    {
    	private static Graphics gBuf = null;
    	private static GraphPaperCanvas canvas = null;
    	private static Image vm = null;
    	private int x, y;
    	private int w, h;
    	
    public static void main( String[] args )
    	{
    		double y;
    		
    		Sine gp1 = new Sine(10,10);
    		for ( double x = -10; x<=10; x+=0.01 )
    		{
    			
    			y = Math.sin(x); //equation
    			gp1.drawPoint(x,y);
    		}
    
    		
    	}
    	public Sine( int x, int y )
    	{
    		if ( canvas == null )
    		{
    			setTitle("Sine Curve");
    			setSize(340,370);
    			setLocation(20,50);
    
    			canvas = new GraphPaperCanvas(null);
    			getContentPane().add(canvas);
    			setVisible(true);
    
    			vm = canvas.createImage(1100,950);
    			gBuf = vm.getGraphics();
    			canvas.setVm(vm);
    
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		}
    
    		this.x = x;
    		this.y = y;
    		w = 300;
    		h = 300;
    		
    		drawBounds();
    		gBuf.setColor( Color.GREEN );
    	}
    	
    	public void drawBounds()
    	{
    		Color cur = gBuf.getColor();
    		gBuf.setColor( Color.LIGHT_GRAY );
    		for ( int d=0; d<w; d+=w/20 )
    			gBuf.drawLine( x+d, y+0, x+d, y+h );
    		for ( int d=0; d<h; d+=h/20 )
    			gBuf.drawLine( x+0, y+d, x+h, y+d );
    
    		gBuf.setColor( Color.BLACK );
    		gBuf.drawRect( x, y, w+1, h+1 );
    		gBuf.drawLine( x+w/2, y+0, x+w/2, y+h );
    		gBuf.drawLine( x+0, y+h/2, x+w, y+h/2 );
    		gBuf.setColor( cur );
    		canvas.repaint();
    	}	
    	public void setColor( Color c )
    	{
    		gBuf.setColor(c);
    	}
    	
    	public void drawPoint( double px, double py )
    	{
    		if ( px > 10 || px < -10 || py > 10 || py < -10 )
    			return;
    
    		px *= w/20;
    		py *= h/20;
    		px += w/2 + 1;
    		py = h/2 - py + 1;
    
    		gBuf.drawLine( x+(int)px, y+(int)py, x+(int)px, y+(int)py );
    		canvas.repaint();
    	}
    }
    class GraphPaperCanvas extends Canvas
    {
    	private Image vm;
    	
    	public GraphPaperCanvas( Image vm )
    	{
    		this.vm = vm;
    		setBackground( Color.white );
    	}
    	
    	public void setVm( Image vm )
    	{
    		this.vm = vm;
    	}
    	
            @Override
    	public void paint( Graphics g )
    	{
    		g.drawImage(vm,0,0,this);
    	}
    	
            @Override
    	public void update(Graphics g) { paint(g); }	
    
    }
    I have made a sine curve but i'm having a hard time putting values the numbers

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: How to plot graph in java mathematical equations

    Well, clearly you understand how to plot items at various points. So consider using drawString() and setFont() methods of the Graphics class.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 11
    Last Post: 01-14-2012, 06:46 PM
  2. java graph plot taking file data
    By niloR19 in forum AWT / Swing
    Replies: 3
    Last Post: 11-28-2011, 06:56 PM
  3. How to plot graph in java for given samples
    By annesteve31 in forum New To Java
    Replies: 27
    Last Post: 11-29-2009, 09:27 PM
  4. i want plot realtime graph in java
    By santhosh_el in forum New To Java
    Replies: 3
    Last Post: 02-26-2009, 08:32 AM
  5. Plot 2D graph in Java from RS-232 data
    By spratana in forum Java 2D
    Replies: 4
    Last Post: 02-11-2009, 06:49 PM

Posting Permissions

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