Results 1 to 4 of 4
- 06-05-2008, 12:58 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 2
- Rep Power
- 0
how to pause and repaint in a loop, to draw a mathematical function.
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 and not in an applet...
if you type "java applet monte carlo inria" in google, click on the first link, and it will lead you to the result I want. But I don't want it 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... I linked a zip file to this thread for those who would have the courage, or the great kindness to try my code...
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
Java Code:/* * 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.
Java Code:/* * 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...
Integrale.javaJava Code:/* * 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"); } } } }
This class is used to parse a String into an evaluable function, thanks to the jep jar
Thank you very much!Java Code:/* * 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; } }
-
Please do not cross-post this in multiple forums:
- 06-05-2008, 10:31 PM #3
Couldn't run your jar or find any source files so I made up something that may answer your question(s).Java Code:C:\jexp\jep-2.4.1>java -jar jep-2.4.1.jar Failed to load Main-Class manifest attribute from jep-2.4.1.jar
Edit:Java Code:import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import java.util.Random; import javax.swing.*; public class AnimationTest implements ActionListener, Runnable { AnimationPanel animationPanel = new AnimationPanel(); Thread thread; boolean running = false; public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); if(ac.equals("START")) start(); if(ac.equals("STOP")) stop(); if(ac.equals("CLEAR")) animationPanel.clear(); } private void start() { if(!running) { running = true; thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } } private void stop() { running = false; if(thread != null) thread.interrupt(); thread = null; } public void run() { while(running) { try { Thread.sleep(250); } catch(InterruptedException e) { running = false; System.out.println("interrupted"); } animationPanel.addDot(); } } private JPanel getAnimationPanel() { return animationPanel; } private JPanel getUIPanel() { JPanel panel = new JPanel(); String[] ids = { "start", "stop", "clear" }; for(int i = 0; i < ids.length; i++) { JButton button = new JButton(ids[i]); button.setActionCommand(ids[i].toUpperCase()); button.addActionListener(this); panel.add(button); } return panel; } public static void main(String[] args) { AnimationTest test = new AnimationTest(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getAnimationPanel()); f.add(test.getUIPanel(), "Last"); f.pack(); f.setLocation(200,200); f.setVisible(true); } } class AnimationPanel extends JPanel { Random seed = new Random(); BufferedImage image; Path2D.Double path; public void addDot() { int x = seed.nextInt(100) - 5 + 300; int y = 225 - seed.nextInt(100); Graphics2D g2 = image.createGraphics(); g2.setPaint(new Color(210,200,230)); g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4)); g2.dispose(); repaint(); } public void clear() { int w = image.getWidth(); int h = image.getHeight(); Graphics2D g2 = image.createGraphics(); g2.setBackground(getBackground()); g2.clearRect(0, 0, w, h); g2.dispose(); repaint(); } protected void paintComponent(Graphics g) { if(image == null) init(); // Looks like you want the dots to appear // under/behind the graph. g.drawImage(image, 0, 0, this); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(Color.blue); g2.draw(path); } public Dimension getPreferredSize() { return new Dimension(600,450); } private void init() { int w = getWidth(); int h = getHeight(); int type = BufferedImage.TYPE_INT_RGB; image = new BufferedImage(w, h, type); Graphics2D g2 = image.createGraphics(); g2.setBackground(getBackground()); g2.clearRect(0, 0, w, h); g2.dispose(); path = initWave(w, h); } private Path2D.Double initWave(int w, int h) { Path2D.Double sineWave = new Path2D.Double(); for(int x = 0; x < w; x++) { double y = h/2.0 - Math.sin(((double)x/w)*2*Math.PI)*(h-2)/2.0; if(x > 0) sineWave.lineTo(x, y); else sineWave.moveTo(x, y); } return sineWave; } }
type "java applet monte carlo inria" in google showed this thread as number one. I looked around a little and couldn't find what you described.Last edited by hardwired; 06-05-2008 at 11:04 PM.
- 06-05-2008, 11:10 PM #4
Member
- Join Date
- Jun 2008
- Posts
- 2
- Rep Power
- 0
Thank you so much for your help, and all that work you've done. I've just found a solution, using a javax.Swing timer:
I changed the graphique1 class to this (by the way, i renamed it Graphique)
Java Code:package montecarlo; import javax.swing.*; import javax.swing.event.*; import java.util.*; import java.awt.*; import java.awt.event.*; public class Graphique extends JPanel implements ActionListener { int X[] = new int[10000]; int Y[] = new int[10000]; int min, max; Random rand = new Random(); int randX = 0; int randY = 0; boolean functionDrawed = false; double zoom = 1; Integrale integrale; javax.swing.Timer t; public Graphique(Integrale I) { integrale = I; min = (int) I.borneMin - 100; max = (int) I.borneMax + 100; for (int i = min; i < max; i++) { X[i - min] = (int) zoom * i + 300; Y[i - min] = (int) (225 - zoom * I.evaluer(i)); } this.setPreferredSize(new java.awt.Dimension(600, 450)); repaint(); t = new javax.swing.Timer(100, this); t.start(); } @Override public void paintComponent(java.awt.Graphics g) { if (!functionDrawed) { 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)); g.drawPolygon(X, Y, max - min); functionDrawed = true; } randX = rand.nextInt(100) + -5 + 300; randY = 225 - rand.nextInt(100); if ((225 - zoom * integrale.evaluer((randX - 300) / zoom)) > randY) { g.setColor(new java.awt.Color(255, 0, 0)); } else { g.setColor(new java.awt.Color(0, 0, 255)); } g.drawLine(randX, randY, randX, randY); } public void actionPerformed(ActionEvent e) { repaint(); } } and it works perfectly!
Similar Threads
-
Demonstration of the mathematical operators
By Java Tip in forum java.utilReplies: 0Last Post: 04-16-2008, 11:02 PM -
Repaint problem
By swimberl in forum Java 2DReplies: 1Last Post: 02-16-2008, 09:12 PM -
Help with Pause
By trill in forum Java AppletsReplies: 2Last Post: 09-28-2007, 08:50 PM -
Help with mathematical part in java
By lenny in forum Advanced JavaReplies: 3Last Post: 08-09-2007, 01:55 PM -
Help with mathematical methods
By Marcus in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 08:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks