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");
}
}
Porfavor ayudarme :huh: que es para clase....
Thread.sleep(500);
Eso hace que el programa se duerma medio segundo antes de seguir.
vale, gracias pero me dice de añadirle; Surround with try/catch. y no se como funciona... ni donde ponerlo
la estructura del bloque try catch es:
try{
código añadido en la última edición
}catch (Exception e){
Lo que quieres que haga el programa si no funciona bien.
}
a que te refieres con añadido en la ultima edicion??
Cita de: DanFire en 17 Junio 2012, 21:40 PM
a que te refieres con añadido en la ultima edicion??
Cita de: ollo en 17 Junio 2012, 21:19 PM
la estructura del bloque try catch es:
try{
código añadido en la última edición
}catch (Exception e){
Lo que quieres que haga el programa si no funciona bien.
}
Con eclipse si utilizas las una de las ultimas versiones se que al ponerte encima de una función que puede enviar excepciones te aparece al poner el ratón encima la opción de insertarlo y te lo inserta de forma automática. Sino también puedes poner try(no le des al espacio y presiona Ctrl + Barra_espaciadora) y te lo rellena solo, solo tienes que introducir la función dentro del 'try' Como indica DanFire
Citar
a que te refieres con añadido en la ultima edicion??
Me refiero a la línea Thread.sleep(500);
aunque no estoy seguro de que te pidan eso exactamente
Te piden hacer JFrames cuando aún no sabes ni tratar excepciones?
try{
/** Codigo que puede contener un error */
}
catch(Exception e){
/** Lo que quieras que haga si encuentra la excepcion */
}
finally{
/** Lo que hara encuentre o no un error */
}
Ejemplo:
try{
Scanner f = new Scanner(new File(args[0]));
}
f.close();
}
catch(FileNotFoundException e){
System.err.println("Error al abrir el fichero");
}