public static void main(String[] args) { Scanner teclado = new Scanner(System.in); int tamaño = 0; int figuras = 0; Rectangle tablero; Ellipse pelota; Rectangle cuadrado; Line linea; Text texto; int enteros[]; enteros = new int[5]; System.out.println("Dime el tamaño del cuadrado : "); tamaño = teclado.nextInt(); System.out.println("Dime el número de figuras : "); figuras = teclado.nextInt(); tablero = new Rectangle(0, 0, tamaño, tamaño); tablero.draw(); switch (figuras) { case 1: pelota = new Ellipse(200, 200, 40, 40); pelota.fill(); break; case 2: pelota = new Ellipse(200, 200, 40, 40); pelota.fill(); cuadrado = new Rectangle(20, 20, 50, 11); cuadrado.fill(); break; case 3: pelota = new Ellipse(200, 200, 40, 40); pelota.fill(); cuadrado = new Rectangle(20, 20, 50, 11); cuadrado.fill(); linea = new Line(200, 25, 60, 30); linea.draw(); break; case 4: pelota = new Ellipse(200, 200, 40, 40); pelota.fill(); cuadrado = new Rectangle(20, 20, 50, 11); cuadrado.fill(); linea = new Line(200, 25, 60, 30); linea.draw(); texto = new Text(300, 400, "DAW"); texto.draw(); default: break; } }
public static void main(String[] args) throws InterruptedException { Ellipse pelota; Color color; int movimiento_x; int movimiento_y; int borde = 400; int diametro = 40; pelota = new Ellipse(200, 200, diametro, diametro); color = new Color( (int) (Math.random() * 256), // 256 para que dé de 0 a 255 colores (int) (Math.random() * 256), (int) (Math.random() * 256)); pelota.setColor(color); pelota.fill(); Rectangle tablero; tablero = new Rectangle(0, 0, borde, borde); tablero.draw(); tablero.setColor(color); // (int) (Math.random() * (max-min+1)) + min movimiento_x = (int) (Math.random() * 7) - 3; // (3-(-3 = 6 movimiento_y = (int) (Math.random() * 7) - 3; if (movimiento_x == 0) movimiento_x++; // (3-(-3 = 6 if (movimiento_y == 0) movimiento_y++; while (true) { // infinito pelota.translate(movimiento_x, movimiento_y); if (pelota.getX() > (borde - diametro - 1) || pelota.getX() < 0) movimiento_x = -movimiento_x; if (pelota.getY() > (borde - diametro - 1) || pelota.getY() < 0) movimiento_y = -movimiento_y; // if ((pelota.getX()+40)>400 || pelota.getX()<0) movimiento_x=-movimiento_x; Thread.sleep(1000 / 60); } }