Yay! Thank you for a quick reply! 
Here is the entire code, so you can compile and run if you so wish.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
/**Testing how a gridsystem for a platformer would work.
*
* TODO:
* -Fix movement and sideways collision.
* -Avoid the ArrayIndex out of bounds error when it should be respawn.
* .Fix the jumpfunction.
*
* @author hallgeir
*/
public class PlatformTest extends JFrame implements ActionListener{
Level level1 = new Level();
/*Arrayrelated variables*/
Rectangle[][] levelGrid = new Rectangle[20][20];
Boolean[][] physicGrid = new Boolean[20][20];
int gridX, gridY, i, j;
final int size = 20;
/*Variables of the player component*/
int x = 200, y = 0, xSpd, ySpd, dir, degrees, rot, yB;
boolean moving, jumping, onFloor;
public PlatformTest() {
setTitle("PlatformTest");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400,400);
setResizable(false);
/*Creates the rectangles of the grid. 400 rectangles total.*/
for (i = 0; i < 20; i++) {
for (j = 0; j < 20; j++) {
levelGrid[i][j] = new Rectangle(gridX, gridY, size, size);
physicGrid[i][j] = false; //sets all fields to not physical.
// System.out.print(levelGrid[i][j]);
// System.out.print(" " + i);
// System.out.println(", " + j);
gridX += 20;
if (gridX == 400) {
gridX = 0;
gridY += 20;
}
}
}
addKeyListener(level1);
add(level1);
new Timer(10, this).start();
setVisible(true);
}
public static void main(String args[]) {
new PlatformTest();
}
@Override
public void actionPerformed(ActionEvent ae) {
repaint();
}
private class Level extends JPanel implements ActionListener, KeyListener {
//URL url = getClass().getResource("Resources/Images/1.png");
//Image img = Toolkit.getDefaultToolkit().getImage(url);
public Level() {
setPreferredSize(new Dimension(400,400));
new Timer(10, this).start();
}
@Override
public void paintComponent(Graphics g) {
/*Background:*/
g.setColor(Color.gray);
g.fillRect(0, 0, 400, 400);
/*Platforms:*/
g.setColor(Color.black);
for (int a = 15; a < 20; a++) {
int yA = 9;
int aX = (int)levelGrid[yA][a].getX();
int aY = (int)levelGrid[yA][a].getY();
physicGrid[yA][a] = true;
g.fillRect(aX, aY, 20, 20);
}
for (int a = 5; a < 15; a++) {
int yA = 11;
int aX = (int)levelGrid[yA][a].getX();
int aY = (int)levelGrid[yA][a].getY();
physicGrid[yA][a + 1] = true;
g.fillRect(aX, aY, 20, 20);
}
for (int a = 1; a < 11; a++) {
int yA = 15;
int aX = (int)levelGrid[yA][a].getX();
int aY = (int)levelGrid[yA][a].getY();
physicGrid[yA][a + 1] = true;
g.fillRect(aX, aY, 20, 20);
}
for (int a = 12; a < 15; a++) {
int yA = 17;
int aX = (int)levelGrid[yA][a].getX();
int aY = (int)levelGrid[yA][a].getY();
physicGrid[yA][a + 1] = true;
g.fillRect(aX, aY, 20, 20);
}
for (int a = 1; a < 19; a++) {
int yA = 18;
int aX = (int)levelGrid[yA][a].getX();
int aY = (int)levelGrid[yA][a].getY();
physicGrid[yA][a] = true;
g.fillRect(aX, aY, 20, 20);
}
/*Player:*/
// Graphics2D g2 = (Graphics2D)g;
// g2.rotate(Math.toRadians(degrees), x + 10, y + 10);
// g2.drawImage(img, x, y, this);
g.setColor(Color.black);
g.fillOval(x, y, 20, 20);
}
@Override
public void actionPerformed(ActionEvent ae) {
int posX = 0, posY = 0;
Position pos = new Position(posX, posY);
/*Cretes the player rectangle:*/
Rectangle pRect = new Rectangle(x, y, 20, 20);
/*To recognize the position of the player:*/
int aX = 0, aY = 0;
while (aY < 20) {
if (pRect.intersects(levelGrid[aX][aY])) {
pos.setPos(aX, aY);
}
aX++;
if (aX == 20) {
aX = 0;
aY++;
}
}
/*General movement:*/
if (x < 400 && x > 0) {
x += xSpd;
}
y += ySpd;
/*Gravity:*/
if (physicGrid[pos.getPosY()][pos.getPosX()]) {
ySpd = 0;
onFloor = true;
}
else {
ySpd = 2;
onFloor = false;
}
/*Moving:*/
if (moving) {
if (dir == 1 && x > 0) {
if (!physicGrid[pos.getPosY() - 1][pos.getPosX() - 1]) {
xSpd = -2;
System.out.println(physicGrid[pos.getPosY() - 1][pos.getPosX() - 1]);
}
}
if (dir == 2 && x < 380) {
if (!physicGrid[pos.getPosY() - 1][pos.getPosX() + 1]) {
xSpd = 2;
System.out.print(pos.getPosX() + " ");
System.out.println(pos.getPosY());
}
}
}
else {
xSpd = 0;
}
/*Jumping:*/
if (jumping) {
if (!physicGrid[pos.getPosY() - 1][pos.getPosX()]) {
if (yB - 50 < y && y > 0) {
ySpd = -3;
}
else {
ySpd = 0;
}
}
else {
ySpd = 0;
}
}
/*Respawn:*/
if (y > 420) {
x = (int)levelGrid[2][10].getX();;
y = (int)levelGrid[2][10].getY();
}
}
@Override
public void keyTyped(KeyEvent ke) {}
@Override
public void keyPressed(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_LEFT:
moving = true;
dir = 1;
break;
case KeyEvent.VK_RIGHT:
moving = true;
dir = 2;
break;
case KeyEvent.VK_SPACE:
if (onFloor) {
jumping = true;
yB = y;
}
default:
break;
}
}
@Override
public void keyReleased(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_LEFT:
moving = false;
break;
case KeyEvent.VK_RIGHT:
moving = false;
break;
case KeyEvent.VK_SPACE:
jumping = false;
break;
default:
break;
}
}
}
private class Position {
int posX, posY;
public Position(int posX, int posY) {
this.posX = posX;
this.posY = posY;
}
public void setPos(int posX, int posY) {
this.posX = posX;
this.posY = posY;
}
public int getPosX() {
return posY;
}
public int getPosY() {
return posX;
}
}
}
As you can see, I have made a mess, trying to fix things…
And I messed up somehow on handling the axises properly…
The problems is in the TODO in the head comment, but what is most urgent to fix for me, is to avoid being able to roll into a platform from the side.
So, should I scrap this code, and start anew in a different way, or can this work?