/* PixelDrawing.java by Craig Persiko
Demonstrates drawing with a 2-dimensional array of integers
*/
class PixelDrawing
{
public static void main(String[] args)
{
int[][] pixels = new int[20][20];
int[] headerRow = new int[20];
// store the column numbers in the header row (used to label output)
for(int c=0; c < headerRow.length; c++)
headerRow[c] = c;
clearScreen(pixels);
for(int i=0; i<pixels.length; i++)
{
pixels[i][i] = 1; // put 1's in a line from top left to bottom right
pixels[i][pixels.length-1-i] = 2; // put 2's in a line from top right to bottom left
}
// display a header row:
System.out.print("Col# :\t");
outputRow(headerRow);
// display each row of the array
for(int r=0; r < pixels.length; r++)
{
System.out.print("\nRow " + r + ":\t");
outputRow(pixels[r]);
}
System.out.println();
}
// make every element of the 2-D array store 0.
static void clearScreen(int[][] p)
{
int x, y;
for (y=0; y < p.length; y++)
{
for (x=0; x < p[y].length; x++)
p[y][x] = 0;
}
}
// display all "size" elements of the 1-D array p.
static void outputRow(int[] p)
{
int x;
for (x=0; x < p.length; x++)
if(p[x] < 9) System.out.print(p[x] + " ");
else System.out.print(p[x]);
}
}
/* Output:
[cpersiko@fog cs111a]$ java PixelDrawing
Col# : 0 1 2 3 4 5 6 7 8 910111213141516171819
Row 0: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2
Row 1: 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0
Row 2: 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0
Row 3: 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0
Row 4: 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0
Row 5: 0 0 0 0 0 1 0 0 0 0 0 0 0 0 2 0 0 0 0 0
Row 6: 0 0 0 0 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0
Row 7: 0 0 0 0 0 0 0 1 0 0 0 0 2 0 0 0 0 0 0 0
Row 8: 0 0 0 0 0 0 0 0 1 0 0 2 0 0 0 0 0 0 0 0
Row 9: 0 0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