Hello
First of all, Forgive my english...
I would like to make a program in which you give :
_ a function (for exemple x^2)
_ min and max
_ a number of "iterations" (I'm not sure it's the correct word in english, but nevermind)
and then, the program shows the graph of that function, along with a "rain" of dots, chosen randomly
I want something that looks like this: http://www-sop.inria.fr/mefisto/java/tutorial1/node15.html
but not in an applet...
Once the data is entered (if you give a try to my source code, don't pay attention to the checkBoxes, it's not necessary to check them) , and the OK button is clicked , ActionPerformed calls the method go() from class Fenetre (window in english). And this go() method runs a loop in wich it calls repaint() and the method Thread.sleep(10) in each loop.
The method paintComponent is supposed to draw one "random dot" plus the mathematical function each time.
If I call repaint several time, it should redraw the function (but since the function didn't change, we see no difference), and ADD another "random dot".
My problem is the loop runs several times, but it's like it calls repaint() once, because once the loop is done, I see the graph of my function, but only one "random" dot is on the graph.
Here is a simplified version of my source code...
Please help me!
The program needs a jar file to run... Unfortunately, it seems that it's impossible to link a file to a post here. Anyway, for those who would have the courage, or the great kindness to try my code, the file jep-2.4.1.jar is available here : http://www.singularsys.com/download/jep-2.4.1-ext-1.1.1-gpl.zip
Although it seems long and complicated, the main issue is between method go() from class Fenetre (last method), and graphique1 class.
here is the code:
Main.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package montecarlo;
/**
*
* @author Abdelhamid
*/
import java.io.*;
import java.util.*;
import java.math.*;
import org.nfunk.jep.*;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Fenetre it = new Fenetre();
}
}
graphique1.java
This is the class where paintComponent is.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package montecarlo;
import javax.swing.*;
import java.util.*;
public class graphique1 extends JPanel {
int X[] = new int[10000];
int Y[] = new int[10000];
int min, max;
Random rand = new Random();
int randX = 0;
int randY = 0;
/** Creates new form Graphique */
public graphique1(Integrale I) {
min = (int) I.borneMin - 100;
max = (int) I.borneMax + 100;
for (int i = min; i < max; i++) {
X[i - min] = i + 300;
Y[i - min] = (int) (225 - 0.1 * I.evaluer(i));
}
this.setPreferredSize(new java.awt.Dimension(600, 450));
}
@Override
public void paintComponent(java.awt.Graphics g) {
g.setColor(new java.awt.Color(255, 255, 255));
g.fill3DRect(0, 0, 599, 449, true);
g.setColor(new java.awt.Color(0, 0, 0));
randX = rand.nextInt(100) + -5 + 300;
randY = 225 - rand.nextInt(100);
g.drawLine(randX, randY, randX, randY);
g.drawPolygon(X, Y, max - min);
}
}
Fenetre.java
This is where go() method is. It calls repaint() and Thread.sleep(10) within a loop.
All the begining of this is class is just full of declarations of JLabel and JTextField etc...
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package montecarlo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.event.*;
public class Fenetre {
private JFrame fenetre = new JFrame("PFE MONTE CARLO 2008") {
{
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
};
final int cte = 400;
private JPanel[] panel;
private JButton button;
private JLabel[] label;
private javax.swing.JTextField[] jText;
private javax.swing.JRadioButton[] jRadio;
private javax.swing.JCheckBox[] jCheck;
//graphique1 graph;
public Fenetre() {
panel = new JPanel[12];
for (int i = 0; i < 12; i++) {
panel[i] = new JPanel();
}
label = new JLabel[13];
jText = new JTextField[5];
jRadio = new JRadioButton[2];
jCheck = new JCheckBox[4];
jText[0] = new JTextField(25);
jText[1] = new JTextField(8);
jText[2] = new JTextField(8);
jText[3] = new JTextField(14);
jText[4] = new JTextField(14);
jCheck[0] = new JCheckBox("Monte Carlo ");
jCheck[1] = new JCheckBox("Rectangle ");
jCheck[2] = new JCheckBox("Trapezes ");
jCheck[3] = new JCheckBox("Simpson ");
jRadio[0] = new JRadioButton("Iterations");
jRadio[1] = new JRadioButton("Valeur ");
label[0] = new JLabel("f(x)=");
label[1] = new JLabel("min");
label[2] = new JLabel("max");
label[3] = new JLabel(" Complexit�");
label[4] = new JLabel("Resultats");
label[5] = new JLabel("0.000000");
label[6] = new JLabel("0.000000");
label[7] = new JLabel("0.000000");
label[8] = new JLabel("0.000000");
label[9] = new JLabel("0.000000");
label[10] = new JLabel("0.000000");
label[11] = new JLabel("0.000000");
label[12] = new JLabel("0.000000");
jText[3].setPreferredSize(new Dimension(100, 25));
jText[4].setPreferredSize(new Dimension(100, 25));
for (int i = 3; i < 13; i++) {
label.setPreferredSize(new Dimension(100, 25));
}
button = new JButton("ok");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
go();
}
});
panel[0].add(label[0]);
panel[0].add(jText[0]);
panel[0].add(button);
panel[0].setPreferredSize(new Dimension(cte, 30));
panel[7].add(label[1]);
panel[7].add(label[2]);
panel[7].setPreferredSize(new Dimension(25, 60));
panel[8].add(jText[1]);
panel[8].add(jText[2]);
panel[8].setPreferredSize(new Dimension(150, 60));
panel[1].add(panel[7]);
panel[1].add(panel[8]);
panel[1].setPreferredSize(new Dimension(cte, 60));
panel[2].add(jRadio[0]);
panel[2].add(jRadio[1]);
panel[2].setPreferredSize(new Dimension(80, 60));
panel[3].add(jText[3]);
panel[3].add(jText[4]);
panel[3].setPreferredSize(new Dimension(250, 60));
panel[11].add(panel[2]);
panel[11].add(panel[3]);
panel[11].setPreferredSize(new Dimension(cte, 80));
panel[6].add(new JPanel() {
{
setPreferredSize(new Dimension(100, 25));
}
});
panel[6].add(jCheck[0]);
panel[6].add(jCheck[1]);
panel[6].add(jCheck[2]);
panel[6].add(jCheck[3]);
panel[4].add(label[3]);
panel[4].add(label[5]);
panel[4].add(label[7]);
panel[4].add(label[9]);
panel[4].add(label[11]);
panel[5].add(label[4]);
panel[5].add(label[6]);
panel[5].add(label[8]);
panel[5].add(label[10]);
panel[5].add(label[12]);
panel[4].setPreferredSize(new Dimension(100, 200));
panel[5].setPreferredSize(new Dimension(100, 200));
panel[6].setPreferredSize(new Dimension(100, 200));
panel[9].add(panel[0]);
panel[9].add(panel[1]);
panel[9].add(panel[2]);
panel[9].add(panel[3]);
panel[9].add(panel[11]);
panel[9].add(panel[6]);
panel[9].add(panel[4]);
panel[9].add(panel[5]);
panel[9].setPreferredSize(new Dimension(cte, 450));
panel[10].add(panel[9]);
fenetre.add(panel[10]);
fenetre.setVisible(true);
fenetre.pack();
}
public void go() {
String fonction = jText[0].getText();
float min = Float.parseFloat(jText[1].getText());
float max = Float.parseFloat(jText[2].getText());
int nbOpns = Integer.parseInt(jText[3].getText());
Integrale uneIntegrale = new Integrale(fonction, min, max);
graphique1 graph = new graphique1(uneIntegrale);
panel[10].add(graph);
fenetre.pack();
for (int i = 0; i < 1000; i++) {
graph.repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println("awakened prematurely");
}
}
}
}
Integrale.java
This class is used to parse a String into an evaluable function, thanks to the jep jar
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package montecarlo;
import org.nfunk.jep.*;
/**
*
* @author Abdelhamid
*/
public class Integrale {
org.nfunk.jep.JEP fonction;
protected int nbOperateurs=0;
protected double borneMin;
protected double borneMax;
protected double valeurAttendue;
public Integrale(String fonc, double min, double max) {
fonction = new JEP();
fonction.addStandardFunctions();
fonction.addStandardConstants();
fonction.addVariable("x", 0);
fonction.parseExpression(fonc);
borneMin = min;
borneMax = max;
int i = 0;
while (i != fonc.length()) {
if (fonc.charAt(i) == '+' || fonc.charAt(i) == '-') {
nbOperateurs++;
} else if (fonc.charAt(i) == '*' || fonc.charAt(i) == '/') {
nbOperateurs++;
} else if (fonc.charAt(i) == '^') {
nbOperateurs+=Integer.parseInt(""+fonc.charAt(i+1));
}
i++;
}
}
public Integrale(String fonc, double min, double max, double val) {
this(fonc, min, max);
valeurAttendue = val;
}
public double evaluer(double val) {
fonction.addVariable("x", val);
return fonction.getValue();
}
public double getMin() {
return borneMin;
}
public double getMax() {
return borneMax;
}
}
Thank you very much!