Hola, he hecho el siguiente código con eclipse cuya función es la de mostrar aleatoriamente colores en cuartos de venta cada x segundos. Pero va tan rápido que los muestra todos a la vez. ¿Alguien podría ayudarme? (soy novato lo habréis notado por la penosa calidad del código)
import java.awt.AWTException; import java.awt.Color; import java.awt.Container; import java.awt.Graphics; import java.awt.Robot;
import javax.management.relation.Role; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import java.awt.*; import java.awt.event.*; import javax.swing.*;
public class quenivelquieres extends JFrame { private static final long serialVersionUID = 8585544783492126617L; public static quenivelquieres app; public static final int APP_WIDTH = 500; public static final int APP_HEIGHT = 500; private JMenuBar Barra; private JMenu Archivo; private JMenuItem salir; public static void main(String[] args) { app = new quenivelquieres (); app.show(); }
public void paint(Graphics gfx) { setLayout(null); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Barra = new JMenuBar(); Archivo = new JMenu ("Archivo"); Barra.add(Archivo); salir = new JMenuItem ("Salir"); Archivo.add(salir); salir.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent evento ){ System.exit( 0 ); } } ); this.setJMenuBar(Barra); setVisible(true); Container workArea = this.getContentPane(); Graphics workAreaGfx = workArea.getGraphics(); int z = 0; while (z<1000){ int x = (int) (Math.random()*10+1); if(x==5){x= x-1;} if(x==6){x= x-3;}//3 if(x==7){x= x-5;}//2 if(x==8){x= x-7;}//1 if(x==9){x= x-5;}//4 if(x==10){x= x-9;}//1 if(x==1){ workAreaGfx.setColor(Color.blue); int width = workArea.getWidth(); int height = workArea.getHeight(); workAreaGfx.fillRect(0,0, width/2, height/2);} if(x==2){ workAreaGfx.setColor(Color.red); int width = workArea.getWidth(); int height = workArea.getHeight(); workAreaGfx.fillRect(250,250, width,height);} if(x==3){ workAreaGfx.setColor(Color.yellow); int width = workArea.getWidth(); int height = workArea.getHeight(); workAreaGfx.fillRect(250,0, width/2, height/2);} if(x==4){ workAreaGfx.setColor(Color.green); int width = workArea.getWidth(); int height = workArea.getHeight(); workAreaGfx.fillRect(0,250, width/2, height/2);} z+=1; this.setSize(APP_WIDTH, APP_HEIGHT); this.setTitle("Dale al Azúl");
}
}
|