School Project, Array Index Out of Bounds Exception: 2 - java

I'm currently working on a project for school and am following a video tutorial, I'm pretty new to coding. From what I can tell everything looks right but when I run the preview it sends me a blank window with the error "ArrayIndexOutOfBoundsException:2"
PShape baseMap;
String csv[];
String myData[][];
//Setup BaseMap and csv info
void setup() {
size(1800, 900);
noLoop();
baseMap = loadShape("WorldMap.svg");
csv = loadStrings("FlightCancellations.csv");
myData = new String[csv.length][4];
for(int i=0; i<csv.length; i++) {
myData[i] = csv[i].split(",");
}
}
//draw
void draw() {
shape(baseMap, 0, 0, width, height);
noStroke();
fill(255, 0, 0, 50);
for(int i=0; i<myData.length; i++){
float graphLong = map(float(myData[i][2]), -180, 180, 0, width);
float graphLat = map(float(myData[i][3]), -90, 90, 0, height);
println(graphLong + " / " + graphLat);
ellipse(graphLong, graphLat, 10, 10);
}
}
Also, the mapped image works fine until I add
for(int i=0; i<myData.length; i++){
float graphLong = map(float(myData[i][2]), -180, 180, 0, width);
float graphLat = map(float(myData[i][3]), -90, 90, 0, height);
println(graphLong + " / " + graphLat);

You should get in the habit of checking that data exists before you use it in your program:
for(int i=0; i<myData.length; i++) {
if (myData[i].length > 3) { // Check that the array has at least 4 entries
float graphLong = map(float(myData[i][2]), -180, 180, 0, width);
float graphLat = map(float(myData[i][3]), -90, 90, 0, height);
println(graphLong + " / " + graphLat);
ellipse(graphLong, graphLat, 10, 10);
}
}

Related

How can I avoid keyPressed method to make my draw permenant in Processing 3?

I'm trying to create a HangMan game since it's an assignment. In the game() method, I want to call some methods from dead class if player's input is wrong. These methods creates HangMan. When I input a wrong letter and press Enter (increasing numWrong) related method is called but only flashes and disappears. I know it because of keyPressed method but how can I draw HangMan on screen and make it permanent till end of the game.
String[] words = {"dictionary","game","player","result"};
char[] strChar, guessed, wrong;
float x, y;
String str, display, display2, typing="", guess="", win = "", lost = "", wrongAnswers;
int rnd, c, numRight=0, winner=0, numWrong=0;
void setup() {
size(800, 800);
surface.setLocation(960, 0);
x = width;
y = height;
rnd = words.length-1;
str = words[(int)random(rnd)].toUpperCase();
strChar = new char[str.length()];
winner = str.length();
guessed = new char[str.length()];
for (int c=0; c<str.length(); c++) {
strChar[c] = str.charAt(c);
guessed[c] = '_';
}
wrong = new char[6];
for (int i=0; i<wrong.length; i++) {
wrong[i] = '*';
}
}
void draw() {
background(10);
display = "Write a letter then press ENTER.";
display2 = " ";
wrongAnswers = "Incorrect Guesses: ";
for (int d=0; d<guessed.length; d++) {
display2 = display2 +" "+guessed[d];
}
for (int i=0; i<wrong.length; i++) {
wrongAnswers = wrongAnswers+" "+wrong[i];
}
fill(255);
textSize(40);
text(display2, 40, 750);
textSize(20);
text(display, 450, 380);
textSize(250);
text(typing.toUpperCase(), 450, 245);
strokeWeight(1);
d.gallows();
fill(55, 200, 155);
textSize(50);
text(win, 110, 680);
textSize(50);
text(lost, 110, 680);
textSize(20);
text(wrongAnswers, 410, 690);
//origins
stroke(100, 150, 200);
strokeWeight(2);
line(0, 700, x, 700);//seperate line
line(x/2, 0, x/2, 700);//vertical line
line(x/2, y/2, x, y/2);//horizontal line
fill(255);
textSize(35);
text(str, 90, 560);
}
void game(String guess) {
guess = guess.toUpperCase();
char myGuess = guess.charAt(0);
boolean guessedRight = false;
for (int m=0; m<str.length(); m++) {
if (myGuess==str.charAt(m)) {
if (exist(display2, myGuess)) {
guessed[m] = myGuess;
numRight++;
guessedRight = true;
}
if (numRight == winner) {
win = "YOU WIN!!";
}
}
}
if (guessedRight == false) {
wrong[numWrong] = myGuess;
numWrong++;
noStroke();
fill(255);
if (numWrong==1) {
d.headAndRope();
}
if (numWrong==2) {
d.body();
d.headAndRope();
}
if (numWrong==3) {
d.leftArm();
d.body();
d.headAndRope();
}
if (numWrong==4) {
d.rightArm();
d.leftArm();
d.body();
d.headAndRope();
}
if (numWrong==5) {
d.leftLeg();
d.rightArm();
d.leftArm();
d.body();
d.headAndRope();
}
if (numWrong==6) {
d.rightLeg();
d.leftLeg();
d.rightArm();
d.leftArm();
d.body();
d.headAndRope();
}
if (numWrong == wrong.length) {
lost = "YOU LOSE!!";
}
}
}
void keyPressed() {
if (key == '\n') {
game(typing);
typing = "";
} else {
typing+=key;
}
}
boolean exist(String sng, char cha) {
for (int i=0; i<sng.length(); i++) {
if (sng.charAt(i)==cha) {
return false;
}
}
return true;
}
//DEAD CLASS
class Dead {
void gallows() {
stroke(0);
//base
fill(127);
rect(40, 600, 340, 30);
//left vertical rect
fill(200, 100, 50);
rect(60, 50, 10, 550);
//right vertical rect
fill(100, 50, 25);
rect(70, 50, 10, 550);
//top horizontal rect
fill(200, 100, 50);
rect(60, 40, 300, 10);
//bottom horizontal rect
fill(100, 50, 25);
rect(70, 50, 290, 10);
//diagonal bottom rect
fill(100, 50, 25);
beginShape();
vertex(80, 250);
vertex(80, 265);
vertex(265, 60);
vertex(250, 60);
endShape(CLOSE);
//diagonal top rect
fill(200, 100, 50);
beginShape();
vertex(70, 245);
vertex(70, 260);
vertex(260, 50);
vertex(245, 50);
endShape(CLOSE);
}
void headAndRope() {
//rope
fill(255);
rect(300, 40, 2, 95);
//head
fill(255);
ellipse(301, 155, 50, 75);
}
void body() {
//body
stroke(255);
strokeWeight(3);
line(301, 193, 301, 375);
}
void leftArm() {
//left arm
stroke(255);
strokeWeight(2);
line(301, 223, 251, 300);
}
void rightArm() {
//right arm
stroke(255);
strokeWeight(2);
line(301, 223, 351, 300);
}
void leftLeg() {
//left leg
stroke(255);
strokeWeight(2);
line(301, 375, 251, 450);
}
void rightLeg() {
//right leg
stroke(255);
strokeWeight(2);
line(301, 375, 351, 450);
}
}
In Processing the draw function gets called every frame, completely redrawing everything on the screen. So if you want the hangman to always appear on the screen, you'll need to draw it in the draw function (or in a function that gets called from draw).
If you restructure your code slightly everything should work:
void draw() {
// draw UI and input prompt based on `numRight` value
// NEW: draw the current state of the hangman based on `numWrong` value
}
void game(String guess) {
// update the game state (`numRight` and `numWrong` variables)
// based on `guess` input
// NEW: since this is only called momentarily, don't do any drawing here
}
void keyPressed() {
// receive key input and pass it to `game`
}
Since game only gets called momentarily when the user enters a guess, you don't want to be doing any drawing in there. It will get overwritten in the next draw cycle. You can still update the game state in game, but the drawing representation of that game state should happen from draw to ensure it gets drawn every time the screen updates.

Nested if statement within for loop [JAVA/LibGDX]

So I got my code to work this more of a question in regards to Java and why it worked the way I got it VS. why it didn't work the first way I wrote it. This is the original code I wrote.
private void renderGUIExtraLives (SpriteBatch batch){
float x = GUIcamera.viewportWidth - 50 - Constants.LIVES_START * 50;
float y = -15;
for (int i = 0; i < Constants.LIVES_START; i++) {
if (worldController.lives <= i) {
batch.setColor(0.5f, 0.5f, 0.5f, 0.5f);
batch.draw(Assets.instance.bunny.head, x + i * 50, y, 50, 50, 120, 100, 0.35f, -0.35f, 0);
batch.setColor(1, 1, 1, 1);
}
}
}
This didn't work, it threw no errors, but it did not draw the lives to the screen, all I did was remove the curly braces of the if statement like so:
private void renderGUIExtraLives (SpriteBatch batch){
float x = GUIcamera.viewportWidth - 50 - Constants.LIVES_START * 50;
float y = -15;
for (int i = 0; i < Constants.LIVES_START; i++) {
if (worldController.lives <= i)
batch.setColor(0.5f, 0.5f, 0.5f, 0.5f);
batch.draw(Assets.instance.bunny.head, x + i * 50, y, 50, 50, 120, 100, 0.35f, -0.35f, 0);
batch.setColor(1, 1, 1, 1);
}
}
And now magically it worked, can some explain why it worked after I removed the curly braces from the nested if statement? I would really appreciate it, as well as any discussion regarding this topic would be great to read if someone has a link to a similar question or answer here on Stack.
Your indentation is wrong and misleading. If you remove the braces after the if only the next statement is conditionally executed. That means the code basically is:
for (int i = 0; i < Constants.LIVES_START; i++) {
if (worldController.lives <= i)
batch.setColor(0.5f, 0.5f, 0.5f, 0.5f);
batch.draw(Assets.instance.bunny.head, x + i * 50, y, 50, 50, 120, 100, 0.35f, -0.35f, 0);
batch.setColor(1, 1, 1, 1);
}
Should be clear why it "magically works" now, it just draws unconditionally.

How to implement the main() method while using Processing in Java?

I tried to implement the following processing code in a JFrame in netbeans.
code was taken from http://www.geekmomprojects.com/mpu-6050-dmp-data-from-i2cdevlib/
However after running MyFrame.java the it only shows a still image in the frame.
It is not taking the serial readings.
MyProcessingSketch.java
`package testprocessing;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Arrays;
import processing.core.*;
import processing.serial.*;
import processing.opengl.*;
import toxi.geom.*;
import toxi.processing.*;
public class MyProcessingSketch extends PApplet {
ToxiclibsSupport gfx;
Serial port; // The serial port
char[] teapotPacket = new char[14]; // InvenSense Teapot packet
int serialCount = 0; // current packet byte position
int synced = 0;
int interval = 0;
float[] q = new float[4];
Quaternion quat = new Quaternion(1, 0, 0, 0);
float[] gravity = new float[3];
float[] euler = new float[3];
float[] ypr = new float[3];
#Override
public void setup() {
// 300px square viewport using OpenGL rendering
size(300, 300, OPENGL);
gfx = new ToxiclibsSupport(this);
// setup lights and antialiasing
lights();
smooth();
// display serial port list for debugging/clarity
System.out.println(Arrays.toString(Serial.list()));
// get the first available port (use EITHER this OR the specific port code below)
//String portName = Serial.list()[0];
// get a specific serial port (use EITHER this OR the first-available code above)
String portName = "COM10";
// open the serial port
port = new Serial(this, portName, 9600);
// send single character to trigger DMP init/start
// (expected by MPU6050_DMP6 example Arduino sketch)
port.write('r');
}
#Override
public void draw() {
if (millis() - interval > 1000) {
// resend single character to trigger DMP init/start
// in case the MPU is halted/reset while applet is running
port.write('r');
interval = millis();
}
// black background
background(0);
// translate everything to the middle of the viewport
pushMatrix();
translate(width / 2, height / 2);
// 3-step rotation from yaw/pitch/roll angles (gimbal lock!)
// ...and other weirdness I haven't figured out yet
//rotateY(-ypr[0]);
//rotateZ(-ypr[1]);
//rotateX(-ypr[2]);
// toxiclibs direct angle/axis rotation from quaternion (NO gimbal lock!)
// (axis order [1, 3, 2] and inversion [-1, +1, +1] is a consequence of
// different coordinate system orientation assumptions between Processing
// and InvenSense DMP)
float[] axis = quat.toAxisAngle();
rotate(axis[0], -axis[1], axis[3], axis[2]);
// draw main body in red
fill(255, 0, 0, 200);
box(10, 10, 200);
// draw front-facing tip in blue
fill(0, 0, 255, 200);
pushMatrix();
translate(0, 0, -120);
rotateX(PI/2);
drawCylinder(0, 20, 20, 8);
popMatrix();
// draw wings and tail fin in green
fill(0, 255, 0, 200);
beginShape(TRIANGLES);
vertex(-100, 2, 30); vertex(0, 2, -80); vertex(100, 2, 30); // wing top layer
vertex(-100, -2, 30); vertex(0, -2, -80); vertex(100, -2, 30); // wing bottom layer
vertex(-2, 0, 98); vertex(-2, -30, 98); vertex(-2, 0, 70); // tail left layer
vertex( 2, 0, 98); vertex( 2, -30, 98); vertex( 2, 0, 70); // tail right layer
endShape();
beginShape(QUADS);
vertex(-100, 2, 30); vertex(-100, -2, 30); vertex( 0, -2, -80); vertex( 0, 2, -80);
vertex( 100, 2, 30); vertex( 100, -2, 30); vertex( 0, -2, -80); vertex( 0, 2, -80);
vertex(-100, 2, 30); vertex(-100, -2, 30); vertex(100, -2, 30); vertex(100, 2, 30);
vertex(-2, 0, 98); vertex(2, 0, 98); vertex(2, -30, 98); vertex(-2, -30, 98);
vertex(-2, 0, 98); vertex(2, 0, 98); vertex(2, 0, 70); vertex(-2, 0, 70);
vertex(-2, -30, 98); vertex(2, -30, 98); vertex(2, 0, 70); vertex(-2, 0, 70);
endShape();
popMatrix();
}
void serialEvent(Serial port) {
interval = millis();
while (port.available() > 0) {
int ch = port.read();
if (synced == 0 && ch != '$') return; // initial synchronization - also used to resync/realign if needed
synced = 1;
print ((char)ch);
if ((serialCount == 1 && ch != 2)
|| (serialCount == 12 && ch != '\r')
|| (serialCount == 13 && ch != '\n')) {
serialCount = 0;
synced = 0;
return;
}
if (serialCount > 0 || ch == '$') {
teapotPacket[serialCount++] = (char)ch;
if (serialCount == 14) {
serialCount = 0; // restart packet byte position
// get quaternion from data packet
q[0] = ((teapotPacket[2] << 8) | teapotPacket[3]) / 16384.0f;
q[1] = ((teapotPacket[4] << 8) | teapotPacket[5]) / 16384.0f;
q[2] = ((teapotPacket[6] << 8) | teapotPacket[7]) / 16384.0f;
q[3] = ((teapotPacket[8] << 8) | teapotPacket[9]) / 16384.0f;
for (int i = 0; i < 4; i++) if (q[i] >= 2) q[i] = -4 + q[i];
// set our toxilibs quaternion to new data
quat.set(q[0], q[1], q[2], q[3]);
/*
// below calculations unnecessary for orientation only using toxilibs
// calculate gravity vector
gravity[0] = 2 * (q[1]*q[3] - q[0]*q[2]);
gravity[1] = 2 * (q[0]*q[1] + q[2]*q[3]);
gravity[2] = q[0]*q[0] - q[1]*q[1] - q[2]*q[2] + q[3]*q[3];
// calculate Euler angles
euler[0] = atan2(2*q[1]*q[2] - 2*q[0]*q[3], 2*q[0]*q[0] + 2*q[1]*q[1] - 1);
euler[1] = -asin(2*q[1]*q[3] + 2*q[0]*q[2]);
euler[2] = atan2(2*q[2]*q[3] - 2*q[0]*q[1], 2*q[0]*q[0] + 2*q[3]*q[3] - 1);
// calculate yaw/pitch/roll angles
ypr[0] = atan2(2*q[1]*q[2] - 2*q[0]*q[3], 2*q[0]*q[0] + 2*q[1]*q[1] - 1);
ypr[1] = atan(gravity[0] / sqrt(gravity[1]*gravity[1] + gravity[2]*gravity[2]));
ypr[2] = atan(gravity[1] / sqrt(gravity[0]*gravity[0] + gravity[2]*gravity[2]));
// output various components for debugging
//println("q:\t" + round(q[0]*100.0f)/100.0f + "\t" + round(q[1]*100.0f)/100.0f + "\t" + round(q[2]*100.0f)/100.0f + "\t" + round(q[3]*100.0f)/100.0f);
//println("euler:\t" + euler[0]*180.0f/PI + "\t" + euler[1]*180.0f/PI + "\t" + euler[2]*180.0f/PI);
//println("ypr:\t" + ypr[0]*180.0f/PI + "\t" + ypr[1]*180.0f/PI + "\t" + ypr[2]*180.0f/PI);
*/
}
}
}
}
void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
float angle = 0;
float angleIncrement = TWO_PI / sides;
beginShape(QUAD_STRIP);
for (int i = 0; i < sides + 1; ++i) {
vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
angle += angleIncrement;
}
endShape();
// If it is not a cone, draw the circular top cap
if (topRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, 0, 0);
for (int i = 0; i < sides + 1; i++) {
vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
// If it is not a cone, draw the circular bottom cap
if (bottomRadius != 0) {
angle = 0;
beginShape(TRIANGLE_FAN);
// Center point
vertex(0, tall, 0);
for (int i = 0; i < sides + 1; i++) {
vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
angle += angleIncrement;
}
endShape();
}
}
public static void main(String[] args) {
PApplet.main(new String[] { "--present", "MyProcessingSketch" });
}
}
`
MyFrame.java
package testprocessing;
import javax.swing.JFrame;
public class MyFrame extends JFrame{
private MyProcessingSketch mysketch;
public MyFrame() {
setTitle("IMU");
setDefaultCloseOperation(EXIT_ON_CLOSE);
mysketch = new MyProcessingSketch();
mysketch.init();
add(mysketch);
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.pack();
frame.setVisible(true);
}
}

Code Flutter (lines kind of jump and look bad)

So I have this little program (or sketch if you like). My problem is, and how should I word this, that the lines, when they are moving, kind of jitter and flutter a bit. Is this to do with the processing power of my computer, or should I code this in a different manner?
This is the code:
int i, j;
void setup() {
size(1440, 900);
background(0);
smooth();
strokeWeight(10);
i = width/2 - (width/2);
j = width;
}
void draw() {
fill(0, 10); // semi-transparent black
stroke(0);
rect(0, 0, width, height); //legger seg lag på lag
if (i < width-200) {
i+=4;
j-=4;
}
else {
i = width/2 - (width/2);
j = width;
}
stroke(255);
line(width/2, height, i, 30);
line(width/2, height, i+40, 30);
line(width/2, height, i+80, 30);
line(width/2, height, i+120, 30);
line(width/2, height, i+160, 30);
line(width/2, height, i+200, 30);
line(width/2, height, j, 30);
line(width/2, height, j-40, 30);
line(width/2, height, j-80, 30);
line(width/2, height, j-120, 30);
line(width/2, height, j-160, 30);
line(width/2, height, j-200, 30);
}
After doing a quick profile with JVisualVM it turns out that there are two culprits:
Rendering lines
Handling anti-aliased transparency
Rending lines:
Behind the scenes Processing is rendering each line as a shape(beginShape()/endShape()), in this case using LINES. You can give Processing a hand, and rather than using multiple beginShape/endShape calls(1 per line), just use one for all your lines:
beginShape(LINES);
for(int k = 0; k < 200; k+= 40){
vertex(hw, height);vertex(i+k, 30);
vertex(hw, height);vertex(j-k, 30);
}
endShape();
Anti-alias and transparency
Using transparency is generally computationally expensive, especially for large images.
Running the snippet bellow, press the mouse button and see how frameRate changes when transparency isn't used.
Anti-aliasing is also computationally expensive. Not as much as transparency, but in addition too, it makes a difference. Press any key to toggle between aliased and anti-aliased graphics
Here are a few tweaks to your code:
int i, j;
int hw;
boolean smooth;
void setup() {
size(1440, 900);
background(0);
strokeWeight(10);
hw = width/2;
i = width/2 - (width/2);//isn't this 0 ?
j = width;
}
void draw() {
fill(0,mousePressed ? 255 : 10); // semi-transparent black
noStroke();
rect(0, 0, width, height); //legger seg lag på lag
if (i < width-200) {
i+=4;
j-=4;
}
else {
i = 0;
j = width;
}
stroke(255);
beginShape(LINES);
for(int k = 0; k < 200; k+= 40){
vertex(hw, height);vertex(i+k, 30);
vertex(hw, height);vertex(j-k, 30);
}
endShape();
frame.setTitle((int)frameRate+" fps, smooth: " + smooth);
}
void keyReleased(){
smooth = !smooth;
if(smooth) smooth();
else noSmooth();
}

trying to create an array of bufferedimages

I'm trying to create an array of bufferedimages that contain cropped images of a bigger image but I keep getting this error:
Exception in thread "main" java.awt.image.RasterFormatException: (y + height) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(ByteInterleavedRaster.java:1233)
at java.awt.image.BufferedImage.getSubimage(BufferedImage.java:1156)
at com.search.visual.ImageChunks.createImageArrays(ImageChunks.java:95)
at com.search.visual.ImageChunks.calculateChunks(ImageChunks.java:122)
at com.search.visual.ImageChunks.<init>(ImageChunks.java:59)
at com.search.visual.Driver.main(Driver.java:10)
This is the code I'm using:
private BufferedImage[][] createImageArrays()
{
BufferedImage[][] bitmapsArray = new BufferedImage[3][3];
//bitmap bmap = bitmapfactory.decoderesource(getresources(), image);
//bufferedimage bmapscaled = bufferedimage.createscaledbitmap(bmp, 240, 240, true);
//Image imageMap = scaledImage;
bitmapsArray[0][0] = bmp.getSubimage(0,0, 80, 80);
bitmapsArray[0][1] = bmp.getSubimage(80,0, 80, 80);
bitmapsArray[0][2] = bmp.getSubimage( 160, 0, 80, 80);
bitmapsArray[1][0] = bmp.getSubimage( 0, 80, 80, 80);
bitmapsArray[1][1] = bmp.getSubimage( 80, 80, 80, 80);
bitmapsArray[1][2] = bmp.getSubimage( 160, 80, 80, 80);
bitmapsArray[2][0] = bmp.getSubimage( 0, 160, 80, 80);
bitmapsArray[2][1] = bmp.getSubimage( 80, 160, 80, 80);
bitmapsArray[2][2] = bmp.getSubimage( 160, 160, 80, 80);
return bitmapsArray;
}
/*
* 1. split the complete image/bitmaps into several smaller images/bitmaps
* 2. calculate the average RGB for each of these smaller images/bitmaps
* 3. fill up the RGB[][] rgbList
*
*/
private void calculateChunks()
{
// 1. split the complete image/bitmaps into several smaller images/bitmaps
// this is a real con, as I've only implemented it as 3x3 of of 80x80
//BufferedImage[][] bmpList = createImageArrays();
// 2. calculate the average RGB for each of these smaller images/bitmaps
BufferedImage[][] bmpList = createImageArrays();
System.out.print(bmpList);
RGB rgb_temp = null;
BufferedImage bmp_temp = null;
int rgb = 0;
int red = 0;
int green = 0;
int blue = 0;
int running_total_red = 0;
int running_total_blue = 0;
int running_total_green = 0;
RGB temp_rgb = null;
for(int I=0; I < 3; I++)
for(int j=0; j < 3; j++)
{
bmp_temp = bmpList[I][j];
red = 0;
green = 0;
blue = 0;
for(int k=0; k < 80; k++)
for(int l=0; l < 80; l++)
{
temp_rgb = getPixelRGBvalues( bmp_temp.getRGB(k, l) );
/* rgb = bmp_temp.getPixel(k, l); //gets the values of the pixel at point k and l
red += (rgb & 0x00ff0000) >> 16; //stores only the red values of rgb to red
green += (rgb & 0x0000ff00) >> 8; //stores only the green values of rgb to green
blue += rgb & 0x000000ff; //stores only the blue values of rgb to blue
*/
running_total_red += temp_rgb.getR();
running_total_blue += temp_rgb.getB();
running_total_green += temp_rgb.getG();
}
// normalise these values
red = running_total_red/ (80*80);
green = running_total_green/ (80*80);
blue = running_total_blue/ (80*80);
// 3. fill up the RGB[][] rgbList
rgbList[I][j] = new RGB(red, green, blue);
}
}
Why does this keep happening?
Your source image is smaller then 240*240 pixels. The subimages you are trying to acquire are out of the original image.

Categories