I want to make bubble shooter game and I have problem with generate bubbles at start. When trying to compile program, I have error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException.
public class MyPanel extends JPanel {
Init init;
public MyPanel(){
super();
init = new Init();
}
public void paint(Graphics g){
Dimension size = getSize();
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.GRAY);
g2d.fillRect(0, 0, size.width, size.height - 70);
for(int j = 0; j < 10; j++)
for(int i = 0; i < 20; i++){
init.fields[i][j].b.paint(g); //here compiler shows error
}
}
}
public class Field {
private int x;
private int y;
private int r = 30;
public Baloon b;
public Field(int x, int y){
this.x = x*r;
this.y = y*r;
}
public void addBaloon(int n){
b = new Baloon(this.x, this.y, r, n);
}
}
public class Init {
Parser pr = new Parser();
private int r = pr.getRadius();
private int x = pr.getXDimension();
private int y = pr.getYDimension();
private int ni = pr.getColorRange();
Field[][] fields = new Field[x][y];
private int startX = 20;
private int startY = 10;
public Init(){
for(int yi = 1; yi<y; yi++){
for (int xi = 1; xi<x; xi++){
fields[xi][yi] = new Field(xi*r, yi*r);
}
}
for(int yi = 1; yi < startY; yi ++){
for(int xi = 1 ; xi < startX; xi++){
Random rand = new Random();
int n = rand.nextInt(ni);
fields[xi][yi].addBaloon(n);
}
}
}
}
You are initializing array from index 1:
for(int yi = 1; yi<y; yi++){
for (int xi = 1; xi<x; xi++){
fields[xi][yi] = new Field(xi*r, yi*r);
}
}
While accessing it from 0 like:
for(int j = 0; j < 10; j++)
for(int i = 0; i < 20; i++){
init.fields[i][j].b.paint(g); //here compiler shows error
}
Array index starts from 0 and goes upto n-1. So you need to initialize like:
for(int yi = 0; yi<y; yi++){
for (int xi = 0; xi<x; xi++){
fields[xi][yi] = new Field(xi*r, yi*r);
}
}
Related
I wrote a code for processing and had formerly sorted pixels with selection sort. I have to hand it in and the teacher said it is taking to long like this, so I decided to divide the pixels brightness into parts of 50 and just sort it very roughly. The image that comes out isn't completely sorted though and I really don't know where it went wrong.
I doesn't have to be sorted perfectly - it's really just about having a cool-looking image as a result.
I hope some can help me and it is understandable what I mean!
Thanks in advance
PImage img;
PImage two;
PImage sorted;
int j = 0;
int x = j;
int y = x;
int u = y;
int h = u;
int d = 1;
void setup() {
size(736,1051);
img = loadImage("guy.png");
two = loadImage("guy2.png");
background(two);
}
void draw() {
loadPixels();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int loc = x + y*width;
float r = red(img.pixels[loc]);
float g = green(img.pixels[loc]);
float b = blue(img.pixels[loc]);
float av = ((r+g+b)/3.0);
pixels[loc] = color(g,b,r, 17); //I know r, g, b are switched here
}
}
updatePixels();
save("guy_coloured.png");
}
void keyPressed(){
sorted = loadImage("guy_coloured.png");
sorted.loadPixels();
image(sorted, 0, 0);
System.out.print("doing it");
for (int i = 0; i < sorted.pixels.length; i++){
color colours = sorted.pixels[i];
float b = brightness(colours);
if(b<50){
sorted.pixels[j] = sorted.pixels[i];
j++;}
}
for (int f = 0; f < img.pixels.length; f++){
color colours = sorted.pixels[f];
float b = brightness(colours);
if(b<100 && b>50){
sorted.pixels[x] = sorted.pixels[f];
x++;}
}
for (int k = 0; k < img.pixels.length; k++){
color colours = sorted.pixels[k];
float b = brightness(colours);
if(b<150 && b>100){
sorted.pixels[y] = sorted.pixels[k];
y++;}
}
for (int t = 0; t < img.pixels.length; t++){
color colours = sorted.pixels[t];
float b = brightness(colours);
if(b<200 && b>150){
sorted.pixels[u] = sorted.pixels[t];
u++;}
}
for (int o = 0; o < img.pixels.length; o++){
color colours = sorted.pixels[o];
float b = brightness(colours);
if(b>200){
sorted.pixels[h] = sorted.pixels[o];
h++;}
}
System.out.print("done");
sorted.updatePixels();
image(sorted, 0, 0);
save("guy_sorted.png");
noLoop();
}
I want the whole image to be sorted, but it gives me back the normal image with about 1/4 sorted from the top.
This is the current result:
https://imgur.com/kHffIpm
Full code including irrelevant parts: https://docs.google.com/document/d/1YC97YMq9fKcbCAn3_RvLIm1bNo72FrNnHT3obc9pp7U/edit?usp=sharing
You do not sort the pixels. What you actually do is to arrange the dark pixel at the begin of the image and overwrite the pixels which are there. If you want to sort the pixels, then you've to swap them.
Write a function which can swap 2 pixel:
void Swap(PImage toSort, int i1, int i2) {
color c = toSort.pixels[i1];
toSort.pixels[i1] = toSort.pixels[i2];
toSort.pixels[i2] = c;
}
Once some pixels have been sorted, and are arranged at the begin of the image, this area doesn't need to be investigated further.
Write a function which sorts pixels dependent on a brightness range [b_min, b_max] and start at a certain index start:
int Sort(PImage toSort, int start, float b_min, float b_max) {
for (int i = start; i < toSort.pixels.length; i++) {
float b = brightness(toSort.pixels[i]);
if (b >= b_min && b < b_max) {
Swap(toSort, i, start);
start ++;
}
}
return start;
}
Sort the image by ascending brightness. e.g:
PImage img, two, sorted;
void setup() {
size(736,1051);
img = loadImage("guy.png");
two = loadImage("guy2.png");
background(two);
}
void draw() {
loadPixels();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int loc = x + y*width;
float r = red(img.pixels[loc]), g = green(img.pixels[loc]), b = blue(img.pixels[loc]);
pixels[loc] = color(g,b,r, 17); //I know r, g, b are switched here
}
}
updatePixels();
save("guy_coloured.png");
}
void Swap(PImage toSort, int i1, int i2) {
color c = toSort.pixels[i1];
toSort.pixels[i1] = toSort.pixels[i2];
toSort.pixels[i2] = c;
}
int Sort(PImage toSort, int start, float b_min, float b_max) {
for (int i = start; i < toSort.pixels.length; i++) {
float b = brightness(toSort.pixels[i]);
if (b >= b_min && b < b_max) {
Swap(toSort, i, start);
start ++;
}
}
return start;
}
void keyPressed(){
sorted = loadImage("guy_coloured.png");
sorted.loadPixels();
image(sorted, 0, 0);
System.out.print("doing it");
int j = 0;
j = Sort(sorted, j, 0.0, 50.0);
j = Sort(sorted, j, 0.50, 100.0);
j = Sort(sorted, j, 0.100, 150.0);
j = Sort(sorted, j, 0.150, 200.0);
j = Sort(sorted, j, 0.200, 256.0);
System.out.print("done");
sorted.updatePixels();
image(sorted, 0, 0);
save("guy_sorted.png");
noLoop();
}
Street Fighter 2d clone I'm trying to get the player Ken to come back down after jumping. He just goes higher and higher but I want it to be like gravity so he's pulled back down. Then I will set a ground level where he cant pass through.
I've researched the applyLinearImpulse method to be called on the body which is initialised beforre asa I kept getting null pointer exception it seems that now it don't crash but Ken just gets drawn further up the Y Axis. Its left me very confused.
Any advice links greatly appreciated.
Ken class
public class Ken extends Player {
private static final int FRAME_COLS = 6, FRAME_ROWS = 1;
private static final int COLUMNS_KICK = 6;
private static final int COLUMNS_LEFT = 8;
private static final int COLUMNS_RIGHT = 8;
private static final int COLUMNS_JUMP = 10;
private static final int COLUMNS_PUNCH = 6;
private static final int COLUMNS_FRONTFLIP = 8;
private static final int COLUMNS_BACKFLIP = 8;
public static final int FRAME_FRONTFLIP = 1;
public static final int FRAME_BACKLIP = 1;
float x, y;
Animation<TextureRegion> walkAnimation;
Animation<TextureRegion> kickAnimation;
Animation<TextureRegion> punchAnimation;
Animation<TextureRegion> leftAnimation;
Animation<TextureRegion> rightAnimation;
Animation<TextureRegion> jumpAnimation;
Animation<TextureRegion> frontFlipAnimation;
Animation<TextureRegion> backFlipAnimation;
Texture walkSheet;
Texture kickSheet;
Texture punchSheet;
Texture leftSheet;
Texture rightSheet;
Texture jumpSheet;
Texture frontFlipSheet;
Texture backFlipSheet;
public Body body;
public World world;
boolean alive = true;
private final static int STARTING_X = 50;
private final static int STARTING_Y = 30;
TextureRegion reg;
float stateTime;
public Ken(GameScreen screen){
this.world = screen.getWorld();
defineKen();
createIdleAnimation();
kickAnimation();
punchAnimation();
lefttAnimation();
righttAnimation();
jumpAnimation();
frontFlipAnimation();
backFlipAnimation();
this.setPosition(STARTING_X, STARTING_Y);
}
public void createIdleAnimation() {
walkSheet = new Texture(Gdx.files.internal("ken/idle.png"));
TextureRegion[][] tmp = TextureRegion.split(walkSheet,
walkSheet.getWidth() / FRAME_COLS,
walkSheet.getHeight() / FRAME_ROWS);
TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFrames[index++] = tmp[i][j];
}
}
walkAnimation = new Animation<TextureRegion>(0.1f, walkFrames);
stateTime = 0f;
reg=walkAnimation.getKeyFrame(0);
}
public void kickAnimation(){
kickSheet = new Texture(Gdx.files.internal("ken/kick_low.png"));
TextureRegion [][] tmp = TextureRegion.split(kickSheet, kickSheet.getWidth() / COLUMNS_KICK,
kickSheet.getHeight() / FRAME_ROWS);
TextureRegion[] kickFrames = new TextureRegion[COLUMNS_KICK * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
kickFrames[index++] = tmp[i][j];
}
}
kickAnimation = new Animation<TextureRegion>(8f, kickFrames);
stateTime = 6f;
reg = kickAnimation.getKeyFrame(1);
}
public void lefttAnimation(){
leftSheet = new Texture(Gdx.files.internal("ken/parry_b.png"));
TextureRegion [][] tmp = TextureRegion.split(leftSheet, leftSheet.getWidth() / COLUMNS_LEFT,
leftSheet.getHeight() / FRAME_ROWS);
TextureRegion[] leftFrames = new TextureRegion[COLUMNS_LEFT * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < COLUMNS_LEFT; j++) {
leftFrames[index++] = tmp[i][j];
}
}
leftAnimation = new Animation<TextureRegion>(0.1f, leftFrames);
stateTime = 0f;
reg = punchAnimation.getKeyFrame(0);
}
public void righttAnimation(){
rightSheet = new Texture(Gdx.files.internal("ken/parry_f.png"));
TextureRegion [][] tmp = TextureRegion.split(rightSheet, rightSheet.getWidth() / COLUMNS_RIGHT,
rightSheet.getHeight() / FRAME_ROWS);
TextureRegion[] rightFrames = new TextureRegion[COLUMNS_RIGHT * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < COLUMNS_RIGHT; j++) {
rightFrames[index++] = tmp[i][j];
}
}
rightAnimation = new Animation<TextureRegion>(0.1f, rightFrames);
stateTime = 0f;
reg = rightAnimation.getKeyFrame(0);
}
public void punchAnimation(){
punchSheet = new Texture(Gdx.files.internal("ken/punch.png"));
TextureRegion [][] tmp = TextureRegion.split(punchSheet, punchSheet.getWidth() / COLUMNS_PUNCH,
punchSheet.getHeight() / FRAME_ROWS);
TextureRegion[] punchFrames = new TextureRegion[COLUMNS_PUNCH * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < COLUMNS_PUNCH; j++) {
punchFrames[index++] = tmp[i][j];
}
}
punchAnimation = new Animation<TextureRegion>(0.1f, punchFrames);
stateTime = 0f;
reg = punchAnimation.getKeyFrame(0);
}
public void jumpAnimation(){
jumpSheet = new Texture(Gdx.files.internal("ken/jump.png"));
TextureRegion [][] tmp = TextureRegion.split(jumpSheet, jumpSheet.getWidth() / COLUMNS_JUMP,
jumpSheet.getHeight() / FRAME_ROWS);
TextureRegion[] jumpFrames = new TextureRegion[COLUMNS_JUMP * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < COLUMNS_JUMP; j++) {
jumpFrames[index++] = tmp[i][j];
}
}
jumpAnimation = new Animation<TextureRegion>(0.1f, jumpFrames);
stateTime = 0f;
reg = jumpAnimation.getKeyFrame(0);
}
public void frontFlipAnimation(){
frontFlipSheet = new Texture(Gdx.files.internal("ken/front_flip.png"));
TextureRegion [][] tmp = TextureRegion.split(frontFlipSheet, frontFlipSheet.getWidth() / COLUMNS_FRONTFLIP,
frontFlipSheet.getHeight() / FRAME_ROWS);
TextureRegion[] frontFlipFrames = new TextureRegion[COLUMNS_FRONTFLIP * FRAME_FRONTFLIP];
int index = 0;
for (int i = 0; i < FRAME_FRONTFLIP; i++) {
for (int j = 0; j < COLUMNS_FRONTFLIP; j++) {
frontFlipFrames[index++] = tmp[i][j];
}
}
frontFlipAnimation = new Animation<TextureRegion>(0.1f, frontFlipFrames);
stateTime = 0f;
reg = frontFlipAnimation.getKeyFrame(0);
}
public void backFlipAnimation(){
backFlipSheet = new Texture(Gdx.files.internal("ken/back_flip.png"));
TextureRegion [][] tmp = TextureRegion.split(backFlipSheet, backFlipSheet.getWidth() / COLUMNS_BACKFLIP,
backFlipSheet.getHeight() / FRAME_BACKLIP);
TextureRegion[] backFlipFrames = new TextureRegion[COLUMNS_BACKFLIP * FRAME_BACKLIP];
int index = 0;
for (int i = 0; i < FRAME_BACKLIP; i++) {
for (int j = 0; j < COLUMNS_BACKFLIP; j++) {
backFlipFrames[index++] = tmp[i][j];
}
}
backFlipAnimation = new Animation<TextureRegion>(0.1f, backFlipFrames);
stateTime = 0f;
reg = backFlipAnimation.getKeyFrame(0);
}
#Override
public void act(float delta) {
super.act(delta);
stateTime += delta;
stateTime += delta;
reg = walkAnimation.getKeyFrame(stateTime,true);
if(Gdx.input.isKeyPressed(Input.Keys.A)){
reg = kickAnimation.getKeyFrame(stateTime, false);
this.addAction(Actions.moveTo(getX() +2, getY(), 1 / 10F));
}
if(Gdx.input.isKeyPressed(Input.Keys.S)){
reg = punchAnimation.getKeyFrame(stateTime, false);
}
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
reg = leftAnimation.getKeyFrame(stateTime, true);
this.addAction(Actions.moveTo(getX() - 10, getY(), 1 / 10f ));
}
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
reg = rightAnimation.getKeyFrame(stateTime, true);
this.addAction(Actions.moveTo(getX() + 10, getY(), 1 /10f));
}
if(Gdx.input.isKeyPressed(Input.Keys.UP)){
body.applyLinearImpulse(new Vector2(0, 20), body.getWorldCenter(), true);
reg = jumpAnimation.getKeyFrame(stateTime, false);
this.addAction(Actions.moveTo(getX(), getY() + 10, 1/ 10f));
}
if(Gdx.input.isKeyPressed(Input.Keys.D)){
reg = frontFlipAnimation.getKeyFrame(stateTime, true);
this.addAction(Actions.moveTo(getX() + 5, getY(), 1 / 10f));
}
if(Gdx.input.isKeyPressed(Input.Keys.W)){
reg = backFlipAnimation.getKeyFrame(stateTime, true);
this.addAction(Actions.moveTo(getX() - 5, getY(), 1 / 10F));
}
}
#Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
batch.draw(reg,getX(),getY(),getWidth()/2,getHeight()/2,getWidth(),getHeight(),getScaleX(),getScaleY(),getRotation());
}
private void defineKen(){
BodyDef bdef = new BodyDef();
bdef.position.set(32 / 100, 32 / 100);
bdef.type = BodyDef.BodyType.DynamicBody;
body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(7 / 100);
fdef.shape = shape;
body.createFixture(fdef).setUserData(this);
body.createFixture(fdef).setUserData(this);
}
}
Repo
Thanks alot
You work with Box2d and Box2d is a 2d physics engine so it also integrated gravity.
First, when you create your World you can define the gravity force:
world = new World(new Vector2(0,-15f), true);
This will pull your Player down.
Then you must update the world every frame. So call in the render() method:
world.step(delta, 6, 2);
Now the Body position will be calculated by the world and gravity apply to the body.
Important now is that you first have a Static body as Ground otherwise, the body will fall down infinitely. Secondly, you must change the place where you draw the Image of Ken to the place of the Body so in act() method update the position:
setX(body.getPosition().x);
setY(body.getPosition().y);
Maybe you look for some Box2d tutorials to have a better understanding:
https://www.gamedevelopment.blog/full-libgdx-game-tutorial-box2d/
I'm trying to initialize a bi-dimensional array of an object that I've created that has a some parameters (x,y,width,height) but doesn't work... The object is just a g.fillOval and when I do the initializing only prints the last object of the array.
Ovals = new Oval[4][4];
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
Ovals[x][y] = new Oval(x*100, y, 30,30);
}
}
...
for (int y = 0; y < 4; y++) {
for (int x = 0; x < 4; x++) {
Ovals[x][y].paint(g);
}
}
The Oval class:
package objectes;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Oval extends Canvas{
private static Random random;
private static int r1 = 0;
private static int r2 = 0;
private static int x = 0;
private static int y = 0;
private static int randomN = 5;
public static int color; //0 = red(#FF5C5C), 1 = blue(#4097ED), 2 = green(#65EB8F), 3 = yellow(#F5F267), 4 = orange(#FFAD42)
public Oval(int x, int y, int r1, int r2) {
//Constructor
this.x = x;
this.y = y;
this.r1 = r1;
this.r2 = r2;
random = new Random();
randomN = random.nextInt();
if (randomN < 0 ) {
randomN = randomN*-1;
}
randomN = randomN % 5;
}
public void paint(Graphics g) {
switch (randomN) {
case 0:
g.setColor(Color.decode("#ff5C5C"));
break;
case 1:
g.setColor(Color.decode("#4097ed"));
break;
case 2:
g.setColor(Color.decode("#65eb8f"));
break;
case 3:
g.setColor(Color.decode("#f5f267"));
break;
case 4:
g.setColor(Color.decode("#ffad42"));
break;
}
g.fillOval(x, y, r1, r2);
}
}
All of your classes variables are static
private static int r1 = 0;
private static int r2 = 0;
private static int x = 0;
private static int y = 0;
This means they are associated with the class Oval.. not a single instance of Oval.
Because there is only one copy of each variable, every time you make a new Oval you will overwrite the last value set. When you finally go to draw the Ovals, all of them will be drawn in the exact same spot!
Make them instance variables instead:
private int r1 = 0;
private int r2 = 0;
private int x = 0;
private int y = 0;
I have been working on making a neural network that has a goal, of hitting a moving target, it has inputs based on the distance from the shooter to the target from both axis, the rotation of the shooter, and the wind speed.
Every 5 seconds the shooter resets to a different position on the screen.
I have attempted to use a back prop algorithm, by giving calculating the error based on the distance the bullet hits from the target, and if the shooter doesn't fire a shot in the 5 second window there is also an error propagated.
My network however does not seem to be learning properly, I was wondering if anyone could point me in the right direction.
Here is my code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Random;
import java.util.stream.DoubleStream;
import javax.swing.JFrame;
public class ClassMain extends JFrame{
int ShooterLocationX = 100;
int ShooterLocationY = 150;
int shooterDiameter = 35;
int targetX = 525;
int targetY = 100;
int targetWidth = 15;
int targetLength = 50;
double error;
double targetVectorX=ShooterLocationX + 100;
double targetVectorY = ShooterLocationY+shooterDiameter/2;
boolean direction = false;
boolean ShotFired = false;
boolean rotateDirection;
boolean start =true;
boolean shotbeenFires;
double ShooterRotation = 0;
double[] outputs1 = new double[4];
double[] outputs2 = new double[8];
double[] outputs3 = new double[8];
double[] outputs4 = new double[3];
Percepton[] input = new Percepton[4];
Percepton[] hidden = new Percepton[8];
Percepton[] hidden2 = new Percepton[8];
Percepton[] output = new Percepton[3];
Shooter shot = new Shooter(ShooterRotation);
Random random = new Random();
long StartTime = 0;
long CurrTime = 0;
public static void main(String []args){
ClassMain CM= new ClassMain();
CM.setup();
}
public void setup(){
setSize(900,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
createBufferStrategy(2);
repaint();
for(int i = 0; i < 4; i ++){
input[i] = new Percepton();
input[i].setInputNumber(1);
}
for(int i =0;i < 8;i++){
hidden[i] = new Percepton();
hidden[i].setInputNumber(input.length);
}
for(int i =0;i < 8;i++){
hidden2[i] = new Percepton();
hidden2[i].setInputNumber(input.length);
}
for(int i = 0;i < 3; i++){
output[i] = new Percepton();
output[i].setInputNumber(hidden.length);
}
}
public void run(){
double[] temp = new double[10];
if(start){
StartTime = System.currentTimeMillis();
start = false;
}
CurrTime = System.currentTimeMillis();
if(CurrTime-StartTime > 5000){
ShooterLocationX=random.nextInt(400);
ShooterLocationY=random.nextInt(275);
shot.newX = ShooterLocationX+(shooterDiameter/2)-5;
shot.newY = ShooterLocationY+(shooterDiameter/2)-5;
shot.windSpeed = (float)(random.nextInt(200)-100)/100;
start = true;
if(!shotbeenFires){
error+=15;
}
for(int n = 0; n < output.length;n++){
output[n].learn(n, error, output[n].weights[n]);
}
for(int n = 0; n < hidden2.length;n++){
hidden2[n].learn(n, error, hidden2[n].weights[n]);
}
for(int n = 0; n < hidden.length;n++){
hidden[n].learn(n, error, hidden[n].weights[n]);
}
for(int n = 0; n < input.length;n++){
input[n].learn(n, error, input[n].weights[n]);
}
error = 0;
shotbeenFires = false;
}
outputs1[0] = input[0].sigmoid((((Math.max(ShooterLocationX,targetX)-Math.min(ShooterLocationX,targetX))/300)*2)*input[0].weights[0],1*input[0].biasWeight);
outputs1[1] = input[1].sigmoid((((Math.max(ShooterLocationY,targetY)-Math.min(ShooterLocationY,targetY))/150)*2)*input[1].weights[0],1*input[1].biasWeight);
outputs1[2] = input[2].sigmoid((ShooterRotation)*input[2].weights[0],1*input[2].biasWeight);
outputs1[3] = input[3].sigmoid((shot.windSpeed)*input[3].weights[0],1*input[3].biasWeight);
System.out.println("\n");
for(int n = 0;n < hidden.length;n++){
for(int i = 0;i<temp.length;i++){
temp[i] = 0;
}
for(int i = 0;i < outputs1.length;i++){
temp[i] = outputs1[i]*hidden[n].weights[i];
}
outputs2[n] = (float)DoubleStream.of(temp).sum();
outputs2[n] = hidden[1].sigmoid(outputs2[n],1*hidden[n].biasWeight);
//System.out.print(outputs2[n]);
}
System.out.println("\n");
for(int n = 0;n < hidden2.length;n++){
for(int i = 0;i<temp.length;i++){
temp[i] = 0;
}
for(int i = 0;i < outputs2.length;i++){
temp[i] = outputs2[i]*hidden2[n].weights[i];
}
outputs3[n] = (float)DoubleStream.of(temp).sum();
outputs3[n] = hidden2[1].sigmoid(outputs3[n],1*hidden2[n].biasWeight);
//System.out.print(outputs3[n]);
}
System.out.println("\n");
for(int n = 0;n < output.length;n++){
for(int i = 0;i<temp.length;i++){
temp[i] = 0;
}
for(int i = 0;i < outputs1.length;i++){
temp[i] = outputs3[n]*hidden[n].weights[i];
}
outputs4[n] = (float)DoubleStream.of(temp).sum();
outputs4[n] = output[n].sigmoid(outputs4[n],1*output[n].biasWeight);
//System.out.print(outputs4[n]);
}
if(!ShotFired){
System.out.println("\n");
if(outputs4[0] > 0.5){
ShotFired = true;
shot.rotation = ShooterRotation;
}
if (outputs4[1] > 0.5){
ShooterRotation = ShooterRotation + outputs3[1];
}
if(outputs4[2] > 0.5){
ShooterRotation =ShooterRotation - outputs3[2];
}
if(ShooterRotation*360 > 360){
ShooterRotation = ((ShooterRotation * 360) + 360) /360;
}else if(ShooterRotation * 360 < 0){
ShooterRotation = ((ShooterRotation*360)-360) /360;
}
}
for(int i = 0; i < 1; i++){
if(ShotFired){
shotbeenFires = true;
shot.newLocationX(shot.newX);
shot.newLocationY(shot.newY);
}
if(targetY < 20 ||targetY+targetLength >300){
direction = !direction;
}
if(direction){
targetY += 8;
}else{
targetY -= 8;
}
if(shot.newX + (15)>= targetX && shot.newX + 15 < targetX +targetWidth && shot.newY + 15> targetY && shot.newY <targetY + targetLength){
System.out.println("Target Hit");
error -= 10;
shot.newX = ShooterLocationX + shooterDiameter/2-5;
shot.newY = ShooterLocationY + shooterDiameter/2-5;
ShotFired = false;
}else if(shot.newX > 600|| shot.newX<10||shot.newY<10||shot.newY> 290){
System.out.println("MISS");
error += (shot.newY - targetY);
shot.newX = ShooterLocationX + shooterDiameter/2-5;
shot.newY = ShooterLocationY + shooterDiameter/2-5;
ShotFired = false;
}
}
repaint();
}
public void paint(Graphics g){
targetVectorX = ShooterLocationX + shooterDiameter/2 + Math.cos(Math.toRadians(ShooterRotation)) * 100;
targetVectorY = ShooterLocationY + shooterDiameter/2 + Math.sin(Math.toRadians(ShooterRotation)) * 100;
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
g2.fillOval(ShooterLocationX, ShooterLocationY, shooterDiameter, shooterDiameter);
g2.setPaint(Color.RED);
g2.drawLine(ShooterLocationX + shooterDiameter/2, ShooterLocationY+shooterDiameter/2, (int)targetVectorX, (int)targetVectorY);
g2.setPaint(Color.BLUE);
g2.fillRect(targetX, targetY, targetWidth, targetLength);
g2.setPaint(Color.RED);
g2.fillOval((int)shot.newX, (int)shot.newY, 10, 10);
g2.setPaint(Color.gray);
g2.fillRect(600, 0, 300,300);
for(int i = 0; i < input.length;i++){
if(outputs1[i] > 0.5){
g2.setPaint(Color.red);
}else{
g2.setPaint(Color.white);
}
g2.drawOval(625+i*35, 250, 25, 25);
for(int j = 0;j<hidden.length;j++){
g2.drawLine(640+i*35, 250, 615+j*30, 195);
}
}
for(int i = 0; i < hidden.length;i++){
if(outputs2[i] > 0.5){
g2.setPaint(Color.red);
}else{
g2.setPaint(Color.white);
}
g2.drawOval(605+i*30, 175, 20, 20);
for(int j = 0;j<hidden2.length;j++){
g2.drawLine(615+i*30, 175, 615+j*30, 145);
}
}
for(int i = 0; i < hidden2.length;i++){
if(outputs3[i] > 0.5){
g2.setPaint(Color.red);
}else{
g2.setPaint(Color.white);
}
g2.drawOval(605+i*30, 125, 20, 20);
for(int j = 0;j<output.length;j++){
g2.drawLine(615+i*30, 125, 697+j*50, 75);
}
}
for(int i = 0; i < output.length;i++){
if(outputs4[i] > 0.5){
g2.setPaint(Color.red);
}else{
g2.setPaint(Color.white);
}
g2.drawOval(685+i*50, 50, 25, 25);
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
run();
}
}
public class Shooter {
double windSpeed = 0.5;
int distance = 0;
double newX;
double newY;
double rotation;
double temp = 0;
public Shooter(double rotate){
rotation = rotate*360;
windSpeed = windSpeed*5;
}
public void newLocationX(double XPrev){
newX = XPrev + Math.cos(Math.toRadians(rotation)) * 20;
}
public void newLocationY(double YPrev){
newY = YPrev + Math.sin(Math.toRadians(rotation)) * 20;
newY =newY - windSpeed;
}
}
import java.util.Random;
public class Percepton {
int numOfInputs = 100;
float[] weights = new float[numOfInputs];
int sum;
double learningRate = 0.1;
float biasWeight;
float a;
float out;
Random random = new Random();
public void setWeight(int index,float weight){
weights[index] = weight;
}
public void setInputNumber(int inputs){
numOfInputs = inputs;
for(int i = 0; i < inputs; i++){
weights[i] = (float)(random.nextInt(400)-200)/200;
biasWeight = (float)(random.nextInt(400)-200)/200;
//System.out.println(weights[i]);
}
}
public void learn(int index, double error, float value){
weights[index] += (float)learningRate * (float)error * value;
}
public double sigmoid(double input,double bias){
return (float) (1/(1+Math.exp(-input*bias)));
}
}
I cant seem to figure it out is it possible that someone can tell me why?
public class Display {
private int width,height;
public int [] pixels;
public int [] tiles = new int[64 * 64];
private Random random = new Random();
public Display(int width, int height) {
this.width = width;
this.height = height;
pixels = new int [width*height]; // 50400
for (int i = 0; i < 64 * 64; i++) {
tiles[i] = random.nextInt (0xffffff);
}
}
public void clear() {
for (int i = 0; i < pixels.length; i++) {
tiles[i] = random.nextInt (0xffffff);
}
}
public void render() {
for (int y = 0; y <height; y++) {
if (y < 0 || y >= height) break;
for (int x = 0; x < width; x++) {
if (x < 0 || x >=width) break;
int tileIndex = (x / 16) + (y / 16) * 64;
pixels[x+y*width] = tiles[tileIndex];
}
}
}
}
The ArrayIndexOutOfBoundsException is likely to occur at the assignment in the clear() method.
You are iterating from 0 to pixels.length. pixels.length is variable-sized (according to what is passed to the constructor). While iterating you assign values to tiles[i]. Tiles is a fixed sized array (64*64 = 4.096 entries). If width*height > 4096, the clear() method will fail if it tries to access tiles[4096] or above.
Maybe you wanted to iterate up to tiles.length only?