- import java.io.*;
- import java.awt.image.*;
- import javax.imageio.*;
- public class Zad1b
- {
- public static void main(String[] args)
- {
- System.out.println("Lattice pattern synthesis");
- BufferedImage image;
- // Image resolution
- int x_res, y_res;
- // Predefined background and lattice color RGB representations
- // packed as integers
- int bgColor, lColor;
- // Loop variables - indices of the current row and column
- int i, j;
- //lattice width
- int w;
- //middle diameter length for x and y
- int d_x, d_y;
- //alternating colors RGB of lattice and background
- int lR, lG, lB, bR, bG, bB;
- //a - horizontal, b - vertical translation parameter
- int a, b;
- a = b = 0;
- // Get required image resolution from command line arguments
- x_res = Integer.parseInt( args[0].trim() );
- y_res = Integer.parseInt( args[1].trim() );
- w = Integer.parseInt( args[2].trim() );
- d_x = Integer.parseInt( args[3].trim() );
- d_y = Integer.parseInt( args[4].trim() );
- lR = Integer.parseInt( args[5].trim() );
- lG = Integer.parseInt( args[6].trim() );
- lB = Integer.parseInt( args[7].trim() );
- bR = Integer.parseInt( args[8].trim() );
- bG = Integer.parseInt( args[9].trim() );
- bB = Integer.parseInt( args[10].trim() );
- // Initialize an empty image, use pixel format
- // with RGB packed in the integer data type
- image = new BufferedImage( x_res, y_res,
- BufferedImage.TYPE_INT_RGB);
- // Create packed RGB representation of black and white colors
- bgColor = int2RGB( lR, lG, lB );
- lColor = int2RGB( bR, bG, bB );
- //checking correctness of arguments
- // Process the image, pixel by pixel
- if(d_x < w || d_y < w)
- {
- System.out.println("Distance between middle of a line can't be shorter that it's width.");
- }
- else
- {
- for ( i = 0; i < y_res; i++)
- for ( j = 0; j < x_res; j++)
- {
- if(j % d_x < (d_x-w) && i % d_y < (d_y-w))
- image.setRGB( j+a, i+b, lColor );
- else
- image.setRGB( j+a, i+b, bgColor);
- }
- }
- // Save the created image in a graphics file
- try
- {
- ImageIO.write( image, "bmp", new File( args[11]) );
- System.out.println( "Lattice image created successfully");
- }
- catch (IOException e)
- {
- System.out.println( "The image cannot be stored" );
- }
- }
- // This method assembles RGB color intensities into single
- // packed integer. Arguments must be in <0..255> range
- static int int2RGB( int red, int green, int blue)
- {
- // Make sure that color intensities are in 0..255 range
- red = red & 0x000000FF;
- green = green & 0x000000FF;
- blue = blue & 0x000000FF;
- // Assemble packed RGB using bit shift operations
- return (red << 16) + (green << 8) + blue;
- }
- }