Graphics


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 random numbers from 1 to 500 to fill the array's values. 

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 "GENERATE". When the button is clicked, re-run runNums and use the repaint method to have the DrawBars update. (If the update is not working, then try barPanel.paintImmediately(0,0,barPanel.getWidth(),barPanel.getHeight()) instead of re-paint.)

[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.

Either have least one color, position, or size in your picture be randomized in some way, or have part of your picture change slightly depending on an instance var (probably a boolean) that gets changed at the end of each call to paintComponent.

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.

Add a button to your GUI that calls repaint for your MyPicture, which should cause it to re-draw the picture to see the result with differnt random values/ based on your instance var changing. (If the update is not working, then try picPanel.paintImmediately(0,0,picPanel.getWidth(),picPanel.getHeight()) instead of re-paint.)