![]() |
$64,000 question:
|
|
The picture (inside the frame) | Encoding (representation) of the picture |
---|---|
![]() Simplified encoding: 0 = black and 1 = yellow |
2-dimensional array: 0 1 2 3 +---+---+---+---+---+ ... 0 | 0 | 0 | 0 | 0 | 0 | ... +---+---+---+---+---+... 1 | 0 | 0 | 0 | 0 | 0 | ... +---+---+---+---+---+ ... .. .. .. .. .. ... +---+---+---+---+---+ ... 23 | 0 | 0 | 0 | 1 | 0 | ... +---+---+---+---+---+ ... 24 | 0 | 0 | 0 | 1 | 0 | ... +---+---+---+---+---+ ... 25 | 0 | 0 | 1 | 1 | 1 | ... +---+---+---+---+---+ ... .. .. .. .. .. ... |
(In reality, the value 1 should be 0xFFFF00 (Hex) (see: click here) or 16776960 (decimal))
|
|
static public void main(String[] args) throws Exception { /* ------------------------------------------------ Preparing to draw picture... ------------------------------------------------ */ MyCanvas pic = new MyCanvas(); // Make a canvas (2 dim array) Frame f = new Frame( "My image" ); // Create a window f.add("Center", pic); // Put the canvas in the window f.setSize(400,300); // We make a 400x300 picture f.setVisible(true); // Make window visible /* ---------------------------------------------------- Draw a picture stored in a 2-dimensional array ---------------------------------------------------- */ int[][] a = new int[400][300]; // This array hold the picture // You will need write statements here to store the // correct pixel codes into the variable a first.... pic.draw(a); // This method call will display the picture // stored in the variable a to the screen } |
import java.awt.*; import java.awt.image.*; public class Draw1 { static public void main(String[] args) throws Exception { final int YELLOW = 0xFFFF00; final int BLACK = 0x000000; /* ------------------------------------------------ Preparing to draw picture... ------------------------------------------------ */ MyCanvas pic = new MyCanvas(); // Make a canvas (2 dim array) Frame f = new Frame( "My image" ); // Create a window f.add("Center", pic); // Put the canvas in the window f.setSize(400,300); // Set size of the window f.setVisible(true); // Make window visible int[][] a = new int[400][300]; // This array hold the picture int row, col; /* ------------------------------- Make the whole picture BLACK ------------------------------- */ for (col = 0; col < 400; col++ ) for (row = 0; row < 300; row++ ) a[col][row] = BLACK; /* -------------------------------------------------- Make a vertical yellow line (col coord unchanged) -------------------------------------------------- */ a[100][191] = YELLOW; a[100][192] = YELLOW; a[100][193] = YELLOW; a[100][194] = YELLOW; a[100][195] = YELLOW; a[100][196] = YELLOW; a[100][197] = YELLOW; a[100][198] = YELLOW; a[100][199] = YELLOW; a[100][200] = YELLOW; a[100][201] = YELLOW; a[100][202] = YELLOW; a[100][203] = YELLOW; a[100][204] = YELLOW; a[100][205] = YELLOW; a[100][206] = YELLOW; a[100][207] = YELLOW; a[100][208] = YELLOW; a[100][209] = YELLOW; a[100][210] = YELLOW; a[100][211] = YELLOW; /* ----------------------------------------------------- Make a horizontal yellow line (row coord unchanged) ----------------------------------------------------- */ a[96][201] = YELLOW; a[95][201] = YELLOW; a[94][201] = YELLOW; a[99][201] = YELLOW; a[98][201] = YELLOW; a[97][201] = YELLOW; a[101][201] = YELLOW; a[102][201] = YELLOW; a[103][201] = YELLOW; a[104][201] = YELLOW; a[105][201] = YELLOW; a[106][201] = YELLOW; pic.draw(a); // Draw the picture in a[][] } } |
Original picture | Picture after moving to left |
---|---|
![]() |
![]() |
What really happens:
|
|
Explained with an illustration:
![]() |
Pixel value at (col, row): a[col][row] Pixel value to the right of (col,row): a[col+1][row] Statement to replace pixel value at (col, row) with pixel value to its right: a[col][row] = a[col+1][row]; |
What really happens with the representation of the picture is:
Value of the array representation before moving left | Value of the array representation after moving left |
---|---|
Array a: 0 1 2 3 +----+----+----+----+ ... 0 | 56 | 78 | 12 | 34 | ... +----+----+----+----+ ... 1 | 11 | 64 | 98 | 21 | ... +----+----+----+----+ ... 2 | 29 | 13 | 75 | 43 | ... +----+----+----+----+ ... .. .. .. .. ... |
Array a: 0 1 2 3 +----+----+----+----+ ... 0 | 78 | 12 | 34 | 23 | ... +----+----+----+----+ ... 1 | 64 | 98 | 21 | 87 | ... +----+----+----+----+ ... 2 | 13 | 75 | 43 | 99 | ... +----+----+----+----+ ... .. .. .. .. ... |
|
for column col = 0, 1, 2, ...., (lastColumn - 1) { fill column col with values in column (col+1); } fill column lastColumn with black pixel values; |
for columns (col = 0, 1, 2, ...., (lastColumn - 1) ) { for rows ( row = 0, 1, ..., lastRow ) do a[col][row] = a[col+1][row]; } for rows ( row = 0, 1, ..., lastRow ) do a[lastColumn][row] = 0; // 0 = code for black |
|
Java method:
/* =========================================================== The "move_left" method copies the "image" stored in the variable "pixel" one column to the left col col+1 . <-- . . <-- . . <-- . =========================================================== */ public static void move_left( int[][] pixel ) { int ncols, nrows; /* ------------------------------------------------ This nested for-loop copies all but one (the right most column) columns of the picture to the left (one column to the left) ------------------------------------------------ */ for ( int col = 0; col < 399; col++ ) for ( int row = 0; row < 300; row++ ) pixel[col][row] = pixel[col+1][row]; /* --------------------------------------------- Fill the right most column with BLACK pixels --------------------------------------------- */ for ( int row = 0; row < 300; row++ ) pixel[399][row] = 0; // Make column 399 into 0 = BLACK } |
Note:
|
static public void main(String[] args) throws Exception { final int YELLOW = 0xFFFF00; final int BLACK = 0x000000; /* ------------------------------------------------ Preparing to draw picture... ------------------------------------------------ */ MyCanvas pic = new MyCanvas(); // Make a canvas (2 dim array) Frame f = new Frame( "My image" ); // Create a window f.add("Center", pic); // Put the canvas in the window f.setSize(400,300); // Set size of the window f.setVisible(true); // Make window visible int[][] a = new int[400][300]; // This array hold the picture int row, col; /* ----------------------------------- Make every pixel into BLACK ----------------------------------- */ for (col = 0; col < 400; col++ ) for (row = 0; row < 300; row++ ) a[col][row] = BLACK; /* ----------------------------------- Make a YELLOW square (10x10) ----------------------------------- */ for (col = 100; col < 110; col++ ) for (row = 200; row < 210; row++ ) a[col][row] = YELLOW; /* ========================================== Move the picture to the left, 120 times ========================================== */ for ( int k = 0; k < 120; k++ ) { pic.draw(a); // Draw the picture in a[][] pause(1); // Pause 0.1 sec move_left(a); // Move the picture to left // Repeat these 3 steps for 120 times..... } System.exit(0); // Exit. } |
How to run the program:
|
Original picture | Picture after moving to right |
---|---|
![]() |
![]() |
|
Explained with an illustration:
![]() |
Pixel value at (col, row): a[col][row] Pixel value to the left of (col,row): a[col-1][row] Statement to replace pixel value at (col, row) with pixel value to its left: a[col][row] = a[col-1][row]; |
What really happens with the representation of the picture is:
Value of the array representation before moving right | Value of the array representation after moving right |
---|---|
Array a: 0 1 2 3 +----+----+----+----+ ... 0 | 56 | 78 | 12 | 34 | ... +----+----+----+----+ ... 1 | 11 | 64 | 98 | 21 | ... +----+----+----+----+ ... 2 | 29 | 13 | 75 | 43 | ... +----+----+----+----+ ... .. .. .. .. ... |
Array a: 0 1 2 3 +----+----+----+----+ ... 0 | 0 | 56 | 78 | 12 | ... +----+----+----+----+ ... 1 | 0 | 11 | 64 | 98 | ... +----+----+----+----+ ... 2 | 0 | 29 | 13 | 75 | ... +----+----+----+----+ ... .. .. .. .. ... |
|
for column col = 1, 2, ...., (lastColumn) { fill column col with values in column (col-1); } fill column 0 with black pixel values; |
for columns (col = 1, 2, ...., (lastColumn - 1) ) { for rows ( row = 0, 1, ..., lastRow ) do a[col][row] = a[col-1][row]; } for rows ( row = 0, 1, ..., lastRow ) do a[0][row] = 0; // 0 = code for black |
|
Java method ???? (this method does not work !)
/* =========================================================== The "move_right" method copies the "image" stored in the variable "pixel" one column to the right col-1 col . --> . . --> . . --> . =========================================================== */ public static void move_right( int[][] pixel ) { int ncols, nrows; /* ------------------------------------------------ This nested for-loop copies all but one (the right most column) columns of the picture to the right (one column to the right) ------------------------------------------------ */ for ( int col = 1; col < 400; col++ ) for ( int row = 0; row < 300; row++ ) pixel[col][row] = pixel[col-1][row]; /* --------------------------------------------- Fill the right most column with BLACK pixels --------------------------------------------- */ for ( int row = 0; row < 300; row++ ) pixel[0][row] = 0; // Make column 399 into 0 = BLACK } |
Quiz:
|
Let's execute the algorithm for a small example
Initial values | Iteration 1 - col=1: copy col 0 to col 1 |
---|---|
Array pixel: 0 1 2 3 +----+----+----+----+ 0 | 56 | 78 | 12 | 34 | +----+----+----+----+ 1 | 11 | 64 | 98 | 21 | +----+----+----+----+ 2 | 29 | 13 | 75 | 43 | +----+----+----+----+ |
Array pixel: 0 1 2 3 +----+----+----+----+ 0 | 56 | 56 | 12 | 34 | +----+----+----+----+ 1 | 11 | 11 | 98 | 21 | +----+----+----+----+ 2 | 29 | 29 | 75 | 43 | +----+----+----+----+ |
Iteration 2 - col=2: copy col 1 to col 2 | Iteration 3 - col=3: copy col 2 to col 3 |
Array pixel: 0 1 2 3 +----+----+----+----+ 0 | 56 | 56 | 56 | 34 | +----+----+----+----+ 1 | 11 | 11 | 11 | 21 | +----+----+----+----+ 2 | 29 | 29 | 29 | 43 | +----+----+----+----+ |
Array pixel: 0 1 2 3 +----+----+----+----+ 0 | 56 | 56 | 56 | 56 | +----+----+----+----+ 1 | 11 | 11 | 11 | 11 | +----+----+----+----+ 2 | 29 | 29 | 29 | 29 | +----+----+----+----+ |
Conclusion:
|
/* =========================================================== The "move_right" method copies the "image" stored in the variable "pixel" one column to the right col-1 col . --> . . --> . . --> . =========================================================== */ public static void move_right( int[][] pixel ) { int ncols, nrows; /* ------------------------------------------------ This nested for-loop copies all but one (the right most column) columns of the picture to the right (one column to the right) ------------------------------------------------ */ for ( int col = 399; col > 0; col-- ) for ( int row = 0; row < 300; row++ ) pixel[col][row] = pixel[col-1][row]; /* --------------------------------------------- Fill the right most column with BLACK pixels --------------------------------------------- */ for ( int row = 0; row < 300; row++ ) pixel[0][row] = 0; // Make column 399 into 0 = BLACK } |
Note:
|
static public void main(String[] args) throws Exception { final int YELLOW = 0xFFFF00; final int BLACK = 0x000000; /* ------------------------------------------------ Preparing to draw picture... ------------------------------------------------ */ MyCanvas pic = new MyCanvas(); // Make a canvas (2 dim array) Frame f = new Frame( "My image" ); // Create a window f.add("Center", pic); // Put the canvas in the window f.setSize(400,300); // Set size of the window f.setVisible(true); // Make window visible int[][] a = new int[400][300]; // This array hold the picture int row, col; /* ----------------------------------- Make every pixel into BLACK ----------------------------------- */ for (col = 0; col < 400; col++ ) for (row = 0; row < 300; row++ ) a[col][row] = BLACK; /* ----------------------------------- Make a YELLOW square (10x10) ----------------------------------- */ for (col = 100; col < 110; col++ ) for (row = 200; row < 210; row++ ) a[col][row] = YELLOW; /* ========================================== Move the picture to the right, 120 times ========================================== */ for ( int k = 0; k < 120; k++ ) { pic.draw(a); // Draw the picture in a[][] pause(1); // Pause 0.1 sec move_right(a); // Move the picture to right // Repeat these 3 steps for 120 times..... } System.exit(0); // Exit. } |
How to run the program:
|