Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import acm.graphics.*;
- import acm.util.*;
- import acm.program.*;
- import java.awt.*;
- public class Seurat extends GraphicsProgram{
- private static final int NUM_PICTURES = 4;
- private static final int SPLOTCH_DIAMETER = 6;
- private static final int WAIT_TIME = 1;
- private RandomGenerator rgen = new RandomGenerator();
- public static void main(String[] args) {
- new Seurat().start(args);
- }
- public void run() {
- setBackground(Color.black);
- GImage image = new GImage(getRandomImage());
- int imageLeft = (int) ((getWidth() - image.getWidth())/2);
- int imageTop = (int) ((getHeight() - image.getHeight())/2);
- while(true) {
- int pixelX = rgen.nextInt((int) image.getWidth());
- int pixelY = rgen.nextInt((int) image.getHeight());
- Color pixelColor = getColorAt(image, pixelX, pixelY);
- int xPos = imageLeft + pixelX;
- int yPos = imageTop + pixelY;
- drawSplotch(xPos, yPos, pixelColor);
- pause(WAIT_TIME);
- }
- }
- private void drawSplotch(int xPos, int yPos, Color pixelColor) {
- xPos = xPos - SPLOTCH_DIAMETER/2;
- yPos = yPos - SPLOTCH_DIAMETER/2;
- GOval circle = new GOval (xPos, yPos, SPLOTCH_DIAMETER, SPLOTCH_DIAMETER);
- circle.setColor(pixelColor);
- circle.setFilled(true);
- add(circle);
- }
- /**
- *
- * @param image
- * @param x of a pixel
- * @param y of a pixel
- * @return the color found at a specific pixel
- */
- private Color getColorAt(GImage image, int x, int y) {
- // feel free to ignore how the program is looking up the pixel color
- // this syntax is not very nice and you don't need to understand it.
- return new Color(image.getPixelArray()[y][x]);
- }
- private String getRandomImage() {
- switch(rgen.nextInt(NUM_PICTURES)) {
- case 0: return "vangogh.jpg";
- case 1: return "magritte.jpg";
- case 2: return "seurat.jpg";
- case 3: return "henson.jpg";
- case 4: return "firefox.jpg";
- default: return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement