Graphics


Simple Shape Drawing

MyPicture.java

read chapter 13


Classwork


Create a class DrawBars that extends JPanel (you do not need the JPanel form for this) and has an instance variable that is an array of ints, size 20.  Add a method runNums which generates 1000 random numbers across the size of the array and uses the array to keep track of how often each number came up. 

In the paintComponent method for this class, draw a bar graph based on the values in the array so that each bar's height in pixels is the number at that position in the array.  Using the height and width of the panel distribute the width of the bars evenly, leaving a 5 pixel gap between bars, and have the lower ends of the bars level. 

Add a JFrame form to your project, and in the code, create a DrawBars instance variable and in the constructor, set it up and do runNums.

In the design view, add a JPanel (Panel). Right click on this and choose customize code, then change the Initialization code to custom creation. Set the JPanel variable to your DrawBars.

Also add a button labeled "STATISTICS". When the button is clicked, re-run runNums and use the repaint method to have the DrawBars update.

[EC]  Draw the numbers for the entries in the array below each of the bars (pretending we used human numbers) and the actual values (same as bar height) above the bars.

[EC]  Put a contrasting color outline around the bar for the highest value in the array.


ALTERNATIVE CLASSWORK

Create a class MyPicture that inherits from JPanel

In a paintComponent method, draw an HCC owl, Maryland crab, or other creature made out of ovals, rectangles, and lines.  You must use at least 10 shapes (not all have to be different) and at least 3 different colors.

Add a JFrame form to your project, and in it, add a JPanel (Panel).  Right click on it and choose customize code, then change the Initialization code to custom creation, and set the JPanel variable to a new instance of your MyPicture class.