How to draw a graph in Java applet

Thread Solved

Join Date: May 2008
Posts: 38
Reputation: Software guy is an unknown quantity at this point 
Solved Threads: 2
Software guy Software guy is offline Offline
Light Poster

How to draw a graph in Java applet

 
0
  #1
Jun 3rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: How to draw a graph in Java applet

 
0
  #2
Jun 4th, 2008
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))));
Last edited by Alex Edwards; Jun 4th, 2008 at 12:21 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: How to draw a graph in Java applet

 
0
  #3
Jun 4th, 2008
Originally Posted by Alex Edwards View Post
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))));
sorry for quoting myself, but instead of Math.sin(i) use Math.sin(i + ((2 * i)/(2 * Math.PI))) in the last argument.

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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: How to draw a graph in Java applet

 
0
  #4
Jun 4th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 137
Reputation: PoovenM is on a distinguished road 
Solved Threads: 11
PoovenM PoovenM is offline Offline
Junior Poster

Re: How to draw a graph in Java applet

 
0
  #5
Jun 4th, 2008
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.
Last edited by PoovenM; Jun 4th, 2008 at 5:56 am.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,558
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 530
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How to draw a graph in Java applet

 
0
  #6
Jun 4th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: How to draw a graph in Java applet

 
0
  #7
Jun 4th, 2008
Originally Posted by PoovenM View Post
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.
There is no rate of change in his function as i increments. Basically the graph isn't dependent on i, so he ends up with a constant graph, or a straight line.

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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: How to draw a graph in Java applet

 
0
  #8
Jun 4th, 2008
By the way, it might not be a good idea to add a button in paint(Graphics g) method, because each time you resize the applet paint is called (which makes sense, because it needs to redraw components when it has been resized).
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,558
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 530
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: How to draw a graph in Java applet

 
0
  #9
Jun 4th, 2008
Yes, you definitely do not want to be adding components in paint().
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 38
Reputation: Software guy is an unknown quantity at this point 
Solved Threads: 2
Software guy Software guy is offline Offline
Light Poster

Re: How to draw a graph in Java applet

 
0
  #10
Jun 4th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 9341 | Replies: 13
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC