| | |
How to draw a graph in Java applet
Thread Solved |
•
•
Join Date: May 2008
Posts: 38
Reputation:
Solved Threads: 2
Hi,
I am jahan, i just started java applets and i was trying to draw a graph. The idea i am using is based on drawLine(xmin,ymin,xmax,ymax). I wanted to get a graph for sinx which is proving to be difficult. i used following syntax to implement my idea:
import java.awt.*;
import java.applet.*;
public class graphic extends Applet {
Button button1;
public void init() {
}
public void paint(Graphics g) {
Button button1 = new Button("Plot");
add(button1);
g.setColor(Color.blue);
g.drawLine(600,0,600,1000); // x-axis
g.drawLine(0,350,1400,350);// y-axis
for (int i=0;i<=1000;i++)
{
g.drawLine(i,(int)Math.sin(i),i,(int)Math.sin(i));//Suppose to give me a graph
//even tho at random location
}
}
}
But result of this code is just a straight line along x-axis , the only thing which comes to my mind is about using cast in here when i did (int)Math.sinx, it is giving me strange value. It cud be because all the value below 0.5 are taken as 0 and above 0.5 are taken as 1.So if anyone can suggest me a way to solve this problem.
I am jahan, i just started java applets and i was trying to draw a graph. The idea i am using is based on drawLine(xmin,ymin,xmax,ymax). I wanted to get a graph for sinx which is proving to be difficult. i used following syntax to implement my idea:
import java.awt.*;
import java.applet.*;
public class graphic extends Applet {
Button button1;
public void init() {
}
public void paint(Graphics g) {
Button button1 = new Button("Plot");
add(button1);
g.setColor(Color.blue);
g.drawLine(600,0,600,1000); // x-axis
g.drawLine(0,350,1400,350);// y-axis
for (int i=0;i<=1000;i++)
{
g.drawLine(i,(int)Math.sin(i),i,(int)Math.sin(i));//Suppose to give me a graph
//even tho at random location
}
}
}
But result of this code is just a straight line along x-axis , the only thing which comes to my mind is about using cast in here when i did (int)Math.sinx, it is giving me strange value. It cud be because all the value below 0.5 are taken as 0 and above 0.5 are taken as 1.So if anyone can suggest me a way to solve this problem.
Keep in mind what Graphics.drawLine does.
You're trying to draw a point from i to i, and Math.sin(i) to Math.sin(i) so you're going to get something like a line due to the fact that your points aren't varying from x1 to x2 and y1 to y2.
Try g.drawLine(i,(int)Math.sin(i),i + 2,(int)Math.sin(i))));
You're trying to draw a point from i to i, and Math.sin(i) to Math.sin(i) so you're going to get something like a line due to the fact that your points aren't varying from x1 to x2 and y1 to y2.
Try g.drawLine(i,(int)Math.sin(i),i + 2,(int)Math.sin(i))));
Last edited by Alex Edwards; Jun 4th, 2008 at 12:21 am.
•
•
•
•
Keep in mind what Graphics.drawLine does.
You're trying to draw a point from i to i, and Math.sin(i) to Math.sin(i) so you're going to get something like a line due to the fact that your points aren't varying from x1 to x2 and y1 to y2.
Try g.drawLine(i,(int)Math.sin(i),i + 2,(int)Math.sin(i))));
it might be Math.pi or Math.Pi for the pi constant... bah!
I'm not anywhere near a java compiler atm >_>
Last edited by Alex Edwards; Jun 4th, 2008 at 1:54 am.
Now that I think about it, you probably don't want to simply use Math.sin(i) in your statement.
This means that you're going to go around the circle VERY fast as i increments. Your wavelength will most likely be small unless you use some kind of scalar that multiplies by the sin function (like 50 or so).
If anything, think about how many sin cycles you want in 1000 increments.
Let's say you want 4 cycles (where a cycle is simply 360 degrees (or 2pi radians) reached). Then you'd have to find out when your number reaches a cycle and multiply by that value.
final double CYCLE = 4
final double MAX = 1000
math.sin( ((i + 2)* Math.pi * CYCLE) / ( MAX) ) for the second y arg, and most likely
math.sin(((i) * Math.pi * CYCLE)/ MAX) for the first y arg.
This means that you're going to go around the circle VERY fast as i increments. Your wavelength will most likely be small unless you use some kind of scalar that multiplies by the sin function (like 50 or so).
If anything, think about how many sin cycles you want in 1000 increments.
Let's say you want 4 cycles (where a cycle is simply 360 degrees (or 2pi radians) reached). Then you'd have to find out when your number reaches a cycle and multiply by that value.
final double CYCLE = 4
final double MAX = 1000
math.sin( ((i + 2)* Math.pi * CYCLE) / ( MAX) ) for the second y arg, and most likely
math.sin(((i) * Math.pi * CYCLE)/ MAX) for the first y arg.
•
•
Join Date: Aug 2006
Posts: 137
Reputation:
Solved Threads: 11
Hi guys, drawLine() doesn't require the points to vary. In fact drawLine(10,10,10,10) would simply result in a point being drawn.
It's Math.PI because all constants, as per the Java convention, are in uppercase. He isn't using degrees though so there isn't a need to convert between degrees and radians; Math.sin(x) where x is an angle measured in radians. The conversion is degrees * PI/180 btw.
He isn't drawing a circle and his approach is almost correct...
What you need to remember Jahan, is that sin graphs have an amplitude of 1 unit. That is, your y value is going to range between -1 and 1 (at least in this case). Since you're dealing with a continuous function, your (int) conversion is going to give you values of either -1, 0 or 1 and this is not what you want. I suggest multiply the double value you get from the sin method with some constant before you convert the value to int. I hope this makes sense? This constant should be used to scale your x value of the Cartesian plane to pixels so perhaps 1 x unit is 10 pixels.
Moreover, your graph isn't going to be drawn on the axises you created because your (x, y) pairs in the drawLine() method don't match the values of your axises. It's not a difficult thing to do, but it does require you to think of the differences between a Cartesian plane and the pixel co-ordinate system you're using on your computer.
It's Math.PI because all constants, as per the Java convention, are in uppercase. He isn't using degrees though so there isn't a need to convert between degrees and radians; Math.sin(x) where x is an angle measured in radians. The conversion is degrees * PI/180 btw.
He isn't drawing a circle and his approach is almost correct...
What you need to remember Jahan, is that sin graphs have an amplitude of 1 unit. That is, your y value is going to range between -1 and 1 (at least in this case). Since you're dealing with a continuous function, your (int) conversion is going to give you values of either -1, 0 or 1 and this is not what you want. I suggest multiply the double value you get from the sin method with some constant before you convert the value to int. I hope this makes sense? This constant should be used to scale your x value of the Cartesian plane to pixels so perhaps 1 x unit is 10 pixels.
Moreover, your graph isn't going to be drawn on the axises you created because your (x, y) pairs in the drawLine() method don't match the values of your axises. It's not a difficult thing to do, but it does require you to think of the differences between a Cartesian plane and the pixel co-ordinate system you're using on your computer.
Last edited by PoovenM; Jun 4th, 2008 at 5:56 am.
You may want to use a GeneralPath or Path2D.Float or Graphics.drawPolyline(int[],int[],int) for this. All of those provide methods to draw a series of points connected by lines.
•
•
•
•
Hi guys, drawLine() doesn't require the points to vary. In fact drawLine(10,10,10,10) would simply result in a point being drawn.
It's Math.PI because all constants, as per the Java convention, are in uppercase. He isn't using degrees though so there isn't a need to convert between degrees and radians; Math.sin(x) where x is an angle measured in radians. The conversion is degrees * PI/180 btw.
He isn't drawing a circle and his approach is almost correct...
What you need to remember Jahan, is that sin graphs have an amplitude of 1 unit. That is, your y value is going to range between -1 and 1 (at least in this case). Since you're dealing with a continuous function, your (int) conversion is going to give you values of either -1, 0 or 1 and this is not what you want. I suggest multiply the double value you get from the sin method with some constant before you convert the value to int. I hope this makes sense? This constant should be used to scale your x value of the Cartesian plane to pixels so perhaps 1 x unit is 10 pixels.
Moreover, your graph isn't going to be drawn on the axises you created because your (x, y) pairs in the drawLine() method don't match the values of your axises. It's not a difficult thing to do, but it does require you to think of the differences between a Cartesian plane and the pixel co-ordinate system you're using on your computer.
for example
(y2 - y1) / (x2 - x1) = 0 which is a constant and he would definitely get a straight line from whatever a-to-b interval.
Unfortunately there is a serious loss of precision when using
(int)Math.sin(i) because it will almost always evaluate to zero since the only time Math.sin(i) is a true integer is when it is -1 or 1 which is its peak. As I increases you will skip PI or 2PI using integer incrementing in Math.sin(i) which means you will skip the maximum and minimum values that bring Math.sin(i) to -1 or 1 and immediately generate a graph that looks much like it does not vary (you'll get numbers between -1 and 1 and cast them as an int in which an int cant hold the information and be immediately cast to the next best thing - zero).
You have a few "bumps" in your graph most likely because you reached a multiple of PI.
Also the logic I provided wasn't for a circle. It was to make the y and x vary enough to generate a sin graph, however there is no variance and there is no way to generate a precise enough number before casting it into an int to satisfy the method.... unless he can pull off something like--
(int)(10 * Math.sin(i))
--to cast the value into an int after it has been increased past 1 for better results.
g.drawLine(i,(int)(50 * Math.sin(((i)* 2 * Math.PI * CYCLE) / ( MAX))),(i+1),(int)(50 * Math.sin(((i+1)* 2 * Math.PI * CYCLE) / ( MAX))));
where CYCLE is a final int 4 and MAX is final int 1000
This actually worked.
•
•
Join Date: May 2008
Posts: 38
Reputation:
Solved Threads: 2
Thanks for your replies , i really wasn't expecting so many replies for my simple question.Because of your explanation in detail i understood it properly.Mathematically Alex is 100% right , because it really gives me a gradient of zero which is literally a straight line. I have changed my code and it gives me a sine curve but , it sticks to the top of the window but i hope that can be figured out . Once again thanks for the help and thanks for supporting the idea that g.drawLine(i,i,i,i) represents a point .Hopefully i ill be posting the code when its all done
Last edited by Software guy; Jun 4th, 2008 at 7:54 pm.
![]() |
Similar Threads
- Tic Tac Toe help (Java)
- Applet - Graphing Quadratic Function Help (Java)
- Graph??? Help For My Project-java Applet (Java)
- Create stats from a text file (Java)
Other Threads in the Java Forum
- Previous Thread: Need help with the java code!
- Next Thread: breaking a string block into list form
| Thread Tools | Search this Thread |
3d 6 @param affinetransform android api applet application arc array arrays automation binary bluetooth bold byte c++ chat class client code color compare component coordinates database detection doctype eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool keyword linux list loop map method methods mobile netbeans newbie nextline object pong print problem producer program programming project projectideas read recursion recursive replaysolutions rim scanner sell server set size sms sort sql string swing terminal threads tree web websites windows






