Java 사각형 격자 그리기

1 개요[ | ]

Java 사각형 격자 그리기

2 예시 1[ | ]

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class MyClass {
  public static void main(String[] args) throws IOException {
    BufferedImage bufferedImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bufferedImage.createGraphics();
    g.fillRect(0, 0, 400, 400);
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        int x = i * 40;
        int y = j * 40;
        Color color = ((i + j) % 2 == 0) ? Color.BLUE : Color.GREEN;
        g.setColor(color);
        g.fillRect(x, y, 30, 30);
      }
    }
    g.dispose();
    ImageIO.write(bufferedImage, "png", new File("myimage.png"));
  }
}

3 예시 2[ | ]

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class MyClass {
  public static void main(String[] args) throws IOException {
    BufferedImage bufferedImage = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bufferedImage.createGraphics();
    g.fillRect(0, 0, 400, 400);
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        int x = i * 40;
        int y = j * 40;
        Color color = ((i + j) % 2 == 0) ? Color.WHITE : Color.BLACK;
        g.setColor(color);
        g.fillRect(x, y, 40, 40);
      }
    }
    g.dispose();
    ImageIO.write(bufferedImage, "png", new File("myimage.png"));
  }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}