Facebook
From Eratic Bird, 7 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 270
  1. import java.io.*;
  2. import java.awt.image.*;
  3. import javax.imageio.*;
  4. public class Zad1b
  5. {
  6.         public static void main(String[] args)
  7.         {
  8.                 System.out.println("Lattice pattern synthesis");
  9.  
  10.                 BufferedImage image;
  11.  
  12.                 // Image resolution
  13.                 int x_res, y_res;
  14.  
  15.                 // Predefined background and lattice color RGB representations
  16.                 // packed as integers
  17.                 int bgColor, lColor;
  18.                 // Loop variables - indices of the current row and column
  19.                 int i, j;
  20.                 //lattice width
  21.                 int w;
  22.                 //middle diameter length for x and y
  23.                 int d_x, d_y;
  24.                 //alternating colors RGB of lattice and background
  25.                 int lR, lG, lB, bR, bG, bB;
  26.                
  27.                 //a - horizontal, b - vertical translation parameter
  28.                 int a, b;
  29.                
  30.                 a = b = 0;
  31.                
  32.                 // Get required image resolution from command line arguments
  33.                 x_res = Integer.parseInt( args[0].trim() );
  34.                 y_res = Integer.parseInt( args[1].trim() );
  35.                 w = Integer.parseInt( args[2].trim() );
  36.                 d_x = Integer.parseInt( args[3].trim() );
  37.                 d_y = Integer.parseInt( args[4].trim() );
  38.                 lR = Integer.parseInt( args[5].trim() );
  39.                 lG = Integer.parseInt( args[6].trim() );
  40.                 lB = Integer.parseInt( args[7].trim() );
  41.                 bR = Integer.parseInt( args[8].trim() );
  42.                 bG = Integer.parseInt( args[9].trim() );
  43.                 bB = Integer.parseInt( args[10].trim() );
  44.                 // Initialize an empty image, use pixel format
  45.                 // with RGB packed in the integer data type
  46.                 image = new BufferedImage( x_res, y_res,
  47.                 BufferedImage.TYPE_INT_RGB);
  48.                 // Create packed RGB representation of black and white colors
  49.                 bgColor = int2RGB( lR, lG, lB );
  50.                 lColor = int2RGB( bR, bG, bB );
  51.                
  52.                 //checking correctness of arguments
  53.                 // Process the image, pixel by pixel
  54.                 if(d_x < w || d_y < w)
  55.                 {
  56.                         System.out.println("Distance between middle of a line can't be shorter that it's width.");
  57.                 }
  58.                 else
  59.                 {
  60.                         for ( i = 0; i < y_res; i++)
  61.                                 for ( j = 0; j < x_res; j++)
  62.                                 {
  63.                                         if(j % d_x < (d_x-w) && i % d_y < (d_y-w))
  64.                                                 image.setRGB( j+a, i+b, lColor );
  65.                                         else
  66.                                                 image.setRGB( j+a, i+b, bgColor);
  67.                                 }
  68.                 }
  69.                
  70.                 // Save the created image in a graphics file
  71.                 try
  72.                 {
  73.                         ImageIO.write( image, "bmp", new File( args[11]) );
  74.                         System.out.println( "Lattice image created successfully");
  75.                 }
  76.                 catch (IOException e)
  77.                 {
  78.                         System.out.println( "The image cannot be stored" );
  79.                 }
  80.         }
  81.  
  82.         // This method assembles RGB color intensities into single
  83.         // packed integer. Arguments must be in <0..255> range
  84.         static int int2RGB( int red, int green, int blue)
  85.         {
  86.                 // Make sure that color intensities are in 0..255 range
  87.                 red = red & 0x000000FF;
  88.                 green = green & 0x000000FF;
  89.                 blue = blue & 0x000000FF;
  90.  
  91.                 // Assemble packed RGB using bit shift operations
  92.                 return (red << 16) + (green << 8) + blue;
  93.         }
  94. }