Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.HashMap;
- import javax.imageio.ImageIO;
- public class C0173_Intermediate {
- private static final int SIZE = 100;
- private static final int STEPS = 1000000;
- private static HashMap<Color, Character> map = new HashMap<>();
- private static Color[] cols = new Color[] {Color.WHITE, Color.GREEN, Color.BLACK, Color.ORANGE};
- private static char[] turn = new char[] {'R', 'L', 'L', 'R'};
- private static BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
- private static Graphics2D g = image.createGraphics();
- public static void main(String[] args) {
- startGrid();
- initMap();
- drawCol(SIZE/2, SIZE/2, 0);
- saveToFile();
- }
- private static void initMap(){
- for(int i = 0; i < cols.length; i++) map.put(cols[i], turn[i]);
- }
- private static void drawCol(int x, int y, int dir){
- Color currPixel;
- for(int i = 0; i < STEPS; i++){
- if(x >= SIZE || y >= SIZE) break;
- currPixel = new Color(image.getRGB(x, y));
- if(map.get(currPixel) == 'L') dir = dir != 3 ? dir+1 : 0;
- else dir = dir != 0 ? dir-1 : 3;
- g.setColor(getNextColour(currPixel));
- g.drawLine(x, y, x, y);
- if(dir == 0) y++;
- else if(dir == 1) x++;
- else if(dir == 2) y--;
- else x--;
- }
- }
- private static Color getNextColour(Color currColour){
- for(int i = 0; i < cols.length; i++){
- if(cols[i].equals(currColour)){
- return i == cols.length-1 ? cols[0] : cols[i+1];
- }
- }
- return null;
- }
- private static void startGrid(){
- g.setColor(cols[0]);
- g.fillRect(0, 0, SIZE, SIZE);
- }
- private static void saveToFile(){
- try {
- ImageIO.write(image, "png", new File("ANTS.png"));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement