Advertisement
Guest User

Untitled

a guest
Dec 24th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.image.BufferedImage;
  4. import java.awt.image.RasterFormatException;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.HashMap;
  8. import javax.imageio.ImageIO;
  9.  
  10. public class LangtonsAnt {
  11.  
  12.     private static final int SIZE = 2000;
  13.     private static final int STEPS = 90000000;
  14.  
  15.     private static HashMap<Color, Character> map = new HashMap<>();
  16.     private static Color[] cols;
  17.     private static char[] turn;
  18.  
  19.     private static BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
  20.     private static Graphics2D g = image.createGraphics();
  21.  
  22.     public LangtonsAnt(){
  23.         initColours("LLRR");
  24.         startGrid();
  25.         initMap();
  26.         drawCol(SIZE/2, SIZE/2, 0);
  27.         saveToFile();
  28.     }
  29.  
  30.     public static void main(String[] args) {
  31.         new LangtonsAnt();
  32.     }
  33.  
  34.     private static void initColours(String turns){
  35.         turn = new char[turns.trim().length()];
  36.         cols = new Color[turns.trim().length()];
  37.         for(int i = 0; i < turns.length(); i++){
  38.             turn[i] = turns.charAt(i);
  39.             cols[i] = new java.awt.Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256));
  40.         }
  41.         cols[0] = Color.WHITE;
  42.     }
  43.  
  44.     private static void initMap(){
  45.         for(int i = 0; i < cols.length; i++)    map.put(cols[i], turn[i]);
  46.     }
  47.  
  48.     private static void drawBW(int x, int y, int dir){
  49.         for(int i = 0; i < STEPS; i++){
  50.             if(x >= SIZE || y >= SIZE || x <= 0 || y <= 0)  break;
  51.             if(new Color(image.getRGB(x, y)).equals(Color.WHITE)){
  52.                 dir = dir != 3 ? dir+1 : 0;
  53.                 g.setColor(Color.BLACK);
  54.                 g.drawLine(x, y, x, y);
  55.             } else{
  56.                 dir = dir != 0 ? dir-1 : 3;
  57.                 g.setColor(Color.WHITE);
  58.                 g.drawLine(x, y, x, y);
  59.             }
  60.             if(dir == 0)    y++;
  61.             else if(dir == 1)   x++;
  62.             else if(dir == 2)   y--;
  63.             else x--;
  64.         }
  65.     }
  66.  
  67.     private static void drawCol(int x, int y, int dir){
  68.         Color currPixel;
  69.         for(int i = 0; i < STEPS; i++){
  70.             if(x >= SIZE || y >= SIZE || x <= 0 || y <= 0)  break;
  71.             currPixel = new Color(image.getRGB(x, y));
  72.             dir = map.get(currPixel) == 'L' ? (dir != 3 ? dir+1 : 0) : (dir != 0 ? dir-1 : 3);
  73.             g.setColor(getNextColour(currPixel));
  74.             g.drawLine(x, y, x, y);
  75.  
  76.             if(dir == 0)    y++;
  77.             else if(dir == 1)   x++;
  78.             else if(dir == 2)   y--;
  79.             else x--;
  80.         }
  81.     }
  82.  
  83.     private static Color getNextColour(Color currColour){
  84.         for(int i = 0; i < cols.length; i++)
  85.             if(cols[i].equals(currColour))
  86.                 return i == cols.length-1 ? cols[0] : cols[i+1];
  87.  
  88.                 return null;
  89.     }
  90.  
  91.     private static void startGrid(){
  92.         g.setColor(cols[0]);
  93.         g.fillRect(0, 0, SIZE, SIZE);
  94.     }
  95.  
  96.     private static void saveToFile(){
  97.         try {
  98.             ImageIO.write(image, "png", new File("Ants.png"));
  99.         } catch (IOException e) {
  100.             e.printStackTrace();
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement