Assignment #1: Colortest
Your starting point is the JAVA code of the Colortest program presented in the last lecture.
Before you go on, please download the two classes that belong to that project and
put them in one folder, named 'Assignment1' OR just download the ZIP-file, unzip it: it will create the
required folder including the 2 sources.
Download Colortest.java
Download ColorPanel.java
Download ZIP file
Using NetBeans, or the console-compiler or whatever you want to use, compile the sources.
Run 'Colortest', this is the main class, containing the 'public static void main...'.
You should see this output:
Your task:
Change the program to show the following window:
What is this assignment about ?
Task A:
Obviously, you have to change the output of the program in a way, that the first row consists of 1 column only,
the second row of 2 columns and so on. Hence you have to understand the code to find the output loop to make the
change.
Task B
Now, since we only need 1 column in the first row, 2 in the second etc., it is a waste of memory to store
the whole color rectangle, including all the color-cells in the upper right triangle that are not shown. As memorized
in the lecture, JAVA 2D arrays (being arrays of arrays, remember ?) offer the possibility to define 2D arrays
having rows of different length, thus creating a datastructure that takes exactly as much memory as needed.
Therefore, part B of your task is:
change the definition and initialization of the array in a way, that IN THE DATASTRUCTURE ITSELF the first row
only contains one column, the second 2 etc.
Hints:
- This might be first time you see a code that contains graphic output. Hint #1: don't panic !
- There's a lot of code you don't have to understand. So where's the point ? You have to find the correct
places to make the required changes without totally understanding what's going on in the framework, you just
have to understand the basic construction of the program, not every single command.
- Although 'Colortest.java' is the main class, you don't have to understand or change anything in there. Just
leave it untouched. What does it do ? A brief overview (you neither have to read nor to understand): using the javax.swing.JFrame class ('swing' is the magic word and package
in JAVA when it comes to graphic output, we'll see later), it creates the actual window-frame on your screen. This
frame owns a space, the so called content pane, where classes can be put in, that show the actual graphic. One of these
classes that can be used to draw your own graphics is the javax.swing.JPanel class. We use exactly this class
to derive our own drawing space, therefore our class for the actual drawing is inherited from JPanel.
- All changes have to be made in the second class, 'ColorPanel.java'. This class is inherited from another swing class, JPanel.
JPanel, whatever it is or where it comes from (we'll learn this when we'll learn about swing), obviously offers a method where your
custom drawing can be made. This method is the 'protected void paintComponent(Graphics g)' method.
- Since i defined it in the ColorPanel class, it overrides the version being present in JPanel. JAVA automatically
calls this function whenever an update for the window is needed, i.e. when the window is created, moved, the size changes,
it is covered/uncovered etc.
- Even without understanding what's going on in swing behind the scenes, looking into the code and seeing the
two 'for' loops should give you the idea where the final drawing is done. This is obviously the place to change
the output to achieve the result for task A.
- Task B, changing the datastructure: the place to look for is... well, there's nothing else left than the
method 'private void initColorArray(int rows, int columns)', so this should be the way to go.
- Task B demands for 2 changes:
- change the definition of the array (here's the place where you change the
datastructure)
- change the loop that fills the array elements with color values
So you have to make two (by keystroke: minor, by their effect for memory usage: major) changes in there.
Scores:
Task A, the pure output change: 3 points.
Task B, changing the datastructure: 7 points
Bonus: 1 point for the bonus task (see below)
Bonus Task
Change the background of the upper right triangle to blue. This can be done in multiple ways, but the
easiest is a straightforward usage of the rectangle drawing command that is already used in the program.
If you a interested further ...
...even without bonus points:
You might want to play with the colors. The output colors are defined by their red, green and blue values.
Changing these values changes the output colors - e.g. try to add blue and see what happens (you should play with
the original version, not the modified triangle version. The full color rectangle looks much better).
GOOD LUCK !