I want to write a program for calculating the area covered on the screen by different windows. I have the number of windows and x1,y1,x2, y2 of each windows as input. I want to use fill function in java. I want to created a matrix the size of the desktop, full of zeroes. For every window, I want to take the width of the window and input that into the matrix, for as many rows as the window height and do this for all windows, and then summarised the resulting matrix by counting the ones, giving me the area covered on the screen, without having to deal with overlapping windows. but I don't know how could I use fill for matrix.
Suppose you have a 2-dim array like matrix[WIDTH][HEIGHT]
.
You can use the Arrays.fill()
method to fill a whole column with a single call. However, you still need to iterate over the columns.
I created a small program to illustrate. Hope it helps:
import java.util.*;
public class TestArray
{
public static final int SCREEN_WIDTH = 100;
public static final int SCREEN_HEIGHT = 100;
class WindowCoords
{
public int top;
public int left;
public int bottom;
public int right;
public WindowCoords(int top,
int left,
int bottom,
int right)
{
this.top = top;
this.left = left;
this.bottom = bottom;
this.right = right;
}
} // class WindowCoords
public List<WindowCoords> getWindows()
{
List<WindowCoords> result;
result = new ArrayList<WindowCoords>();
result.add(new WindowCoords(4, 67, 23, 89));
result.add(new WindowCoords(18, 12, 65, 30));
result.add(new WindowCoords(45, 3, 95, 15));
result.add(new WindowCoords(67, 40, 93, 59));
return (result);
}
public void run()
{
// Initialize matrix
// Setting its contents to 0 not strictly necessary though I prefer to do so
int[][] matrix;
int column;
matrix = new int[SCREEN_WIDTH][SCREEN_HEIGHT];
//for (column = 0; column < SCREEN_WIDTH; column++)
// Arrays.fill(matrix[column], 0);
// Get windows
List<WindowCoords> windows;
windows = getWindows();
// Fill covered screen
Iterator<WindowCoords> it;
WindowCoords window;
it = windows.iterator();
while (it.hasNext())
{
window = it.next();
for (column = window.left; column <= window.right; column++)
Arrays.fill(matrix[column], window.top, window.bottom, 1);
}
// Show result
int row;
for (row = 0; row < SCREEN_HEIGHT; row++)
{
for (column = 0; column < SCREEN_WIDTH; column++)
System.out.print(matrix[column][row]);
System.out.println();
}
} // run
public static void main(String[] args)
{
TestArray test;
test = new TestArray();
test.run();
}
} // class TestArray
Just a simple nested loop will do:
public static void main(String[] args) {
int[][] matrix = new int[10][10];
fill(matrix, 2, 2, 4, 4);
System.out.println(Arrays.deepToString(matrix));
}
private static void fill(int[][] matrix, int x1, int y1, int x2, int y2) {
for (int y = y1; y <= y2; y++) {
for (int x = x1; x <= x2; x++) {
matrix[y][x] = 1;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.