Results 1 to 6 of 6
  1. #1
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Thumbs down run() only works after minimize and maximize...

    I've got a strange problem here...

    I have the next code (a bit long):

    Java Code:
    /**
    *Applet CaidaBola
    *
    * <applet code= "CaidaBola.class" width = "400" height="400"></applet>
    */
    
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    
    
    public class CaidaBola extends Applet 
    	implements Runnable, ActionListener{
    
      //VARIABLES ECUACIONES
      double g; //gravedad
      double pe; //densidad cuerpo
      double pf; //densidad fluido
      double r; //radio
      double n; //viscosidad fluido
      double vl; //velocidad limite
    
      //OBJETOS EVENTOS
      TextField valorVarX, valorDismX;
      Button okButton;
    
      double posicionY, piepelota, posicionX, anchopelota, altopelota;
      int xRegla, yRegla1, yRegla2, longitudRegla;
      int partesGeneral, ypartes1, ypartes2, ypartes3, xpartes1, xpartes2, xnumeros;
      int ancho, alto; // para el buffer
      String textFieldX, textFieldDismX; //strings de textfield
      double x, dismX; //cociente de disminucion
      Thread hilo = null;
      Image buffer;
      Graphics pantallaVirtual;
    
      public void init() {
    	setLayout(new FlowLayout(FlowLayout.RIGHT));
    	vl = (2*g*(pe-pf)*(r*r)) / (9*n);
    	ancho = getBounds().width;
            alto = getBounds().height;
    	xRegla = 30;
    	yRegla1 = 15;
    	yRegla2 = alto - 15;
    	longitudRegla = yRegla2 - yRegla1;
    	partesGeneral =  longitudRegla / 3;
    	ypartes1 = yRegla1 + partesGeneral;
    	ypartes2 = yRegla1 + partesGeneral*2;
    	ypartes3 = yRegla1 + partesGeneral*3;
    	xpartes1 = xRegla - 5;
    	xpartes2 = xRegla +5;
    	xnumeros = xpartes1 - 25;
    	anchopelota = 15;
    	altopelota = 15;
    	posicionY = yRegla1;
    	posicionX = xRegla + 20;
    	piepelota = posicionY + altopelota;
    	buffer = createImage(ancho, alto);
    	pantallaVirtual = buffer.getGraphics();
    	valorVarX = new TextField("1", 4);
    	valorDismX = new TextField("0.001", 4);
    	okButton = new Button("Action!");
    	add(okButton);
    	add(valorVarX);
    	add(valorDismX);
    	okButton.addActionListener(this);
      }
    
      public void start() {
    	if (hilo == null) {
    			hilo = new Thread(this);
    			hilo.start();
    		}
      }
    
      public void stop() {
    	hilo = null;
      }
    
      public void paint(Graphics g) {
    	g.setColor(Color.black);
    	g.drawString("0.00", xnumeros, yRegla1);
    	g.drawString("0.01", xnumeros, ypartes1);
    	g.drawString("0.02", xnumeros, ypartes2);
    	g.drawString("0.03", xnumeros, ypartes3);
    	g.drawLine(xpartes1, yRegla1, xpartes2, yRegla1);
    	g.drawLine(xpartes1, ypartes1, xpartes2, ypartes1);
    	g.drawLine(xpartes1, ypartes2, xpartes2, ypartes2);
    	g.drawLine(xpartes1, ypartes3, xpartes2, ypartes3);
    	g.drawLine(xRegla, yRegla1, xRegla, yRegla2);
    	Graphics2D g2 = (Graphics2D) g;
    	g2.setPaint(Color.red);
    	g2.fill(new Ellipse2D.Double(posicionX,posicionY,anchopelota,altopelota));
    	
      }
    
      public void actionPerformed(ActionEvent evt) {
    	textFieldX = valorVarX.getText();
    	textFieldDismX = valorDismX.getText();
    	x = Double.parseDouble(textFieldX);
    	dismX = Double.parseDouble(textFieldDismX);
    	run();
      }
    
      public void update(Graphics g){
    	Color colorTemporal = pantallaVirtual.getColor();
    	pantallaVirtual.setColor(Color.white);
    	pantallaVirtual.fillRect(0,0,ancho,alto);
    	pantallaVirtual.setColor(colorTemporal);
    	paint(pantallaVirtual);
    	g.drawImage(buffer,0,0,this);
      }
    
      public void run() {
    	while (hilo!=null && hilo.isAlive() && (piepelota+x)<yRegla2 && x>0) {
    		x -= dismX;
    		if (x <= 0.01) {
    			x = 0.01;
    		}
    		posicionY +=x;
    		piepelota +=x;
    		repaint();
    		try {
    			hilo.sleep(10);
    		}
    		catch (InterruptedException e) {}
    	
    	}
    	
    	if ( (piepelota+x) > yRegla2 ) { 
    		x = yRegla2-piepelota;
    		piepelota +=x;
    		posicionY +=x;
    		repaint();
    	}
    	
    
    	if (piepelota==yRegla2) {
    	
    		repaint();
    		stop();
    	}
    	
    	if (x==0 && piepelota<yRegla2) {
    		repaint();
    	}
    	
      }
    }
    I'd like you to try it on the appletviewer or somewhere else...
    For some reason, when I initialize the applet, and I click the okButton, the actionPerformed method seems to work, because the text changes to red (so that means that it has used repaint(), as actionPerformed says), but the ball doesn't move (so it means the the run() method, specified in the actionPerformed method, does not get initialized). But wait!, when I minimize the appletviewer and then maximize it, the ball starts moving (the run() method gets started).
    What's happening here? why doesn't the run() method start when I click on the okButton?
    Thanks, and my apologies if I'm annoying with all that threads.

  2. #2
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    I may consider reading this if you translate everything written in dago [no speako dago] to English*.


    *Dialect of your choice (except hillbilly), but preferably English (GB).
    Last edited by Hibernate; 08-09-2011 at 08:56 PM.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  3. #3
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Java Code:
    /**
    *Applet CaidaBola
    *
    * <applet code= "CaidaBola.class" width = "400" height="400"></applet>
    */
    
    import java.applet.Applet;
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.Ellipse2D;
    
    
    public class CaidaBola extends Applet 
    	implements Runnable, ActionListener{
    
      //VARIABLES ECUACIONES
      double g; //gravedad
      double pe; //densidad cuerpo
      double pf; //densidad fluido
      double r; //radio
      double n; //viscosidad fluido
      double vl; //velocidad limite
    
      //OBJETOS EVENTOS
      TextField valorVarX, valorDismX;
      Button okButton;
    
      double posicionY, piepelota, posicionX, anchopelota, altopelota;
      int xRegla, yRegla1, yRegla2, longitudRegla;
      int partesGeneral, ypartes1, ypartes2, ypartes3, xpartes1, xpartes2, xnumeros;
      int ancho, alto; // para el buffer
      String textFieldX, textFieldDismX; //strings de textfield
      double x, dismX; //cociente de disminucion
      Thread hilo = null;
      Image buffer;
      Graphics pantallaVirtual;
    
      public void init() {
    	setLayout(new FlowLayout(FlowLayout.RIGHT));
    	vl = (2*g*(pe-pf)*(r*r)) / (9*n);
    	ancho = getBounds().width;
            alto = getBounds().height;
    	xRegla = 30;
    	yRegla1 = 15;
    	yRegla2 = alto - 15;
    	longitudRegla = yRegla2 - yRegla1;
    	partesGeneral =  longitudRegla / 3;
    	ypartes1 = yRegla1 + partesGeneral;
    	ypartes2 = yRegla1 + partesGeneral*2;
    	ypartes3 = yRegla1 + partesGeneral*3;
    	xpartes1 = xRegla - 5;
    	xpartes2 = xRegla +5;
    	xnumeros = xpartes1 - 25;
    	anchopelota = 15;
    	altopelota = 15;
    	posicionY = yRegla1;
    	posicionX = xRegla + 20;
    	piepelota = posicionY + altopelota;
    	buffer = createImage(ancho, alto);
    	pantallaVirtual = buffer.getGraphics();
    	valorVarX = new TextField("1", 4);
    	valorDismX = new TextField("0.001", 4);
    	okButton = new Button("Action!");
    	add(okButton);
    	add(valorVarX);
    	add(valorDismX);
    	okButton.addActionListener(this);
      }
    
      public void start() {
    	if (hilo == null) {
    			hilo = new Thread(this);
    		}
      }
    
      public void stop() {
    	hilo = null;
      }
    
      public void paint(Graphics g) {
    	g.setColor(Color.black);
    	g.drawString("0.00", xnumeros, yRegla1);
    	g.drawString("0.01", xnumeros, ypartes1);
    	g.drawString("0.02", xnumeros, ypartes2);
    	g.drawString("0.03", xnumeros, ypartes3);
    	g.drawLine(xpartes1, yRegla1, xpartes2, yRegla1);
    	g.drawLine(xpartes1, ypartes1, xpartes2, ypartes1);
    	g.drawLine(xpartes1, ypartes2, xpartes2, ypartes2);
    	g.drawLine(xpartes1, ypartes3, xpartes2, ypartes3);
    	g.drawLine(xRegla, yRegla1, xRegla, yRegla2);
    	Graphics2D g2 = (Graphics2D) g;
    	g2.setPaint(Color.red);
    	g2.fill(new Ellipse2D.Double(posicionX,posicionY,anchopelota,altopelota));
      }
    
      public void actionPerformed(ActionEvent evt) {
    	textFieldX = valorVarX.getText();
    	textFieldDismX = valorDismX.getText();
    	x = Double.parseDouble(textFieldX);
    	dismX = Double.parseDouble(textFieldDismX);
    	hilo.start();
      }
    
      public void update(Graphics g){
    	Color colorTemporal = pantallaVirtual.getColor();
    	pantallaVirtual.setColor(Color.white);
    	pantallaVirtual.fillRect(0,0,ancho,alto);
    	pantallaVirtual.setColor(colorTemporal);
    	paint(pantallaVirtual);
    	g.drawImage(buffer,0,0,this);
      }
    
      public void run() {
    	while (hilo!=null && hilo.isAlive() && (piepelota+x)<yRegla2 && x>0) {
    		x -= dismX;
    		if (x <= 0.01) {
    			x = 0.01;
    		}
    		posicionY +=x;
    		piepelota +=x;
    		repaint();
    		try {
    			hilo.sleep(10);
    		}
    		catch (InterruptedException e) {}
    	
    	}
    	
    	if ( (piepelota+x) > yRegla2 ) { 
    		x = yRegla2-piepelota;
    		piepelota +=x;
    		posicionY +=x;
    		repaint();
    	}
    	
    
    	if (piepelota==yRegla2) {
    	
    		repaint();
    		stop();
    	}
    	
    	if (x==0 && piepelota<yRegla2) {
    		repaint();
    	}
    	
      }
    }
    try this.
    public void start() <-- here you are overriding the start method from your applet and you will start the thread immediately.
    run(); <-- dont call the run method directly, the code in our start method was better. so create the thread in the start method of your applet, and call start() on the thread object in your actionperformed (see code above)

  4. #4
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by eRaaaa View Post
    try this.
    public void start() <-- here you are overriding the start method from your applet and you will start the thread immediately.
    run(); <-- dont call the run method directly, the code in our start method was better. so create the thread in the start method of your applet, and call start() on the thread object in your actionperformed (see code above)
    Great, it works perfectly! It was just logical :P . Thank you very much :)

    Quote Originally Posted by Hibernate View Post
    I may consider reading this if you translate everything written in dago [no speako dago] to English*.


    *Dialect of your choice (except hillbilly), but preferably English (GB).
    Hello (by the way). I just try to speak english the best that I can. If you think that I need a higher level to be in this forum, you are free to report me to admins.

  5. #5
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    No, I just did not understand the code's language. And since you seem to speak English well, it may benefit to upload the code in English, it is not too easy trying to understand code where all names are random.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

Similar Threads

  1. How to Maximize the JFrame ?
    By MarMer in forum AWT / Swing
    Replies: 5
    Last Post: 06-07-2011, 05:19 AM
  2. Disable Maximize button in JFrame?
    By Manish87 in forum AWT / Swing
    Replies: 0
    Last Post: 02-01-2011, 03:39 PM
  3. Replies: 6
    Last Post: 11-22-2010, 11:55 AM
  4. Want to Maximize your knowledge on Hibernate, Spring ?
    By fitce in forum Reviews / Advertising
    Replies: 2
    Last Post: 04-22-2010, 06:18 PM
  5. Minimize and maximize GUI
    By dejos456 in forum New To Java
    Replies: 6
    Last Post: 12-09-2009, 09:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •