Question

Assignment Λ You shall write a Java program that accepts 5 command-line arguments and generates an image of a Sierpinski triaStarter Code Creating image files in Java is very easy, via the ⓗ BufferedImage and ⓗ ImageIO classes. Drawing on an image is

Assignment Λ You shall write a Java program that accepts 5 command-line arguments and generates an image of a Sierpinski triangle, as a 24- bit RGB PNG image file. Specifications The command-line arguments shall consist of the following 1. The width (in pixels) of the image, as a positive decimal integer 2. The height (in pixels) of the image, as a positive decimal integer 3. The minimum area (in pixels) that a triangle must have in order to be drawn, as a decimal floating-point value to double precision. 4. The foreground/drawing color, as a string acceptable to method Color.decode, as in the starter code below. 5. The output filename, i.e. a path to a file in which the output will be stored as a PNG image. You may assume that all command-line arguments are present and valid. The vertices for the outermost Sierpinski triangle shall be the middle of the top row, the bottom left corner, and the bottom right corner of the image. The background color of the image (i.e., all pixels not part of the Sierpinski triangle) shall be the naive complement (AKA negation, AKA inversion) of the foreground color. This can be computed by subtracting each of the RGB components from 255 (see Color.getRed et al).
Starter Code Creating image files in Java is very easy, via the ⓗ BufferedImage and ⓗ ImageIO classes. Drawing on an image is easily accomplished using the Graphics2D Object that a Bufferedimage object will provide, using ) Color objects to specify drawing and background colors The following code will generate a 320x240 32-bit RGB PNG image with yellow background and blue foreground, with two diagonal lines connecting opposite corners, in a file named sierpinski-demo.png. Feel free to start from this code SierpinskiStarterCode.java import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; * Class SierpinskistarterCode is a demo for this assignment. public class SierpinskiStarterCode [ k i * Writes a sample image to file "sierpinski-demo. png" *@param args unused public static void main(String[] args) f // A 320x240-pixel RGB image BufferedImage image new BufferedImage (320, 240, BufferedImage.TYPE INT RGB /Graphics2D is our "drawing" class Graphics2D graphics -image.createGraphics); Any clearing operations will use a yelLow color graphics. setBackground (Color" decode("#ffff00")); // Clear the image to the background color graphics.clearRect(8, Θ, İmage . getwidth(), İmage.getHeight()); //Any drawing operations will use a blue color graphics . SetColor (Color . decode("#0000ff" ) ); /Draw a line from the upper-Left corner (, e) to the Lower-right corner (width - 1, height 1): graphics.drawLínea, θ, image.getwidth() - 1, image . getHeight ( ) -1); /Draw a line from the Lower-Left corner (e, height - 1) to the upper-right corner (width - 1, e): graphics.drawlinea, image.getHeight() - 1, İmage.getwidth() - 1, θ); Save the image as a PNG file named "sierpinski-demo.png" try ImageIO.write (image, "png", new File("sierpinski-demo.png")) catch (Exception e) [ System.err.println("Unable to write image file!:"e.getMessage())
0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • code

--------------------------------

import java.awt.Color;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class SierpinskiStarterCode
{
public static void main(String args[])
{
int length = args.length; // getting the length of number of argumnet from command line
  
// checking that number of agruments is 5 or not , if 5 then proceed else print error message
if(length == 5 ) {
  
// getting the value and type-casting it in appropriate data type
int width = Integer.parseInt(args[0]);
int height = Integer.parseInt(args[1]);
double min_area = Double.parseDouble (args[2]);
String colour = args[3];
String filename = args[4];
  
  
// printing the received value
System.out.println(" Value received \n---------------------------- ");
System.out.println(" Width : "+width);
System.out.println(" height : "+height);
System.out.println(" Minimum Area : "+min_area);
System.out.println(" Colour code : "+colour);
System.out.println(" Output File name : "+filename);
  
System.out.println(" Generating Image...");
// BufferedImage image = new BufferedImage(320,240,BufferedImage.TYPE_INT_RGB);
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
// graphics.setBackground(Color.decode("#ffff00"));
graphics.setBackground(Color.decode(colour));
graphics.clearRect(0,0,image.getWidth()-1,image.getHeight()-1);
graphics.setColor(Color.decode(colour));
graphics.drawLine(0,image.getWidth()-1,image.getHeight()-1,0);
try
{
// ImageIO.write(image,"png",new File("sierpinski-demo.png"));
ImageIO.write(image,"png",new File(filename));
System.out.println(" Image generated successfully and saved with filename "+filename);
}
catch (Exception e)
{
System.err.println("Unable to write image file!:"+e.getMessage());
}
}
else
{
System.err.println(" You must have provide 5 values from command line \n---------------------------------------------------");
System.err.println(" usage : java <program name> <width> <height> <minimum area> <colour> <output file name>");
}
  
}
}

  • screenshot that shows the error message when no argument is given from command line

C windowslsystem32\cmd.exe : \Users\abhayk3x\Desktop>java SierpinskistarterCode You must have provide 5 values from command l

  • screenshot of the output when correct input is provided through command line

CAwindows system32\cmd.exe :\Users\abhayk3x\Desktop>java Value received SierpinskistarterCode 34.6 #fff000 myoutput .png 320

C windowslsystem32\cmd.exe : \Users\abhayk3x\Desktop>java SierpinskistarterCode You must have provide 5 values from command line usage : java <program name> <width> <height> <minimum area > <colour> <output file name > :Users\abhayk3x\Desktop>

CAwindows system32\cmd.exe :\Users\abhayk3x\Desktop>java Value received SierpinskistarterCode 34.6 #fff000 myoutput .png 320 240 : 320 : 240 : 34.6 : #fff000 Width height Minimum Area Colour code Output File name : myoutput.png Generating Image... Image generated successfully and saved with filename myoutput.png : \Users\abhayk3x\Desktop>

Add a comment
Know the answer?
Add Answer to:
Assignment Λ You shall write a Java program that accepts 5 command-line arguments and generates an image of a Sierpinski triangle, as a 24- bit RGB PNG image file. Specifications The command-line arg...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT