Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- 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 Main {
- public static void generateImage(int [][] m, String fileName, int squareSize) throws IOException {
- BufferedImage bufferedImage;
- Graphics2D g2d;
- int width = m[0].length*squareSize;
- int height = m.length*squareSize;
- bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
- g2d = bufferedImage.createGraphics();
- for (int r=0; r<m.length; r++) {
- for (int c=0; c<m[0].length; c++) {
- g2d.setColor(int2color(m[r][c]));
- g2d.fillRect(c*squareSize, r*squareSize, squareSize, squareSize);
- }
- }
- g2d.dispose();
- File file = new File(fileName);
- ImageIO.write(bufferedImage, "png", file);
- }
- public static Color int2color(int value) {
- switch (value) {
- case 0: return Color.red;
- case 1: return Color.white;
- case 2: return Color.green;
- case 3: return Color.blue;
- default: return Color.gray;
- }
- }
- public static void main(String[] args) throws IOException {
- Scanner s = new Scanner (System.in);
- int higth;
- int x;
- int y;
- System.out.println("Quanto vuoi grande la tebella? Scrivi prima le colonne e poi le righe");
- System.out.println("Ricorda che deve essere un quadrato");
- x = s.nextInt();
- y = s.nextInt();
- System.out.println("Da che numero vuoi partire?");
- higth = s.nextInt();
- int [][] matrice = new int [x][y];
- matrice[x/2][y/2]=higth;
- do {
- if (higth > 3) {
- Distribute(matrice);
- }
- }
- while(!Controlla(matrice));
- decorations(matrice);
- String fileName = "my_image.png";
- generateImage(matrice, fileName, 30);
- }
- public static void decorations(int [][] grill) {
- for (int x = 0; x < grill [0].length; x++) {
- System.out.println();
- for (int y = 0; y < grill[1].length; y++) {
- System.out.format("%6d",grill[x][y]);
- }
- System.out.println();
- }
- }
- public static void Distribute(int [][] m) {
- for (int x = 0; x < m[0].length; x++) {
- for (int y = 0; y < m[1].length; y++) {
- do {
- if (m[x][y] > 3) {
- m[x][y] -= 4;
- m[x + 1][y] += 1;
- m[x - 1][y] += 1;
- m[x][y + 1] += 1;
- m[x][y - 1] += 1;
- }
- }
- while (m[x][y]>3);
- }
- }
- }
- public static boolean Controlla(int [][] m) {
- for (int x = 0; x < m[0].length; x++) {
- for (int y = 0; y < m[1].length; y++) {
- if (m[x][y]>3) {
- return false;
- }
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement