Need Java Help - Making a graph ...

Status
Not open for further replies.

moullas

Ars Scholae Palatinae
620
First of all...i'm just learning Java so this must be a trivial thing you someone who knows his stuff :)

My program is available at http://40th.simplyadvanced.net/temp/Java/PiApplet.zip

**Problem Description**
-----------------------
I wrote an Applet that calculates Pi, and i want to make a graph showing the fluctuation of values as until it reaches a certain value close to 3.14...

I tried to takle the problem by drawing a line with it's height taken by pi*10 (or 100) so it would give 26 pixels at first, 33 and so on thus making a graph. Something though is going wrong.

Is there a simpler way of doing this (storing the values in an array or something and then passing it to a graph function i have not heard of yet??

I am using the 1.3.1 JDK btw

Any help appreciated thanx for bearing with me
:D
 

Nimdok

Ars Scholae Palatinae
1,214
Move all of your general code out of the paint() method. paint() is only called when Java thinks your canvas needs repainting.

I don't have time to test your program, but you might try something like this:

code:<HR>
Code:
protected int pi;

public MyClass() {
  for(int i=0;i < 100;i++) {
    //calculations...
    pi = whatever;
    repaint(1); //make AWT call repaint in 1 millisecond
  }
}

public void paint(Graphics g) {
  //your drawing code
}

If that doesn't work it might be necessary to spawn a thread which calls repaint. If that's the case, come back and post the error messages.
 

Nimdok

Ars Scholae Palatinae
1,214
Addendum:

Move that code out of your constructor and into the start() method (which is called by Java when your applet should start executing).

code:<HR>
Code:
public class MyClass() {
}

public void start() {
  //retrieve integer
  for(int i=0;i < 100;i++) {
    pi = whatever;
    repaint(1);
  }
}

public void paint(Graphics g) {
  //your painting code
}
 
Status
Not open for further replies.