Guest User

Daily Programmer 174-Intermediate YuriKahn

a guest
Aug 6th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. package ava;
  2.  
  3. import java.awt.Color;
  4. import java.awt.image.BufferedImage;
  5. import java.io.BufferedReader;
  6. import java.io.File;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10. import java.util.Random;
  11.  
  12. import javax.imageio.ImageIO;
  13.  
  14. public class AvatarGenerator extends Thread {
  15.  
  16.     private ArrayList<String> userNames;
  17.     private long seed;
  18.    
  19.     public static final int AVATAR_XSIZE = 10;
  20.     public static final int AVATAR_YSIZE = 10;
  21.    
  22.     public AvatarGenerator(String s1, String seedString, boolean multipleFiles) {
  23.         userNames = new ArrayList<String>();
  24.         seed = djb2(seedString);
  25.         if(!multipleFiles) {
  26.             userNames.add(s1);
  27.             return;
  28.         }
  29.        
  30.         try{
  31.             BufferedReader br = new BufferedReader(new FileReader(s1));
  32.             String currentUserName = null;
  33.             while((currentUserName = br.readLine()) != null) {
  34.                 userNames.add(currentUserName);
  35.             }
  36.             br.close();
  37.         }catch(IOException e) {
  38.             System.out.println("Error: Could not read usernames file.");
  39.             System.exit(1);
  40.         }
  41.     }
  42.    
  43.     public void run() {
  44.         for(int i=0; i<userNames.size(); i++) {
  45.             String userName = userNames.get(i);
  46.             String outputFileName = userName + ".bmp";
  47.             Random random = new Random(seed + djb2(userName));
  48.             float H = Math.abs(random.nextFloat())%1f;
  49.             float S = (Math.abs(random.nextFloat())%0.5f)+0.5f;
  50.             float B = (Math.abs(random.nextFloat())%0.3f)+0.7f;
  51.             int forgroundColor = (new Color(50,50,50)).getRGB();
  52.            
  53.             int[][] avatar = new int[AVATAR_XSIZE][AVATAR_YSIZE];
  54.             for(int x=0; x<AVATAR_XSIZE/2; x++) {
  55.                 for(int y=0; y<AVATAR_YSIZE; y++) {
  56.                     boolean choice = random.nextBoolean();
  57.                     if(choice) {
  58.                         float mod1 = ((Math.abs(random.nextFloat()))%0.1f)-0.05f;
  59.                         avatar[x][y] = Color.HSBtoRGB(H+mod1, S, Math.min(Math.max(B+mod1,0.0f),1.0f));
  60.                         float mod2 = ((Math.abs(random.nextFloat()))%0.1f)-0.05f;
  61.                         avatar[AVATAR_XSIZE-x-1][y] = Color.HSBtoRGB(H+mod2, S, Math.min(Math.max(B+mod2,0.0f),1.0f));
  62.                     }else{
  63.                         avatar[x][y] = forgroundColor;
  64.                         avatar[AVATAR_XSIZE-x-1][y] = forgroundColor;
  65.                     }
  66.                 }
  67.             }
  68.             writeImage(outputFileName, avatar);
  69.         }
  70.     }
  71.    
  72.     private void writeImage(String outputFilename, int[][] avatar) {
  73.         File outputFile = new File(outputFilename);
  74.         int PIXELX = 5;
  75.         int PIXELY = 5;
  76.         BufferedImage theImage = new BufferedImage(AVATAR_XSIZE*PIXELX, AVATAR_YSIZE*PIXELY, BufferedImage.TYPE_INT_RGB);
  77.         for(int x=0; x<AVATAR_XSIZE; x++) {
  78.             for(int y=0; y<AVATAR_YSIZE; y++) {
  79.                 for(int px=0; px<PIXELX; px++) {
  80.                     for(int py=0; py<PIXELY; py++) {
  81.                         theImage.setRGB(PIXELX*x + px, PIXELY*y + py, avatar[x][y]);
  82.                     }
  83.                 }
  84.             }
  85.         }
  86.         try{
  87.             ImageIO.write(theImage, "BMP", outputFile);
  88.         }catch(IOException e) {
  89.             System.out.println("Error: Could not write file.");
  90.             System.exit(1);
  91.         }
  92.     }
  93.    
  94.     private long djb2(String stringToHash) {
  95.         long hash = 5381;
  96.         int c;  
  97.         for(int i=0; i<Math.max(stringToHash.length(),8); i++) {
  98.             c = stringToHash.charAt(i % stringToHash.length());
  99.             hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
  100.         }
  101.         return hash;
  102.     }
  103.    
  104.     public static void main(String[] args) {
  105.         if(args.length == 2) {
  106.             AvatarGenerator instance = new AvatarGenerator(args[0], args[1], false);
  107.             instance.start();
  108.         }else if(args.length == 3) {
  109.             if("-m".equals(args[2])) {
  110.                 AvatarGenerator instance = new AvatarGenerator(args[0], args[1], true);
  111.                 instance.start();
  112.                 return;
  113.             }else{
  114.                 System.out.println("Error: Unknown flag.");
  115.             }
  116.         }else{
  117.             System.out.println("Usage:\n\t$ AvatarGenerator <username> <seed>");
  118.             System.out.println("OR\n\t$ AvatarGenerator <file with usernames> <seed> -m");
  119.         }
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment