CS351 - Design of Large Programs - Professor Ackley

Tuesday and Thursday: 11:00am - 12:15pm (Centennial B146) Labs: Monday (001) 1:00pm - 1:50pm and Thursday (005) 12:30pm - 1:20pm (Centennial B146)

Lab #7: Runnable JARs & GUIs

Goals

  • Get back up to speed on javax.swing.JFrame and javax.swing.JPanel
  • Create a custom javax.swing.JPanel which draws command line argument shapes at random sizes
  • Create a runnable JAR

Needs


Background

The next project will have a heavy amount of GUI design and implementation. To get you back up to speed with the basics of the javax.swing.* packages, this lab was written for you to create a fixed size window (javax.swing.JFrame) which contains a custom extension of a javax.swing.JPanel that has shapes drawn ontop of it.


//TODO

You need to create a com.putable.labs.lab7.ShapePanel class which extends javax.swing.JPanel. This ShapePanel class will be added to the created javax.swing.JFrame that you see in the main() method that is given to you.

Whenever a JPanel (or any class which extends it) is added to a JFrame, the

protected void paintComponent(Graphics g)

method is called. Since we would like to draw given shapes on our ShapePanel, this method should be overridden (@Override) so it draws our own custom content on it.

The actual content that we would like our ShapePanel to contain depends on the args passed into the main method. Users can input any number of strings as arguments to the program as long as they are strings of the following set: "Square", "Circle" or "Triangle". For each time "Square" is passed as an element in the argument array, a Blue square of random size will be drawn on the ShapePanel (NOTE: random size must be such that it fits inside of the 500px X 500px, unmodifiable JFrame window). Each time "Circle" is passed as an element in the argument array, a Red circle (also of random size) is drawn. Same for "Triangle", except it is drawn using the color Green.

Since the sizes are completely random, the following outputs from a sample run with arguments "Circle Circle Square Triangle Square" produces the following 3 potential outputs:

Potential Javadoc for your method that does the work of using the java.awt.Graphics2D object to draw shapes on the ShapePanel, is given below:

/**
* Uses the {@code Graphics} object to draw each shape in the
* {@code shapesToDraw} {@code ArrayList}, exactly once, at a random
* size. More specifically, a Pseudo Random Number generator is used to
* choose a random {@code int} between 0 and {@code maxSize} (which is the
* maximum size of the {@code JFrame}). The value that the PRNG generates is
* either:
*
* The diameter, if drawing a {@link Shape.CIRCLE}
* The length of a side, if drawing a {@link Shape.SQUARE}
* The length of a side, if drawing an equilaterial {@link Shape.Triangle}
*
* Each {@link Shape} should be drawn such that it exists in the center of
* the window. That is, the center of each {@link Shape} is exactly
* {@code getWidth()/2} for the x-coordinate and {@code getHeight()/2} for
* the y-coordinate.
* All {@link Shape.CIRCLE}s should be {@code Color.RED}, all
* {@link Shape.SQUARE}s should be {@code Color.BLUE} and all
* {@link Shape.TRIANGLE}s should be {@code Color.GREEN}.
*
* NOTE: The height of an equilateral triangle is defined as
* {@code 1/2*sqrt(3)*l} where {@code l} is the length of the side.
*
* @param g
*     the {@code Graphics} object in which you will use to draw the
*     {@link Shape}s.
*/

You are more than welcome to use this exact Javadoc for a helper method which you write.


An “executable JAR” is nothing but a JAR in which the entry point of execution is defined.
To create an executable JAR from within Eclipse:

  1. Right click on the com.putable.labs.lab7 package -> Export...
  2. Under the "Java" folder, select "JAR file" (even though "Runnable JAR file" looks tempting...)
  3. Check the "Export generated class files and resources" and "Export Java source files and resources" boxes like normal
  4. Select the export destination
  5. Press "Next"
  6. Press "Next" again
  7. At the very bottom of this final window, use the "Browse..." button to navigate to your com.putable.labs.lab7.ShapePanel class and select it. This is what tells the MANIFEST.MF file which main() method to run, and essentially makes it a Runnable or "executable" JAR
  8. Test it by running on the command line: $ java -jar ShapeGUIRunnable.jar Square Triangle Square Circle Triangle


Turn in a ShapeGUIRunnable.jar file containing a single com.putable.labs.lab7.ShapePanel class. The main() method should live in this class. Make sure to turn in the java source files otherwise you will get a 0.

The ShapeGUIRunnable.jar should be turned in using the online interface by 11:59:59 PM of your lab day.