Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.util.HashMap;
  7. import javax.imageio.ImageIO;
  8.  
  9. public class C0173_Intermediate {
  10.  
  11.     private static final int SIZE = 100;
  12.     private static final int STEPS = 1000000;
  13.  
  14.     private static HashMap<Color, Character> map = new HashMap<>();
  15.     private static Color[] cols = new Color[] {Color.WHITE, Color.GREEN, Color.BLACK, Color.ORANGE};
  16.     private static char[] turn = new char[] {'R', 'L', 'L', 'R'};
  17.  
  18.     private static BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
  19.     private static Graphics2D g = image.createGraphics();
  20.  
  21.     public static void main(String[] args) {
  22.         startGrid();
  23.         initMap();
  24.         drawCol(SIZE/2, SIZE/2, 0);
  25.         saveToFile();
  26.     }
  27.  
  28.     private static void initMap(){
  29.         for(int i = 0; i < cols.length; i++)    map.put(cols[i], turn[i]);
  30.     }
  31.  
  32.     private static void drawCol(int x, int y, int dir){
  33.         Color currPixel;
  34.         for(int i = 0; i < STEPS; i++){
  35.             if(x >= SIZE || y >= SIZE)  break;
  36.             currPixel = new Color(image.getRGB(x, y));
  37.             if(map.get(currPixel) == 'L')   dir = dir != 3 ? dir+1 : 0;
  38.             else    dir = dir != 0 ? dir-1 : 3;
  39.             g.setColor(getNextColour(currPixel));
  40.             g.drawLine(x, y, x, y);
  41.  
  42.             if(dir == 0)    y++;
  43.             else if(dir == 1)   x++;
  44.             else if(dir == 2)   y--;
  45.             else x--;
  46.         }
  47.     }
  48.  
  49.     private static Color getNextColour(Color currColour){
  50.         for(int i = 0; i < cols.length; i++){
  51.             if(cols[i].equals(currColour)){
  52.                 return i == cols.length-1 ? cols[0] : cols[i+1];
  53.             }
  54.         }
  55.         return null;
  56.     }
  57.  
  58.     private static void startGrid(){
  59.         g.setColor(cols[0]);
  60.         g.fillRect(0, 0, SIZE, SIZE);
  61.     }
  62.  
  63.     private static void saveToFile(){
  64.         try {
  65.             ImageIO.write(image, "png", new File("ANTS.png"));
  66.         } catch (IOException e) {
  67.             e.printStackTrace();
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement