I have a picture and I am trying to remove all the green pixels. How do I do this using simple java and a 2D array?
So far, my code looks like this:
public void removeGreen() {
Picture pic = new Picture("IMG_7320.JPG");
Pixel pixel = null;
for (int row = 0; row < pic.getHeight(); row++) {
for (int col = 0; col < pic.getWidth(); col++) {
pixel = getPixel(row,col);
pixel.getColor();
if(pixel.getRed() < 40 & pixel.getBlue() < 160 & pixel.getGreen() > 220) {
Color white = new Color(255,255,255);
pixel.setColor(white);
}
}
}
}
(Right now I am trying only replacing the green pixel with a white pixel because I'm not sure how to remove the pixel altogether.)
And the code in my main method that I am using to test the removeGreen() method,looks like this:
//method to test removeGreen
public static void testRemoveGreen() {
Picture me = new Picture("IMG_7320.JPG");
me.explore();
me.removeGreen();
me.explore();
}
So, my code now looks like this:
public void removeGreen(Picture pic) {
for (int row = 0; row < pic.getHeight(); row++) {
for (int col = 0; col < pic.getWidth(); col++) {
Pixel pixel = pic.getPixel(row,col);
if((pixel.getRed() < 40) && (pixel.getBlue() < 160) && (pixel.getGreen() > 220)) {
Color white = new Color(255,255,255);
pixel.setColor(white);
}
}
}
}
and my main method is still the same. I still do not understand why the method is not working properly.
The following makes clear that the passed pic parameter is altered.
public static void removeGreen(Picture pic) {
for (int row = 0; row < pic.getHeight(); row++) {
for (int col = 0; col < pic.getWidth(); col++) {
Pixel pixel = pic.getPixel(row,col);
if (pixel.getRed() < 40 && pixel.getBlue() < 160
&& pixel.getGreen() > 220) {
pixel.setColor(Color.white);
}
}
}
}
The && is a short-cut AND: x && y() will not call y is x is false.
Mind the pic.getPixel. If removeGreen is intended as method of Picture, remove the parameter and the static keyword, and pic..
public static void testRemoveGreen() {
Picture me = new Picture("IMG_7320.JPG");
me.explore();
removeGreen(me);
me.explore();
}
As the .jpg, JPEG, format does not have transparency, some colour like white is needed to be shown as "background."
Related
I am trying to export my sketch to pdf. The issue that I have is that for some reason only a portion of my sketch is exported to pdf, as if the original sketch was cropped! When I run my sketch, 64 lines show up (as intended) and indeed when I save it to png all the 64 lines are there and sketch looks just the same as when I run it.
When I export the sketch to pdf though, only 16 line show up, as if the pdf was cropping my sketch and only the cropped portion was being exported.
This is the png showing what the sketch is supposed to look like:
This is what the pdf has exported:
And this is my code:
import processing.pdf.*;
import java.util.Random;
int cols, rows;
int videoScale = 100;
boolean recordPDF = false;
void setup() {
size(800,800);
pixelDensity(2);
frameRate(0.5);
cols = width/videoScale;
rows = height/videoScale;
}
void draw() {
if (recordPDF) {
beginRecord(PDF, "pdfs/" + str(random(1000)) + ".pdf");
}
background(255);
strokeWeight(1.5);
drawLines();
if (recordPDF) {
endRecord();
recordPDF = false;
println("Printed pdf.");
}
}
void keyPressed() {
if (key == 'p') {
recordPDF = true;
}
if (key == 's') {
saveFrame("img.png");
}
}
void drawLines() {
// Begin loop for columns
for (int i = 0; i < cols; i++) {
// Begin loop for rows
for (int j = 0; j < rows; j++) {
int x = i*videoScale;
int y = j*videoScale;
line(x,y,x+30,y+30);
}
}
}
I have looked into the relevant documentation on PDF exports but could not find a solution to this. Any help would be greatly appreciated!
Remove pixelDensity(2) from the setup() to fix. PixelDensity of 2 was designed to allow retina monitors to use all of the pixels. If you must use it, then you would need to write a separate drawLines() for the pdf file (example follows). Note that for the pdf drawLines() the videoScale is cut in half and the second set of x,y coordinates for each line is +15 instead of +30. You will also have to change the path for each file saved to be correct for your system. I used a different method for creating a pdf from pGraphics, which should be irrelevant.
/*
If pixelDensity(2) is used, need to modify second drawLines() for pdf.
Change file paths to suit your system.
*/
import processing.pdf.*;
import java.util.Random;
int cols, rows;
int videoScale = 100;
void drawLines() {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
int x = i*videoScale;
int y = j*videoScale;
strokeWeight(1.5);
line(x, y, x+30, y+30);
}
}
}
void setup() {
size(800, 800);
background(255);
pixelDensity(2);
// frameRate(0.5);
cols = width/videoScale;
rows = height/videoScale;
drawLines();
}
void draw() {
}
void keyPressed() {
if (key == 'p') {
PGraphics pdf = createGraphics(width, height, PDF, "/Users/me/Desktop/output.pdf"); // change this
videoScale = 50; // cut in half
pdf.beginDraw();
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
int x = i*videoScale;
int y = j*videoScale;
pdf.line(x, y, x+15, y+15); // +15 instead of +30 for second x,y coordinates
}
}
pdf.dispose();
pdf.endDraw();
println("Printed pdf.");
}
if (key == 's') {
saveFrame("/Users/me/Desktop/img.png"); // change this
println("Image saved.");
}
}
This post is going to be really long, but I would be so glad if the community can help me.
This is a question from a former class test. I have to expect a similar one this time and I'm currently stuck. I'm new to Java and its concepts.
The result of the test should look like this:
The result
I'm facing multiple problems at once. I will try to be as specific as I can. First I will explain the task, which has given conditions that can't be changed.
The task
Create an array field with 2 dimensions. The field should work with various sizes. Besides the size, the array field should contain the following pattern(see picture).
1.) Create an constructor that creates a rectangle array field from the parameter passed in, with to extra conditions.
1.1) If the parameter is less than 5, the field should be forced to appear as a 5/5 field.
1.2) If the paramter is more than 5 it should always appear as an n/n array.
public class Pattern {
char[][] field;
public Pattern(int n) {
// Here goes my code for creating the field
}
2.) Write a method that fills the constructor.
public void fillArray() {
// Here goes my code.
}
My approach so far
public Pattern(int n) {
field = new char[n][n];
int i, j;
if (field.length < 5) {
for (i = 1; i <= 5; i++) {
for (j = 1; j <= 5; j++) {
// ? what to do with the array here?
}
System.out.println();
}
}
public void fillArray() {
// no fu**ing idea...
}
public static void main (String[]args){
Pattern cross = new Pattern(2);
cross.fillArray();
System.out.println(cross);
}
Questions
1.) What value should be returned in the constructor?
2.) How can I access the object in the method and set a for loop, that gets the predefined field size? Should I use this ?
I'm sorry for my bad understanding on how to pass array information and execute these correctly.
public class Pattern {
private final int size;
private final char[][] field;
public Pattern(int n) {
size = Math.max(n, 5);
field = new char[size][size];
}
public void fillArray() {
for (int row = 0, i = size - 1; row < size; row++, i--) {
for (int col = 0, j = size - 1; col < size; col++, j--) {
if (row == col || row == j)
field[row][col] = '*';
else if (col > row)
field[row][col] = col < i ? '1' : '2';
else
field[row][col] = col > i ? '3' : '4';
}
}
}
public void print() {
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++)
System.out.print(field[row][col] == '\0' ? ' ' : field[row][col]);
System.out.println();
}
}
public static void main(String... args) {
Pattern cross = new Pattern(2);
cross.fillArray();
cross.print();
}
}
You are putting what should be in fillArray in the constructor. The constructor should just be this:
public Pattern(int n) {
if (n < 5) {
field = new char[5][5];
} else {
field = new char[n][n];
}
}
And the two for loops should be in the fillArray method:
public void fillArray() {
// you should loop until field.length and field[x].length
for (int x = 0; x < field.length; x++) {
for (int y = 0; y < field[x].length; y++) {
// ...
}
}
}
Try to write your own logic of how to fill in the array.
If you are stuck, here is my solution:
for (int x = 0 ; x < field.length ; x++) {
for (int y = 0 ; y < field[x].length ; y++) {
/*
The '1' area can be represented by these inequalities:
x - y < 0
x + y < array.length - 1
The '2' area can be represented by these inequalities:
x - y < 0
x + y > array.length - 1
The '3' area can be represented by these inequalities:
x - y > 0
x + y > array.length - 1
The '4' area can be represented by these inequalities:
x - y > 0
x + y < array.length - 1
Everywhere that does not satisfy any of the inequalities are *s
*/
int xMinusY = x - y;
int xPlusY = x + y;
if (xMinusY < 0 && xPlusY < field.length - 1) {
field[x][y] = '1';
} else if (xMinusY < 0 && xPlusY > field.length - 1) {
field[x][y] = '2';
} else if (xMinusY > 0 && xPlusY > field.length - 1) {
field[x][y] = '3';
} else if (xMinusY > 0 && xPlusY < field.length - 1) {
field[x][y] = '4';
} else {
field[x][y] = '*';
}
}
}
// ...
// printing the array out:
for (int x = 0 ; x < field.length ; x++) {
for (int y = 0 ; y < field.length ; y++) {
System.out.print(field[x][y]);
}
System.out.println();
}
Answers to your questions:
Constructors does not return anything.
You don't need to use "this" since the 2-dimensional array is a field which can be accessed anywhere in the class Pattern. If you want to access the size of the array, you can use field.length.
Before you fill the array, you should first check if n is less than 5 (if it is, it should be set to 5). This is done in the constructor. Then you create the array.
After that, you have the for loops in which you have to figure out how to create the pattern. This should be done in the fillArray method.
Okay, so here is the solution. To get this kind of patterns draw it first on paper, then just write the columns and rows and search for the Pattern.
The only way you can learn this is by practice.
A constructor is like a method but does not have a return and uses the name of the Class.
The constructor will be always called when you instanciate a new Object of that class.
Example:
Pattern p = new Pattern(3);
Leave the constructor for the things you will need to do when you create an Object of that class. In your case decide the size of your Pattern.
Example:
public Pattern(int fS) { //fS = fieldSize
if (fS < 5)
fS = 5;
field = new char[fS][fS];
}
Then you have the methods, once an Object is created you can use all the methods from his Instance. In your case you will have a method called "fillArray".
Solution:
public void fillArray() {
int fS = field.length;
char c = ' ';
int len = fS - 1;
for (int row = 0; row < fS; row++)
for (int col = 0; col < fS; col++) {
if (row == col || row + col == len) c = '*';// This will make the asterisc cross
if (row < col && row < len - col) c = '1'; //This will do the 1 part
if (row < col && row > len - col) c = '2'; //This will do the 2 part
if (row > col && row > len - col) c = '3';//This will do the 3 part
if (row > col && row < len - col) c = '4';//This will do the 4 part
field[row][col] = c;
}
}
Remember:
In this case your Array will be not filled until you call the method fillArray()
Example:
Pattern p = new Pattern(7);
p.fillArray();
To print your Pattern I recommend you use the toString() method. You will need to Override it.
How to:
#Override //Important
public String toString() {
StringBuilder s = new StringBuilder();
for (char[] row : field) {
for (char col : row)
s.append("\t").append(col);
s.append("\n");
}
return s.toString();
}
Use the "#Override" tag to see if you did any typo writing the mehod toString. Now to print your pattern use:
Pattern p = new Pattern(7);
p.fillArray();
System.out.println(p); //This line
PD: Sorry if I did any grammar mistake my mother language is not english. Hope it helps.
I'm trying to calculate the edges of a black object within a .png file. And by that I mean find the column and row values that makeup a box that encapsulates an object. I attached a link to the photo I created which draws the box according to the values I find. As you can see the top, bottom, and right lines seem to line up correctly, but if you zoom in to the left line, part of the image is outside of the box. Why is that? I have an algorithm which I'll post below that searches every pixel in the array and finds the last occurrence of pixel values != 0 for the top, bottom, left and right sides. For some reason that extra image on the left side is registering pixels that are == 0... Are the values being rounded down to zero? If somebody could explain what is happening that would be great.
Here is a link to the image: http://i.imgur.com/UG8Cghe.png. You really have to zoom into the left side to see what I am talking about. Downloading the image and viewing is probably a necessity. It is a VERY small detail.
Here is the method that converts the BufferedImage(.png) to a 2D Array:
private static int[][] loadImageTo2D(String file_path)
{
img = null;
try { img = ImageIO.read(new File(file_path)); }
catch (IOException e) { System.out.println(e); }
int width = img.getWidth();
int height = img.getHeight();
int[][] pix = new int[height][width];
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
pix[row][col] = img.getRGB(col, row);
}//for
}//for
return pix;
}//loadImageTo2D
Here is how I am searching for the sides:
private static int[] getPerim(int[][] pix)
{
//Array holds object boundary edges.
int[] lines = new int[4];
lines[BOTTOM] = 0;
lines[TOP] = pix.length;
lines[LEFT] = pix[0].length;
lines[RIGHT] = 0;
//Top down iteration, find the first and last row and column of the
//actual graphic.
for (int row = 0; row < pix.length; row++)
{
for(int col = 0; col < pix[0].length; col++)
{
if (pix[row][col] != 0)
{
if (row < lines[TOP]) { lines[TOP] = row; }
else if (row > lines[BOTTOM]) { lines[BOTTOM] = row; }
else if (col < lines[LEFT]) { lines[LEFT] = col; }
else if (col > lines[RIGHT]) { lines[RIGHT] = col; }
}//if
}//for
}//for
return lines;
}//getPerim
I'm then using lines[] to draw the blue box you see in image. Help!
Drop the else part of if else and make all of them ifs . Only one of those if elses can be executed. What happens if pixel is the most down and most left pixel? It will be only used as the most bottom one, as the if-else statement won't get to the col part. I suggest you change it to
if (row < lines[TOP]) { lines[TOP] = row; }
if (row > lines[BOTTOM]) { lines[BOTTOM] = row; }
if (col < lines[LEFT]) { lines[LEFT] = col; }
if (col > lines[RIGHT]) { lines[RIGHT] = col; }
And no, you can't group left border with right border, as they can be on the same pixel.
I've been lurking and found heaps of great information form here, however the last few days I have been stuck and haven't been able to find help with my issue so I thought id post.
I have some homework and I have to make the contents of my array drop down to the bottom row. If i rotate the grid the items should still drop down to the bottom row and if i eat an object from the bottom row, everything above it in that column should drop down too.
Any help is greatly appreciated.
Here is a demo video of what should happen:
http://youtu.be/CB07vN-C_-Y
This is what i have so far:
`public class Assignment
{
// This method should return a *new copy* of
// the 2D cell matrix, with entries rotated clockwise
// The original matrix should not be changed
public static int[][] rotateClockwise(int[][] cells)
{
int w = cells.length;
int h = cells[0].length;
int[][] matrix = new int[h][w];
for (int i = 0; i < h; ++i)
{
for (int j = 0; j < w; ++j)
{
matrix[i][j] = cells[j][h - i - 1];
}
}
return matrix;
}
// This method should return a *new copy* of
// the 2D cell matrix, with entries rotated anti-clockwise
// The original matrix should not be changed
public static int[][] rotateAnticlockwise(int[][] cells)
{
int w = cells.length;
int h = cells[0].length;
int[][] matrix = new int[h][w];
for (int i = 0; i < h; ++i)
{
for (int j = 0; j < w; ++j)
{
matrix[i][j] = cells[w - j - 1][i];
}
}
return matrix;
}
// This method should return a *new copy* of the array, except
// that if there is a 0 that has a non-zero in the preceding
// slot in the array, then those two entries should be swapped
// See ProgrammingProject.pdf for an example
// The original array should not be changed
public static int[] dropOne(int[] column)
{
return column; // this will compile but gives the wrong result
}
}`
I'd model a column as a Queue<Icon> col = new LinkedList<Icon>(); there's an outline here for Queue<Segment> and a complete example here for Queue<Bauble>. You can peek() at the head (bottom) of the queue; if it's empty, you remove() a block from the column and add() it to the tail (top).
Addendum: You might start with this example, drop the getGray(), change the layout to new GridLayout(0, 1). Then, instead of shuffle(list), you'd cycle the queue.
for(int i = 0; i < arrayWidth; i++) {
boolean reachedZero = false;
for( int j = 0; j < arrayHeight; j++) {
if(array[i][j] == 1 && reachedZero == true) {
while( j >=0 && array[i][j - 1] == 0) {
array[i][j-1] = array[i][j];
array[i][j] = 0;
j--;
reachedZero = false;
}
j--; // Maybe an error here, it's late
if( array[i][j] == 0) {
reachedZero = true;
}
}
}
This was posted by a lovely redditor (RankWeis) from the /learnprogramming sub-reddit.
http://www.reddit.com/r/learnprogramming/comments/126597/java_help_needed_on_adding_a_gravity_effect_to/
The idea here is to create a grid of boxes. underneath the black grid is another grid of multi-colored boxes. when you click a box it's mask disappears showing the colored box beneath. You then click a second box if the colors match hurray, if not then the game continues. Here is the code.
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GuessingGame extends Applet{
/**
*
*/
private static final long serialVersionUID = 1L;
private final int START_X = 20;
private final int START_Y = 40;
private final int ROWS = 4;
private final int COLS = 4;
private final int BOX_WIDTH = 20;
private final int BOX_HEIGHT = 20;
//this is used to keep track of boxes that have been matched.
private boolean matchedBoxes[][];
//this is used to keep track of two boxes that have been clicked.
private MaskableBox chosenBoxes[];
private MaskableBox boxes[][];
private Color boxColors[][];
private Button resetButton;
public void init() {
boxes = new MaskableBox[ROWS][COLS];
boxColors = new Color[ROWS][COLS];
resetButton = new Button("Reset Colors");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
randomizeColors();
buildBoxes();
repaint();
}
});
add(resetButton);
//separate building colors so we can add a button later
//to re-randomize them.
randomizeColors();
buildBoxes();
}
public void paint(Graphics g) {
for (int row =0; row < boxes.length; row ++) {
for (int col = 0; col < boxes[row].length; col++) {
if(boxes[row][col].isClicked()) {
//boxes[row][col].setMaskColor(Color.black);
//boxes[row][col].setMask(!boxes[row][col].isMask());
//boxes[row][col].setClicked(false);
//}
if (!matchedBoxes[row][col]) {
gameLogic(boxes[row][col]);
//boxes[row][col].draw(g);
}
}
}
}
//loop through the boxes and draw them.
for (int row = 0; row < boxes.length; row++) {
for (int col = 0; col < boxes[row].length; col++) {
boxes[row][col].draw(g);
}
}
}
public void gameLogic(MaskableBox box) {
if ((chosenBoxes[0] != null)&&(chosenBoxes[1] != null)) {
if(chosenBoxes[0].getBackColor() == chosenBoxes[1].getBackColor()) {
for (int i=0; 0 < chosenBoxes.length; i++ ) {
for(int row = 0; row < boxes.length; row++) {
for(int col = 0; col < boxes[row].length; col++) {
if( boxes[row][col] == chosenBoxes[i] ) {
System.out.println("boxes [row][col] == chosenBoxes[] at index: " + i );
matchedBoxes[row][col] = true;
break;
}
}
}
}
}else {
chosenBoxes[0].setMask(true);
chosenBoxes[1].setMask(true);
}
chosenBoxes = new MaskableBox[2];
}else {
if (chosenBoxes[0] == null) {
chosenBoxes[0] = box;
chosenBoxes[0].setMask(false);
return;
}else{
if (chosenBoxes[1] == null) {
chosenBoxes[1] = box;
chosenBoxes[1].setMask(false);
}
}
}
}
private void removeMouseListeners() {
for(int row = 0; row < boxes.length; row ++) {
for(int col = 0; col < boxes[row].length; col++) {
removeMouseListener(boxes[row][col]);
}
}
}
private void buildBoxes() {
// need to clear any chosen boxes when building new array.
chosenBoxes = new MaskableBox[2];
// create a new matchedBoxes array
matchedBoxes = new boolean [ROWS][COLS];
removeMouseListeners();
for(int row = 0; row < boxes.length; row++) {
for(int col = 0; col < boxes[row].length; col++) {
boxes[row][col] =
new MaskableBox(START_X + col * BOX_WIDTH,
START_Y + row * BOX_HEIGHT,
BOX_WIDTH,
BOX_HEIGHT,
Color.gray,
boxColors[row][col],
true,
true,
this);
addMouseListener(boxes[row][col]);
}
}
}
private void randomizeColors() {
int[] chosenColors = {0,0,0,0,0,0,0,0};
Color[] availableColors = {Color.red, Color.blue, Color.green,
Color.yellow, Color.cyan, Color.magenta, Color.pink, Color.orange };
for(int row = 0; row < boxes.length; row++) {
for (int col = 0; col < boxes[row].length; col++) {
for (;;) {
int rnd = (int) (Math.random() * 8);
if (chosenColors[rnd]< 2) {
chosenColors[rnd]++;
boxColors[row][col] = availableColors[rnd];
break;
}
}
}
}
}
}
here is the second batch of code containing maskablebox
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
public class MaskableBox extends ClickableBox {
private boolean mask;
private Color maskColor;
Container parent;
public MaskableBox(int x, int y, int width, int height, Color borderColor,
Color backColor, boolean drawBorder, boolean mask, Container parent ) {
super(x, y, width, height, borderColor, backColor, drawBorder, parent);
this.parent = parent;
this.mask = mask;
}
public void draw(Graphics g) {
if(mask=false) {
super.draw(g);
// setOldColor(g.getColor());
// g.setColor(maskColor);
// g.fillRect(getX(),getY(),getWidth(), getHeight());
// if(isDrawBorder()) {
// g.setColor(getBorderColor());
// g.drawRect(getX(),getY(),getWidth(),getHeight());
// }
// g.setColor(getOldColor());
}else {
if(mask=true) {
//super.draw(g);
setOldColor(g.getColor());
g.setColor(maskColor);
g.fillRect(getX(),getY(),getWidth(), getHeight());
if(isDrawBorder()) {
g.setColor(getBorderColor());
g.drawRect(getX(),getY(),getWidth(),getHeight());
}
g.setColor(getOldColor());
}
}
}
public boolean isMask() {
return mask;
}
public void setMask(boolean mask) {
this.mask = mask;
}
public Color getMaskColor() {
return maskColor;
}
public void setMaskColor(Color maskColor) {
this.maskColor = maskColor;
}
}
I now get these error messages.
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at GuessingGame.gameLogic(GuessingGame.java:74)
at GuessingGame.paint(GuessingGame.java:55)
at java.awt.Container.update(Container.java:1801)
at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
at sun.awt.RepaintArea.paint(RepaintArea.java:216)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:306)
at java.awt.Component.dispatchEventImpl(Component.java:4706)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Look, the error message you see there, is called: "the stack trace"
It contains very helpful information on what the error was:
Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 2
at GuessingGame.gameLogic(GuessingGame.java:77) // This is where the error happened
at GuessingGame.paint(GuessingGame.java:55) // This is your code
at java.awt.Container.update(Container.java:1801) // not your code
at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239) // not your code
If you see it says: GuessingGame.java:77 that means the error occurred in the file GuessingGame.java in the line 77. The other files are not in your source code ( Container.java and Repaint.java ) so, they are not where the problem happened.
In GuessingGame.java at line 77, you attempted to access an index beyond the array bounds ( hence ArrayIndexOutOfBoundException ) furthermore, the number 2 it the index you tried to use.
With that information you can take a look at the source code and see that line 77 ( at least from the posted code ) is:
if( boxes[row][col] == chosenBoxes[i] ) {
So, boxes is not the problem ( its size is 4 it was declared asboxes = new MaskableBox[ROWS][COLS]; and both ROWS and COLS are declared as 4 ). So the responsible must be: chosenBoxes
It was initialized as:
chosenBoxes = new MaskableBox[2];
Size 2, that means the only valid indexes are 0 and 1. When you attempt to use index=2 the exception raised.
Now having this information in mind you may search for code that might cause it.
As other answers points the cause is
for (int i=0; 0 <= chosenBoxes.length; ++i ) {
Just 3 lines above!. It reads, "while zero is lower or equals than 2..." which will always return true, because 0 will always be < than 2.
So, you just have to fix that part and re-test.
I hope this explanation help you to solve this kind of problems in the future. It is very important to learn to read the stacktrace.
I can even imagine an acronym in the future: RTFST ;)
In your gameLogic method you have chosenBoxes which has two elements, but you loop to i <= chosenBoxes.length (which would loop i = 0, 1, 2). That's why you get the exception. Change it to i < chosenBoxes.length.
Edit
In fact, you have:
for (int i=0; 0 <= chosenBoxes.length; ++i ) {
change that to:
for (int i=0; i < chosenBoxes.length; i++ ) {
I'm not sure why you were prefix incrementing i in that loop, either.
Here:
for (int i=0; 0 <= chosenBoxes.length; ++i ) {
Your condition is always true ;)