Advertisement
Sampywise

Painting App

Jul 10th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. import acm.graphics.*;
  2. import acm.util.*;
  3. import acm.program.*;
  4. import java.awt.*;
  5.  
  6.  
  7. public class Seurat extends GraphicsProgram{
  8.  
  9.     private static final int NUM_PICTURES = 4;
  10.     private static final int SPLOTCH_DIAMETER = 6;
  11.     private static final int WAIT_TIME = 1;
  12.     private RandomGenerator rgen = new RandomGenerator();
  13.    
  14.     public static void main(String[] args) {
  15.         new Seurat().start(args);
  16.     }
  17.    
  18.     public void run() {
  19.         setBackground(Color.black);
  20.         GImage image = new GImage(getRandomImage());
  21.         int imageLeft = (int) ((getWidth() - image.getWidth())/2);
  22.         int imageTop = (int)  ((getHeight() - image.getHeight())/2);
  23.        
  24.         while(true) {
  25.             int pixelX = rgen.nextInt((int) image.getWidth());
  26.             int pixelY = rgen.nextInt((int) image.getHeight());
  27.             Color pixelColor = getColorAt(image, pixelX, pixelY);
  28.             int xPos = imageLeft + pixelX;
  29.             int yPos = imageTop + pixelY;
  30.             drawSplotch(xPos, yPos, pixelColor);
  31.             pause(WAIT_TIME);
  32.            
  33.         }
  34.        
  35.     }
  36.    
  37.     private void drawSplotch(int xPos, int yPos, Color pixelColor) {
  38.         xPos = xPos - SPLOTCH_DIAMETER/2;
  39.         yPos = yPos - SPLOTCH_DIAMETER/2;
  40.         GOval circle = new GOval (xPos, yPos, SPLOTCH_DIAMETER, SPLOTCH_DIAMETER);
  41.         circle.setColor(pixelColor);
  42.         circle.setFilled(true);
  43.         add(circle);
  44.     }
  45.    
  46.     /**
  47.      *
  48.      * @param image
  49.      * @param x of a pixel
  50.      * @param y of a pixel
  51.      * @return the color found at a specific pixel
  52.      */
  53.     private Color getColorAt(GImage image, int x, int y) {
  54.         // feel free to ignore how the program is looking up the pixel color
  55.         // this syntax is not very nice and you don't need to understand it.
  56.         return new Color(image.getPixelArray()[y][x]);
  57.     }
  58.  
  59.     private String getRandomImage() {
  60.         switch(rgen.nextInt(NUM_PICTURES)) {
  61.         case 0: return "vangogh.jpg";
  62.         case 1: return "magritte.jpg";
  63.         case 2: return "seurat.jpg";
  64.         case 3: return "henson.jpg";
  65.         case 4: return "firefox.jpg";
  66.         default: return null;
  67.        
  68.         }
  69.     }
  70.    
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement