1 pièce(s) jointe(s)
afficher une fonction mathématique.
Bonjour.
Je suis en train de faire un programme à qui on donne:
_ une fonction (par exemple x^2)
_ une borne min et une borne max
_ un nombre d'iterations
(Pas besoin de cocher le bouton radio "valeur", ou aucune des autres checkbox)
et qui doit afficher la fonction, et une "pluie" de points qui tombent aléatoirement sur le graphe.
Je souhaiterai obtenir qqch comme ce qu'on trouve ici
Une fois les données entrées, on clique sur OK. ActionPerformed appelle alors la méthode go() de la classe Fenetre qui à son tour fait une boucle dans laquelle elle appelle la méthode repaint() avec un Thread.sleep(10) entre chaque iteration.
Le problème qui se pose c'est que la boucle "tourne" plusieurs fois, mais j'ai l'impression que paintComponent() n'est appellée qu'une fois, puisqu'à la fin j'ai bien ma fonction de déssinée, mais un seul point est affiché.
Voila une partie du code source, en version simplifiée...
J'ai mis en piece jointe un fichier zip contenant le jar necessaire au parsing de la fonction mathematique.(pour ceux qui auraient le courage ou l'infini bonté de vouloir tester mon code)
Main.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| /*
* 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
C'est la classe qui lance le panel pour dessiner la fonction, et UN seul point aux coordonnées aléatoires.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| /*
* 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
C'est la classe qui lance le JFrame. Tout le début n'est que déclarations de JtextField etc...
La méthode go() tout en bas se charge d'appeller repaint() et Thread.sleep(10) au sein d'une boucle. De cette facon la fonction est (sensée etre) redessinée plusieurs fois, mais c pas grave on ne voit pas de difference. PUIS un seul point aux coordonées aléatoire est (sensé etre) dessiné à chaque iteration.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/*
* 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[i].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
Cette classe est utile pour "parser" une chaine de caractères par exemple "x^2" en une fonction mathématique evaluable. Ceci grace au jar de jep
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
/*
* 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;
}
} |