Not looping

Started by
2 comments, last by minibutmany 11 years, 3 months ago

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

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);
}
}
}
Advertisement

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 ;)

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

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.

My Games -- My Music 

Come join us for some friendly game dev discussions over in XNA Chat!

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()".

Stay gold, Pony Boy.

This topic is closed to new replies.

Advertisement