# Thread: How to plot graph in java mathematical equations

1. Member Join Date
May 2013
Posts
3
Rep Power
0

## 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  Reply With Quote

2. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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  Reply With Quote

3. Member Join Date
May 2013
Posts
3
Rep Power
0

## 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);
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  Reply With Quote

4. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## 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  Reply With Quote

#### Posting Permissions

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