Sign in to follow this  
Crusable77

Not looping

Recommended Posts

Crusable77    594

I am at school and i am just messing around and this loop will not loop:

 

[code] 

import java.util.Random;
import java.awt.*;
import javax.swing.JApplet;
 
public class Matrix extends JApplet{
 
   public void paint(Graphics g){
    
       Random rand = new Random();
  
       int width = getWidth();
       int height = getHeight();
        
       int num ;
       int posX;
       int posY;
       
       g.setColor(Color.black);
       g.fillRect(0,0,width,height);
        
       g.setColor(Color.GREEN);
        
       String matrix[] = {"a", "b", "c", "d","e","f","g","h","i","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"};
        
       
       for(;;){
           
           num = rand.nextInt(36);
           posX = rand.nextInt(width);
           posY = rand.nextInt(height);
           
           g.drawString(matrix[num], posX, posY);
        }
       
   }
}
[/code]

Share this post


Link to post
Share on other sites
BCullis    1955

Are you sure?

Try adding a printout to System.Out that prints the string you're trying to draw.  I'm willing to bet it's looping like mad, so much in fact that you never complete the paint method because you're stuck in the loop ;)

Share this post


Link to post
Share on other sites
FalconDragoon    485

I'm not versed in Java awt much, but I do believe you're supposed to call g.setFont before you can draw text.

 

Without seeing the context in which the paint method is called, it's hard to guess at what's going on. That for loop, however should run forever.

Share this post


Link to post
Share on other sites
minibutmany    1998

You should never put your game loop inside of your paint method, because it will never repaint the screen.

Make another method that calls the paint method ever time that loop loops using "repaint()".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this