Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# drawing graph

Ranch Hand
Posts: 41
Hi,
I have given a task to draw graph for given X,Y values on imaginary X and Y axis not the screen co-ordinates.
I have set of X values ranging from 0.0 to 2.5
and Y values ranging from 0.0 to 1.2
Scale on X-Axis is something like this
------------------------------------------
0.0 0.50 1.00 1.50 2.0 2.5

Scale on Y-Axis is something like this
------------------------------------------
0.0 0.2 0.4 0.6 0.8 1.0 1.2
I need to draw points first for given (x,y) values and join the points with line.
Any hint about how to implement this?
I have thought something like this but I am stuck in this logic for decimal values.
Folowing is the code I am using to draw a scale (X- axis) on Applet window of size (500,500).
import java.awt.Graphics;
public class GraphApplet extends java.applet.Applet {
public void paint(Graphics g) {
g.drawRect(0,0,499,499);
g.drawLine(50,30,50,450);
g.drawLine(50,450,450,450);
g.drawString("0.00",50,465);
g.fillOval(50,450,3,3);
g.drawString("0.50",130,465);
g.fillOval(130,450,3,3);
g.drawString("1.00",210,465);
g.fillOval(210,450,3,3);
g.drawString("1.50",290,465);
g.fillOval(290,450,3,3);
g.drawString("2.00",370,465);
g.fillOval(370,450,3,3);
g.drawString("2.50",450,465);
g.fillOval(450,450,3,3);
}
}
<html>
<body>
<Applet code="GraphApplet.class" width=500 height=500 >
</applet>
</body>
</html>

X-axis I am drawing at (50,450,450,450)
0.0 on x-axis is at(50,450),
0.5 on x-axis is at(50+80,450),
so 0.1 will be at (50+ (0.1*80)/0.5 ,450)=(66,450) on Applet window.
If x value is something like 1.7 my logic screws up here as location of 1.7 will be at (77.2,450)
How I am going to draw line with decimal values of (x1,y1).

Is there an alternative way to draw graph using our own YX scale (imaginary axis)?
I realy appriciate if anyone put some light on this.
Thanks,

[ April 30, 2002: Message edited by: padma patil ]
[ April 30, 2002: Message edited by: padma patil ]

Manfred Leonhardt
Ranch Hand
Posts: 1492
You are never going to avoid rounding errors since you are drawing floating point values onto a pixel screen (integers). The best you can do is just use Math.round and live with the answer. No human will ever be able to tell the difference anyway ...
Regards,
Manfred.

Ranch Hand
Posts: 41
Hi,
Here is my complete code to draw graph on imaginary coornates. I am getting correct graph but wanted to know to make the line look like a little curved rather than just straigt line between two points.
CODE
import java.awt.Graphics;
import java.awt.Color;
public class GraphApplet extends java.applet.Applet {

double x[]={0.5,1.0,1.20,1.21,1.22,1.23,1.24,1.25,1.5,2.0,2.5};
double y[]={0.2,0.4,0.6,0.6,0.6,0.6,0.6,0.6,0.6,0.8,1.0};
// double x[]={1.0,1.25};
// double y[]={0.4,0.6};
double x1,y1,x2,y2,lx=0,ly=0,ux=0,uy=0;
public void paint(Graphics g) {

g.setColor(new Color(255, 255, 192));
g.fillRect(0,0,599,599);
// Imaginary X-Axis Scale
g.setColor(Color.black);
g.drawLine(50,510,450,510);
g.drawString("0.00",50,535);
g.fillOval(50,510,3,3);
g.drawString("0.50",130,535);
g.fillOval(130,510,3,3);
g.drawString("1.00",210,535);
g.fillOval(210,510,3,3);
g.drawString("1.50",290,535);
g.fillOval(290,510,3,3);
g.drawString("2.00",370,535);
g.fillOval(370,510,3,3);
g.drawString("2.50",450,535);
g.fillOval(450,510,3,3);

// Imaginary Y Axis

g.drawLine(50,30,50,510);
g.drawString("1.2",35,30);
g.fillOval(50,30,3,3);
g.drawString("1.0",35,110);
g.fillOval(50,110,3,3);
g.drawString("0.8",35,190);
g.fillOval(50,190,3,3);
g.drawString("0.6",35,270);
g.fillOval(50,270,3,3);
g.drawString("0.4",35,350);
g.fillOval(50,350,3,3);
g.drawString("0.2",35,430);
g.fillOval(50,430,3,3);
g.drawString("0.0",35,510);
g.fillOval(50,510,3,3);

for(int i=0; i<x.length; i++)
{
x1=50+((x[i]*80)/0.5);
y1=510-((y[i]*80)/0.2);
lx=x1;
ly=y1;
g.fillOval((int)x1,(int)y1,3,3);
if(i != (x.length -1))
{
int j=i+1;
x2=50+((x[j]*80)/0.5);
y2=510-((y[j]*80)/0.2);
ux=x2;
uy=y2;
g.setColor(Color.red);

g.drawLine((int)x1,(int)y1,(int)x2,(int)y2);
} //end of if

} //end of for

}

public void start()
{
repaint();
}
}
-