/** Creates a PaintWindow with a picture bouncing left to right within the window. */ public void exercise1e() { PaintWindow pw = new PaintWindow(); Random rand = new Random(); ImageIcon image = new ImageIcon( "/Users/Kristoffer/Documents/Programering/Prog. Fördjupning/Inlämningsuppgifter/src/p1/gubbe.jpg"); int width = pw.getBackgroundWidth(); int height = pw.getBackgroundHeight(); // deltaX and deltaY int dx = -3; int dy = 0; // Start position in for the image int x = 250; int y = rand.nextInt(height - 100); // Bildens höjd är 100 // oändlig loop, raderna 36-42 upprepas tills PaintWindow-fönstret // stängs while (true) { pw.showImage(image, x, y); PaintWindow.pause(5); // pausa exekveringen i 20 ms innan nästa // förflyttning x += dx; y += dy; // Checks if x-pos is out of the bounds of the window // Using width - 100 takes the size of the image in consideration if (x < 0) { dx = -dx; } else if (x > width - 100) { dx = -dx; } } }
/** Creates a PaintWindow with a picture bouncing randomly around within the window. */ public void exercise1f() { PaintWindow pw = new PaintWindow(); Random rand = new Random(); ImageIcon image = new ImageIcon( "/Users/Kristoffer/Documents/Programering/Prog. Fördjupning/Inlämningsuppgifter/src/p1/gubbe.jpg"); int width = pw.getBackgroundWidth(); int height = pw.getBackgroundHeight(); // Delta X and Y are given a random number between -3(inclusive) and 3 // (inclusive) int dx = rand.nextInt(7) - 3; int dy = rand.nextInt(7) - 3; // Start position is randomly generated within the window int x = rand.nextInt(width - 100); int y = rand.nextInt(height - 100); // Bildens höjd är 100 // oändlig loop, raderna 36-42 upprepas tills PaintWindow-fönstret // stängs while (true) { pw.showImage(image, x, y); PaintWindow.pause(20); // pausa exekveringen i 20 ms innan nästa // förflyttning x += dx; y += dy; // Checks if x-pos is out of the bounds of the window // Using width - 100 takes the size of the image in consideration if (x < 0) { dx = -dx; } else if (x > width - 100) { dx = -dx; } // Checks if y-pos is out of the bounds of the window // Using height - 100 takes the size of the image in consideration if (y < 0) { dy = -dy; } else if (y > height - 100) { dy = -dy; } } }