943,912 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 25170
  • Java RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jun 4th, 2008
0

Re: How to draw a graph in Java applet

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
I wish I could help you with using more of the Graphics class's functions but unfortunately I truly HATE using Graphics from getGraphics in containers. I wouldn't be able to help you with setting the location of your Graphics object.

I personally prefer using GObjects, GCompounds, GLines that work on GCanvases.

The only method I have an issue with understanding is the move method in most GObject classes. I'm almost certain that it fires a thread that continuously offsets the object but i could be wrong.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Jun 4th, 2008
0

Re: How to draw a graph in Java applet

import java.awt.*;
import java.applet.*;

public class graphic extends Applet {
Button button1;
public void init() {
}

public void paint(Graphics g) {

int CYCLE=4;
int MAX = 1000;
int x1=0;
int x2=0;
g.setColor(Color.red);//color for axes
g.drawLine(0,150,700,150);//x-axis
g.drawLine(240,0,240,500);//y-axis
g.drawString("X-Axis", 430,140);//Label for x-axis
g.drawString("Y-Axis",200,270);//Label for y-axis
g.setColor(Color.blue);//color for the sin curve

for (int i=-130;i<=368;i++)
{
try
{
Thread.sleep(10); // 10 millisecond delay
}
catch(InterruptedException e)
{
e.printStackTrace();
}

x1 = (int)(100 * Math.sin(((i)*2*Math.PI*CYCLE)/(MAX)));
x2 = (int)(100 * Math.sin(((i+1)*2*Math.PI*CYCLE)/(MAX)));
g.drawLine(i+121,x1+138,(i+1)+121,x2+138);
}
g.setFont(new Font("Times New Roman",Font.BOLD,15));
g.drawString("IT WORKS ; )",100,50); }
}
//Here is the code
Last edited by Software guy; Jun 4th, 2008 at 8:52 pm.
Reputation Points: 16
Solved Threads: 18
Junior Poster
Software guy is offline Offline
151 posts
since May 2008
Jun 4th, 2008
1

Re: How to draw a graph in Java applet

import java.awt.*;
import java.applet.*;

public class graphic extends Applet {
Button button1;
public void init() {
}

public void paint(Graphics g) {

int CYCLE=4;
int MAX = 1000;
int x1=0;
int x2=0;
g.setColor(Color.red);//color for axes
g.drawLine(0,150,700,150);//x-axis
g.drawLine(240,0,240,500);//y-axis
g.drawString("X-Axis", 430,140);//Label for x-axis
g.drawString("Y-Axis",200,270);//Label for y-axis
g.setColor(Color.blue);//color for the sin curve

for (int i=-130;i<=368;i++)
{
try
{
Thread.sleep(10); // 10 millisecond delay
}
catch(InterruptedException e)
{
e.printStackTrace();
}

x1 = (int)(100 * Math.sin(((i)*2*Math.PI*CYCLE)/(MAX)));
x2 = (int)(100 * Math.sin(((i+1)*2*Math.PI*CYCLE)/(MAX)));
g.drawLine(i+121,x1+138,(i+1)+121,x2+138);
}
g.setFont(new Font("Times New Roman",Font.BOLD,15));
g.drawString("IT WORKS ; )",100,50); }
}
//Here is the code
java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. public class graphic extends Applet {
  5. Button button1;
  6. public void init() {
  7. }
  8.  
  9. public void paint(Graphics g) {
  10.  
  11. int CYCLE=4;
  12. int MAX = 1000;
  13. int WEIGHT = 100;
  14. int x1=0;
  15. int x2=0;
  16. g.setColor(Color.red);//color for axes
  17. g.drawLine(0,150,700,150);//x-axis
  18. g.drawLine(240,0,240,500);//y-axis
  19. g.drawString("X-Axis", 430,140);//Label for x-axis
  20. g.drawString("Y-Axis",200,270);//Label for y-axis
  21. g.setColor(Color.blue);//color for the sin curve
  22.  
  23. for (int i=-130;i<=368;i++)
  24. {
  25. try
  26. {
  27. Thread.sleep(10); // 10 millisecond delay
  28. }
  29. catch(InterruptedException e)
  30. {
  31. e.printStackTrace();
  32. }
  33.  
  34. x1 = (int)(WEIGHT * Math.sin(((i)*2*Math.PI*CYCLE)/(MAX)));
  35. x2 = (int)(WEIGHT * Math.sin(((i+1)*2*Math.PI*CYCLE)/(MAX)));
  36. g.drawLine(i+121,x1+138,(i+1)+121,x2+138);
  37. }
  38. g.setFont(new Font("Times New Roman",Font.BOLD,15));
  39. g.drawString("IT WORKS ; )",100,50); }
  40. }
  41. //Here is the code

Keep in mind that the MAX is really the maximum difference between the min-x and max-x values.

For example, if you can go from -100 to 100 in your graph, MAX should be the difference of the two, resulting in MAX = 200

Edit: Added an int named WEIGHT that controls the peak and valley of your sin graph. The greater the WEIGHT the deeper and steeper your waves, the lower the WEIGHT the more subtle your graph will be.
Last edited by Alex Edwards; Jun 4th, 2008 at 11:54 pm.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Jun 6th, 2008
0

Re: How to draw a graph in Java applet

Alex said:
Quote ...
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.
Well that's not exactly true; the gradient (i.e. rate of change) of a Sin curve depends on the input. Though I did misread the part about the circle, my bad

Good work Alex Though because of the nature of a Sin graph, one did not have to consider the changed between graphics and the Cartesian plane. On the screen y increases as we move down, however, on the Cartesian plane, y increase as we move up. Something to consider if ever you needed to draw, say a straight line.

So perhaps mark this post as Solved
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
147 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need help with the java code!
Next Thread in Java Forum Timeline: breaking a string block into list form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC