http://nameless.cis.udel.edu/class_wiki/index.php?title=CISC181_S2015_Lab3&feed=atom&action=historyCISC181 S2015 Lab3 - Revision history2024-03-28T09:19:32ZRevision history for this page on the wikiMediaWiki 1.28.0http://nameless.cis.udel.edu/class_wiki/index.php?title=CISC181_S2015_Lab3&diff=5&oldid=prevCer: Created page with "===Preliminaries=== This assignment uses the Swing library for basic drawing. You may want to refer to the documentation for the [http://docs.oracle.com/javase/7/docs/api/ja..."2017-01-18T15:07:52Z<p>Created page with "===Preliminaries=== This assignment uses the Swing library for basic drawing. You may want to refer to the documentation for the [http://docs.oracle.com/javase/7/docs/api/ja..."</p>
<p><b>New page</b></p><div>===Preliminaries===<br />
<br />
This assignment uses the Swing library for basic drawing. You may want to refer to the documentation for the [http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html <tt>JFrame</tt>], [http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html <tt>JComponent</tt>], [http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html <tt>Graphics</tt>], and [http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html <tt>Color</tt>] classes, among others.<br />
Just like last time:<br />
* Make a new project (but now ''n'' = 3) following [[CISC181_S2015_NewAndroidStudioProject | these ]] instructions. <br />
* Name your class "Lab3". This happens when you are creating a new module, in the ''Java class name'' field<br />
<br />
===Instructions===<br />
<br />
Modify <tt>Lab3.java</tt> as follows:<br />
<br />
* Add your name and section number in a comment before the Lab3 class body<br />
* Initialize a ''different'' <tt>JFrame</tt> and custom <tt>JComponent</tt> (as explained in the [https://docs.google.com/presentation/d/1_EBByb4ROZo0DYTZAlfIEoueHa4MXvphYeZPBFL6iMg/edit?usp=sharing Feb. 20/23 lecture slides]) for each of the two exercises below<br />
** Here is some [http://nameless.cis.udel.edu/class_wiki/index.php/CISC181_S2015_JFrameExample example code used in lecture]<br />
** Each custom <tt>JComponent</tt> will have its own <tt>paintComponent()</tt> method doing something different for ''each'' exercise below<br />
** Two JFrames initialized in main() means two windows should pop up each time you run the program<br />
<br />
====<tt>RandomJComponent</tt>====<br />
<br />
For this exercise you will create a colorful "frame" that could be used to surround a picture, as shown below. You will ''not'' be drawing an image in the middle of the frame for this lab.<br />
<br />
For all randomness, use the [http://docs.oracle.com/javase/7/docs/api/java/util/Random.html <tt>Random</tt>] class as necessary<br />
<br />
* The window should be initialized to 500 x 500, have the title "Random colors", and be drawn with a white background<br />
* Each circle should be a random diameter from 10-30 pixels<br />
* There should be 5,000 circles<br />
* No circle should be drawn if its upper-left corner lies in the middle 50% of the window (both vertically and horizontally)<br />
** Don't use the 500 value directly, but rather <tt>getWidth()</tt> and <tt>getHeight()</tt> for the current values, in case the window is resized<br />
* Every circle should be completely inside the window<br />
* The RGB color of each circle should be set as follows:<br />
** Red component: 0 at the top of the window, 255 at the bottom, and linearly scaled by ''y'' coordinate in between<br />
** Green, blue components: Each a different random number in the range [0, 255]<br />
<br />
[[Image:Random_window.png|300px]]<br><br />
<br />
Add your name and section number in a comment before the RandomJComponent class body<br />
<br />
====<tt>GraphJComponent</tt>====<br />
<br />
For this exercise you will draw a portion of a graph of the '''sine function''' (i.e., <tt>Math.sin()</tt>).<br />
<br />
Your graph should have the following characteristics:<br />
<br />
* The window will be 500 x 250, have the title "sin function", and have a white background<br />
* The plot should be '''scaled''' to more-or-less fill the window. As with the Feb. 23 discussion in lecture of the grid of squares, remember the difference between pixels and whatever mathematical units (radians, rows/cols, etc.) you are working with!<br />
** Again, don't use the 500 and 250 numbers directly for dimensions, but rather <tt>getWidth()</tt> and <tt>getHeight()</tt> in case the window is resized<br />
* The plot covers the range from 0 to 2 radians<br />
* Draw the curve in '''red''' as a series of connected line segments (use <tt>drawLine()</tt> in a loop). Make sure to use enough segments that the curve is somewhat smooth.<br />
* Draw a horizontal and vertical axis in '''gray''' as shown<br />
* Label the vertical axis with "-1" and "1" where appropriate<br />
* Label the horizontal axis with "0", "π", and "2π" where appropriate. "π" should be drawn as a Greek letter (the Unicode is '\u03c0')<br />
<br />
[[Image:Graph_window.png|400px]]<br><br />
<br />
Add your name and section number in a comment before the GraphJComponent class body.<br />
<br />
You may add more labels, tick marks, etc. to your plot as desired -- the above represents minimum requirements. Above and beyond would be adding the '''cosine function''' in blue.<br />
<br />
===Finishing up===<br />
<br />
Remember to use proper [https://google-styleguide.googlecode.com/svn/trunk/javaguide.html naming and formatting style] throughout your code, and submit your <tt>Lab3.java</tt>, <tt>RandomJComponent.java</tt>, and <tt>GraphJComponent.java</tt> on Sakai by '''Friday, February 27'''</div>Cer