Advertisement
Guest User

Distributoree

a guest
May 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3. import java.awt.Color;
  4. import java.awt.Graphics2D;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import javax.imageio.ImageIO;
  9. public class Main {
  10.  
  11.     public static void generateImage(int [][] m, String fileName, int squareSize) throws IOException {
  12.         BufferedImage bufferedImage;
  13.         Graphics2D g2d;
  14.  
  15.         int width = m[0].length*squareSize;
  16.         int height = m.length*squareSize;
  17.  
  18.         bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  19.         g2d = bufferedImage.createGraphics();
  20.  
  21.         for (int r=0; r<m.length; r++) {
  22.             for (int c=0; c<m[0].length; c++) {
  23.                 g2d.setColor(int2color(m[r][c]));
  24.                 g2d.fillRect(c*squareSize, r*squareSize, squareSize, squareSize);
  25.             }
  26.         }
  27.  
  28.         g2d.dispose();
  29.  
  30.         File file = new File(fileName);
  31.         ImageIO.write(bufferedImage, "png", file);
  32.  
  33.     }
  34.  
  35.     public static Color int2color(int value) {
  36.         switch (value) {
  37.             case 0: return Color.red;
  38.             case 1: return Color.white;
  39.             case 2: return Color.green;
  40.             case 3: return Color.blue;
  41.             default: return Color.gray;
  42.         }
  43.     }
  44.  
  45.     public static void main(String[] args) throws IOException {
  46.         Scanner s = new Scanner (System.in);
  47.         int higth;
  48.         int x;
  49.         int y;
  50.         System.out.println("Quanto vuoi grande la tebella? Scrivi prima le colonne e poi le righe");
  51.         System.out.println("Ricorda che deve essere un quadrato");
  52.         x = s.nextInt();
  53.         y = s.nextInt();
  54.         System.out.println("Da che numero vuoi partire?");
  55.         higth = s.nextInt();
  56.         int [][] matrice = new int [x][y];
  57.         matrice[x/2][y/2]=higth;
  58.  
  59.         do {
  60.             if (higth > 3) {
  61.                 Distribute(matrice);
  62.             }
  63.  
  64.         }
  65.         while(!Controlla(matrice));
  66.         decorations(matrice);
  67.  
  68.         String fileName = "my_image.png";
  69.         generateImage(matrice, fileName, 30);
  70.     }
  71.  
  72.     public static void decorations(int [][] grill) {
  73.         for (int x = 0; x < grill [0].length; x++) {
  74.             System.out.println();
  75.             for (int y = 0; y < grill[1].length; y++) {
  76.                 System.out.format("%6d",grill[x][y]);
  77.             }
  78.             System.out.println();
  79.         }
  80.     }
  81.     public static void Distribute(int [][] m) {
  82.         for (int x = 0; x < m[0].length; x++) {
  83.             for (int y = 0; y < m[1].length; y++) {
  84.                 do {
  85.                     if (m[x][y] > 3) {
  86.                         m[x][y] -= 4;
  87.                         m[x + 1][y] += 1;
  88.                         m[x - 1][y] += 1;
  89.                         m[x][y + 1] += 1;
  90.                         m[x][y - 1] += 1;
  91.                     }
  92.                 }
  93.                 while (m[x][y]>3);
  94.             }
  95.         }
  96.  
  97.     }
  98.     public static boolean Controlla(int [][] m) {
  99.         for (int x = 0; x < m[0].length; x++) {
  100.             for (int y = 0; y < m[1].length; y++) {
  101.                 if (m[x][y]>3) {
  102.                     return false;
  103.                 }
  104.             }
  105.         }
  106.         return true;
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement