Repaint() not called - java

I'm developing a game for a school project, a Bomberman-like game.
I'm using swing and I was using Canvas to draw my graphics but the KeyListener was not working, so I quitted using Canvas and started using paintComponent(Graphics g). The KeyListener is responding now, but my graphics don't refresh when my while loop calls the repaint() methods.
My code:
dispose();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
board b = new board();
b.setSize(630, 650);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setVisible(true);
direction dessin = new direction();
b.add(dessin);
b.setVisible(true);
dessin.setBackground(Color.BLACK);
}
});
Then:
package Bm;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import javax.swing.JComponent;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class direction extends JPanel implements Runnable {
static float bmx = 35;
static float bmy = 35;
static float ex = 520;
static float ey = 520;
static float v = 0.03f;
static boolean gauche;
static boolean droite;
static boolean haut;
static boolean bas;
static void movEnnemi() {
int r = 1 + (int) (Math.random() * ((4 - 1) + 1));
Ennemi.droite = false;
Ennemi.gauche = false;
Ennemi.bas = false;
Ennemi.haut = false;
switch (r) {
case 1:
Ennemi.droite = true;
break;
case 2:
Ennemi.gauche = true;
break;
case 3:
Ennemi.bas = true;
break;
case 4:
Ennemi.haut = true;
break;
}
try {
Thread.sleep(5);
} catch (Exception e) {
}
;
}
public direction() {
super();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int ligne = 0; ligne < board.gridHauteur; ligne++) {
for (int colonne = 0; colonne < board.gridLargeur; colonne++) {
switch (board.plateau1[ligne][colonne]) {
case 0:
g.setColor(Color.lightGray);
g.fillRect(30 * ligne, 30 * colonne, 30, 30);
break;
case 1:
g.setColor(Color.black);
g.fillRect(30 * ligne, 30 * colonne, 30, 30);
board.plateau1[ligne][colonne] = board.BLOCKED;
break;
case 2:
g.setColor(Color.darkGray);
g.fillRect(30 * ligne, 30 * colonne, 30, 30);
board.plateau1[ligne][colonne] = board.BLOCKED;
break;
}
}
}
g.setColor(Color.blue);
g.fillRect((int) ex, (int) ey, 20, 20);
g.setColor(Color.red);
g.fillRect((int) bmx, (int) bmy, 21, 21);
g.dispose();
}
public void run() {
long dernierTempsLoop = System.currentTimeMillis();
while (true) {
long delta = (System.currentTimeMillis() - dernierTempsLoop);
dernierTempsLoop = System.currentTimeMillis();
movEnnemi();
for (int i = 0; i < delta / 5; i++) {
logic(5);
Ennemi.logic(5);
}
if ((delta % 5) != 0) {
logic(delta % 5);
Ennemi.logic(delta % 5);
}
System.out.println((int) (bmx / 30) + " - " + (int) (bmy / 30));
try {
Thread.sleep(20);
} catch (Exception e) {
}
;
repaint(); // <== HERE
}
}
public static void logic(long delta) {
float dx = 0;
float dy = 0;
if (gauche) {
dx--;
}
if (droite) {
dx++;
}
if (haut) {
dy--;
}
if (bas) {
dy++;
}
if ((dx != 0) || (dy != 0)) {
joueur.mouvement(dx * delta * v, dy * delta * v);
if (joueur.mouvement((dx * delta * v), (dy * delta * v)) == false) {
if (joueur.mouvement(0, dy * delta * v)) {
joueur.mouvement(0, dy * delta * v);
}
if (joueur.mouvement(dx * delta * v, 0)) {
joueur.mouvement(dx * delta * v, 0);
}
}
}
}
}
And :
package Bm;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import javax.swing.*;
#SuppressWarnings("serial")
class board extends JFrame implements KeyListener {
static JPanel p;
public Timer fpstimer;
public direction g;
static final int BLOCKED = 1;
static int gridLargeur = 21;
static int gridHauteur = 21;
int fenLargeur = 630;
int fenHauteur = 650;
public static int plateau1[][] = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
public board() {
super("Bomberman");
g = new direction();
addKeyListener(this);
threadLoop p = new threadLoop("loop");
p.start();
}
public static boolean blocked(double d, double e) {
return plateau1[(int) d][(int) e] == BLOCKED;
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
direction.gauche = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
direction.droite = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
direction.bas = true;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
direction.haut = true;
}
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
System.exit(0);
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
direction.gauche = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
direction.droite = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
direction.bas = false;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
direction.haut = false;
}
}
public void keyTyped(KeyEvent e) {
}
}
class threadLoop extends Thread {
public direction g;
threadLoop(String name) {
super(name);
}
public void run() {
g = new direction();
g.run();
}
}
public class Jouer {
}
I hope you understand my problem, and will be able to help me, thank you :)

You are creating different instances of your class direction, hence the issue you are seeing
You should not use static like this. It goes against good OO-programming
Use Swing key-bindings instead of using KeyListener
Follow java naming conventions (your code is really hard to read for now): Classes start with an Upper case letter, methods and variables with a lower-case letter. Use CamelCase to concatenate words.
Don't extend when not needed (JFrame, Thread, etc...)
Try to separate the various concepts of your program (one part should be responsible for the display (displaying the board, the enemies, the player), another one to react on user input (left-key pressed, right-key pressed, etc...) and a third one to handle the logic of your game (player location, enemies location, the board, etc...)).
Here is a very lame attempt to explain those various advices:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
public class Game {
private static final String ICON_URL = "http://images2.wikia.nocookie.net/__cb20100515002803/fanon/images/a/a2/Bomberman_sprite.png";
private static final int GRID_SIZE = 24;
private static final int SQUARE_SIZE = 30;
private JFrame frame;
private Board board;
private static class Board extends JPanel {
private int[][] grid;
private int playerX;
private int playerY;
private ImageIcon playerIcon;
public Board() throws MalformedURLException {
// Some code to generate a random pseudo-board
Random random = new Random();
grid = new int[GRID_SIZE][];
for (int i = 0; i < GRID_SIZE; i++) {
grid[i] = new int[GRID_SIZE];
for (int j = 0; j < GRID_SIZE; j++) {
int r = random.nextInt(10);
grid[i][j] = r > 8 ? 2 : r > 6 ? 1 : 0;
}
}
playerIcon = new ImageIcon(new URL(ICON_URL));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(GRID_SIZE * SQUARE_SIZE, GRID_SIZE * SQUARE_SIZE);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// pseudo-board painting
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
switch (grid[i][j]) {
case 1:
g.setColor(Color.GREEN);
g.fillRect(i * SQUARE_SIZE, j * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);
break;
case 2:
g.setColor(Color.RED);
g.fillRect(i * SQUARE_SIZE, j * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);
break;
default:
break;
}
}
}
// Player painting
int x = playerX * SQUARE_SIZE + (SQUARE_SIZE - playerIcon.getIconWidth()) / 2;
int y = playerY * SQUARE_SIZE + (SQUARE_SIZE - playerIcon.getIconHeight()) / 2;
g.drawImage(playerIcon.getImage(), x, y, this);
}
public int getPlayerX() {
return playerX;
}
public int getPlayerY() {
return playerY;
}
public void setPlayerX(int playerX) {
if (playerX >= 0 && playerX < GRID_SIZE && grid[playerX][playerY] == 0) {
this.playerX = playerX;
repaint();
}
}
public void setPlayerY(int playerY) {
if (playerY >= 0 && playerY < GRID_SIZE && grid[playerX][playerY] == 0) {
this.playerY = playerY;
repaint();
}
}
}
private class MoveLeftAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
board.setPlayerX(board.getPlayerX() - 1);
}
}
private class MoveRightAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
board.setPlayerX(board.getPlayerX() + 1);
}
}
private class MoveUpAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
board.setPlayerY(board.getPlayerY() - 1);
}
}
private class MoveDownAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
board.setPlayerY(board.getPlayerY() + 1);
}
}
private class ExitAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
int i = JOptionPane.showConfirmDialog(board, "Are you sure you want to exit?");
if (i == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
}
protected void initUI() throws MalformedURLException {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
board = new Board();
board.setBackground(Color.BLACK);
board.registerKeyboardAction(new MoveLeftAction(), KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), JComponent.WHEN_FOCUSED);
board.registerKeyboardAction(new MoveRightAction(), KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), JComponent.WHEN_FOCUSED);
board.registerKeyboardAction(new MoveUpAction(), KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), JComponent.WHEN_FOCUSED);
board.registerKeyboardAction(new MoveDownAction(), KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), JComponent.WHEN_FOCUSED);
board.registerKeyboardAction(new ExitAction(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW);
frame.add(board);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
try {
new Game().initUI();
} catch (MalformedURLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Could not load icon from Internet", "Unable to start", JOptionPane.ERROR_MESSAGE);
}
}
});
}
}
Please note that the display and the logic of the games are here completely intertwined, so I don't quite follow advice nr 6.

The direction you assign to the frame/board is not the same you a trying to paint to. You create a new reference in your main loop
I'd also suggest you use key bindings over key listeners
Updated
You construct a initial direction and then add that to the screen. This is actually what will be rendered.
direction dessin = new direction();
b.add(dessin);
But in your threadLoop you create a new (disconnected) direction and start trying to update it...
public void run() {
g = new direction();
g.run();
}
...this will never paint as it has no connection to the screen.
You also create a third reference in board...
g = new direction();
All these disconnected direction classes have no one to paint or communicate with each and are unnecessary.
I would create a single reference in board, add it to the frame and pass that reference to the threadLoop
Updated
You could take a look at this. It's a basic example demonstrating a simple threaded animation engine and key bindings for the movement of a game asset

Related

I can't translate points in my 3D graphics engine (java)

I'm following javidx9's tutorial on youtube about making a 3D graphics engine (I'm doing it in java, rather than C++, but I'm pretty closely following his steps), and I'm at the point where we move the cube away from the 'camera' to better see it (35:49, if you're curious), but as soon as I add the relevant lines of code and run it, nothing shows up. When I take them away, it comes back, and I have no idea what's happening. Code is below, sorry it's kind of long and probably poorly written (using Graphics2D for display):
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
static Frame frame;
static double FOV = Math.PI / 2;
static int w = 400;
static int h = 400;
static double a = h / w;
static double f = 1 / Math.tan(FOV / 2);
static double zFar = 1000;
static double zNear = 0.1;
static double q = zFar / (zFar - zNear);
static double[][] matProj = new double[4][4];
public static void main(String[] args) {
frame = new Frame();
}
static void draw() {
Mesh cubeMesh = new Mesh();
//south
cubeMesh.addTri(0, 0, 0, 0, 1, 0, 1, 1, 0);
cubeMesh.addTri(0, 0, 0, 1, 1, 0, 1, 0, 0);
//east
cubeMesh.addTri(1, 0, 0, 1, 1, 0, 1, 1, 1);
cubeMesh.addTri(1, 0, 0, 1, 1, 1, 1, 0, 1);
//north
cubeMesh.addTri(1, 0, 1, 1, 1, 1, 0, 1, 1);
cubeMesh.addTri(1, 0, 1, 0, 1, 1, 0, 0, 1);
//west
cubeMesh.addTri(0, 0, 1, 0, 1, 1, 0, 1, 0);
cubeMesh.addTri(0, 0, 1, 0, 1, 0, 0, 0, 0);
//top
cubeMesh.addTri(0, 1, 0, 0, 1, 1, 1, 1, 1);
cubeMesh.addTri(0, 1, 0, 1, 1, 1, 1, 1, 0);
//bottom
cubeMesh.addTri(1, 0, 1, 0, 0, 1, 0, 0, 0);
cubeMesh.addTri(1, 0, 1, 0, 0, 0, 1, 0, 0);
matProj[0][0] = a * f;
matProj[1][1] = f;
matProj[2][2] = q;
matProj[3][2] = -1 * q * zNear;
matProj[2][3] = 1;
matProj[3][3] = 0;
frame.graphics.g2D.setColor(Color.WHITE);
frame.graphics.g2D.setStroke(new BasicStroke(2));
for(TrianglePoints i : cubeMesh.triangles) {
TrianglePoints translated = i;
translated.p1[2] = i.p1[2] + 3; //when I comment out these lines, it works, but then of course the cube isn't translated
translated.p2[2] = i.p2[2] + 3;
translated.p3[2] = i.p3[2] + 3;
int[] xPoints = {(int) MultiplyMatrixVector(translated.p1, matProj)[0], (int) MultiplyMatrixVector(translated.p2, matProj)[0], (int) MultiplyMatrixVector(translated.p3, matProj)[0]};
int[] yPoints = {(int) MultiplyMatrixVector(translated.p1, matProj)[1], (int) MultiplyMatrixVector(translated.p2, matProj)[1], (int) MultiplyMatrixVector(translated.p3, matProj)[1]};
for(int j = 0; j < xPoints.length; j++) {
xPoints[j] += 1;
xPoints[j] *= (0.5 * w);
yPoints[j] += 1;
yPoints[j] *= (0.5 * w);
}
frame.graphics.g2D.drawPolygon(xPoints, yPoints, 3);
}
}
private static double[] MultiplyMatrixVector(double[] i, double[][] m) {
double[] o = new double[3];
o[0] = i[0] * m[0][0] + i[1] * m[1][0] + i[2] * m[2][0] + m[3][0];
o[1] = i[0] * m[0][1] + i[1] * m[1][1] + i[2] * m[2][1] + m[3][1];
o[2] = i[0] * m[0][2] + i[1] * m[1][2] + i[2] * m[2][2] + m[3][2];
double w = i[0] * m[0][3] + i[1] * m[1][3] + i[2] * m[2][3] + m[3][3];
if(w != 0) { //I think this might be where the issue is, when I translate it w won't be 0, but I don't know how that would make it not work, I have to divide by w here.
o[0] /= w;
o[1] /= w;
o[2] /= w;
}
return o;
}
}
class Frame extends JFrame {
private static final long serialVersionUID = 1L;
GraphicsC graphics = new GraphicsC();
Frame(){
this.setSize(Main.w, Main.h);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(graphics);
this.setVisible(true);
}
}
class GraphicsC extends JPanel {
private static final long serialVersionUID = 1L;
Graphics2D g2D;
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.BLACK);
g2D = (Graphics2D) g;
Main.draw();
}
}
class TrianglePoints {
double[] p1 = new double[3];
double[] p2 = new double[3];
double[] p3 = new double[3];
TrianglePoints(double[] p1, double[] p2, double[] p3) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
}
class Mesh {
List<TrianglePoints> triangles = new ArrayList<TrianglePoints>();
void addTri(double a, double b, double c, double d, double e, double f, double g, double h, double i) {
double[] p1 = {a, b, c};
double[] p2 = {d, e, f};
double[] p3 = {g, h, i};
triangles.add(new TrianglePoints(p1, p2, p3));
}
}
I highly suspect your issue to be that in C++, doing (timestamp)
triTranslated = tri;
will in fact make a copy of the triangle, whereas your code merely gets a reference, therefore your triangles drift away at pace of 3 units per render, likely vanishing out of view before you could see anything.

Adding floor based on image or how to optimize pre-existing method

I'm creating a game with Java for a school project. I currently have a Player class and I am creating the floor he will walk on inside it. Right now, I just made a Clamp method where I add different sections based on the image so he walks on the 'floor'. Eventually, I have platforms that he needs to be able to jump on, but fall if he misses. That code is currently not working and I'm not sure how to go about it. Is there an easier or more efficient way to do the floor? If not, how can I make the platforms work?
I've already tried using a variable at the end of the method to test if it is a platform or not.
package com.sewd.sophomorevisitation.objects;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import com.sewd.sophomorevisitation.Background;
import com.sewd.sophomorevisitation.KeyListener;
import com.sewd.sophomorevisitation.Main;
import com.sewd.sophomorevisitation.sprites.SpriteAnimation;
import com.sewd.sophomorevisitation.sprites.SpriteSheet;
public class Player extends GameObject {
Handler handler;
static int times = 0;
private static BufferedImage[] shreccRun = {SpriteSheet.getSprite(0, 0, 128, 106, 0, 0), SpriteSheet.getSprite(1, 0, 128, 106, 0, 0), SpriteSheet.getSprite(0, 0, 128, 106, 0, 0), SpriteSheet.getSprite(2, 0, 128, 106, 0, 0)};
private static BufferedImage[] shreccJump = {SpriteSheet.getSprite(3, 0, 128, 106, 0, 0), SpriteSheet.getSprite(4, 0, 128, 106, 0, 0)};
public static SpriteAnimation shreccRunAnimation = new SpriteAnimation(shreccRun, 5);
public static SpriteAnimation shreccJumpAnimation = new SpriteAnimation(shreccJump, 5);
public static SpriteAnimation animation = shreccRunAnimation;
public Player(int x, int y, ID id) {
super(x, y, id);
}
public void tick() {
y += velY;
if(x < 0) {
death();
}
if(Main.started) {
/*floor(1076, 1215, -500, Main.screenSize.width, 0, 972);
floor(1215, 1374, -500, Main.screenSize.width, 0, 969);
floor(1374, 1538, -500, Main.screenSize.width, 0, 936);
floor(1538, 1667, -500, Main.screenSize.width, 0, 925);
floor(1667, 3711, -500, Main.screenSize.width, 0, 919);*/
floor(0, 473, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 40);
floor(473, 600, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 70);
floor(600, 775, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 90);
floor(775, 2900, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 100);
floor(2900, 2980, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 140);
floor(2980, 3060, -500, Main.screenSize.width, 0, (Main.screenSize.height/ 4) * 3 - 190);
floor(2980, 3060, -500, Main.screenSize.width, 0, 873);
floor(3060, 3073, -500, Main.screenSize.width, 0, 843);
floor(3073, 3827, -500, Main.screenSize.width, 0, 813);
floor(3827, 3857, -500, Main.screenSize.width, 0, 784);
floor(3827, 10000, -500, Main.screenSize.width, 0, 784);
}
if(animation != shreccJumpAnimation) {
animation.update();
}else {
if(times <= 5)animation.update();
times++;
}
}
public void death() {
System.out.println("Dead");
}
public static void floor(int start, int stop, int minX, int maxX, int minY, int maxY) {
if( start <= Background.x && Background.x < stop) Player.clamp(minX, maxX, minY, maxY);
}
public static void clamp(int minX, int maxX, int minY, int maxY) {
if(x <= minX) x = minX;
else if(x >= maxX) x = maxX;
else if(y <= minY) {
y = minY;
KeyListener.isInAir = true;
velY = -velY;
if(velY == 0) velY = 10;
}else if(y >= maxY) {
y = maxY;
KeyListener.isInAir = false;
if(times > 0) {
times = 0;
animation.stop();
animation.restart();
animation = shreccRunAnimation;
animation.start();
}
}else return;
}
public void render(Graphics g) {
g.drawImage(animation.getSprite(), x - shreccRun[0].getWidth() - 50, y, shreccRun[0].getWidth() * 2, shreccRun[0].getHeight() * 2, null);
}
}
The floor needs to work properly in all places where there are no platforms. He just walks on it. Otherwise, if he is landing on a platform he needs to stay on it until that platform ends, then he falls. Or, if he misses a platform then he falls.
Figured it out
public static boolean onFloor;
public void tick() {
if(Main.isStarted()) {
floor();
if(!onFloor) {
Player.setVelY(20);
}else {
Player.setVelY(0);
}
}
}
private void floor() {
clamp(0, 5000, 824, "f");
clamp(5000, 10000, 750, "p");
}
private void clamp(int start, int stop, int minY, String type) {
if(x > start && x < stop) {
if(Player.getY() != minY) {
if(Player.getY() > minY) {
if(type != "p") {
Player.setY(minY);
Player.setVelY(0);
onFloor = true;
return;
}
}
if(!Player.jumping) {
Player.setVelY(10);
onFloor = false;
}
}else if(Player.getY() == minY) {
Player.setY(minY);
Player.setVelY(0);
onFloor = true;
}else if(Player.getY() > minY) {
if(type != "p") {
Player.setY(minY);
Player.setVelY(0);
}
}
}
}

Cannot cast to java.lang.Comparable exception while trying to implement Dijkstra's Algorithm [duplicate]

This question already has answers here:
MyClass cannot be cast to java.lang.Comparable: java.lang.ClassCastException
(3 answers)
Closed 4 years ago.
I have implemented Dijkstra's Algorithm using PriorityQueue. However on running the code, I get the following exception :
Exception in thread "main" java.lang.ClassCastException: Dijkstra$Vertex cannot be cast to java.lang.Comparable
at java.util.PriorityQueue.siftUpComparable(Unknown Source)
at java.util.PriorityQueue.siftUp(Unknown Source)
at java.util.PriorityQueue.offer(Unknown Source)
at java.util.PriorityQueue.add(Unknown Source)
at Dijkstra.dijkstra(Dijkstra.java:55)
at Dijkstra.main(Dijkstra.java:89)
The code used is :
import java.util.HashSet;
import java.util.PriorityQueue;
public class Dijkstra {
static class Vertex{
private int vertexid;
private Double distance;
public Vertex(int vertexid, Double distance) {
this.vertexid = vertexid;
this.distance = distance;
}
public int getVertexid() {
return vertexid;
}
public Double getDistance() {
return distance;
}
public int compareTo(Vertex other) {
return this.getDistance().compareTo(other.getDistance());
}
public boolean equals(Object o) {
if (o instanceof Vertex) {
Vertex v = (Vertex) o;
return vertexid == v.vertexid && distance == v.distance;
}
return false;
}
}
public static void dijkstra(double g[][], int n, int m, int source) {
// g is the adjacency matrix
// n is the number of nodes
// m is the number of edges
// initialize shortest path
double d[] = new double[n];
d[source] = 0;
for (int i = 0; i < n; i++) {
d[i] = Double.POSITIVE_INFINITY;
}
HashSet<Integer> s = new HashSet<Integer>();
PriorityQueue<Vertex> q = new PriorityQueue<Vertex>();
// initialize q
for (int i = 0; i < n; i++) {
q.add(new Vertex(i, d[i]));
}
Vertex u;
while (!q.isEmpty()) {
u = q.remove();
System.out.println(u.getVertexid() + "\t" + u.getDistance());
s.add(u.getVertexid());
for (int i = 0; i < n; i++) {
if (i != u.getVertexid() && g[u.getVertexid()][i] != Double.POSITIVE_INFINITY) {
if (u.getDistance().doubleValue() + g[u.getVertexid()][i] < d[i]) {
q.remove(new Vertex(i, d[i]));
d[i] = u.getDistance().doubleValue() + g[u.getVertexid()][i];
q.add(new Vertex(i, d[i]));
}
}
}
}
}
public static void main(String[] args) {
double graph[][] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
Dijkstra.dijkstra(graph, 8, 14, 0);
}
}
The Vertex class is used to create a structure that basically stores the vertex and its label distance. The priority queue will work on objects of this class, and will use the distance value as the ordering value for remove operations. How to rectify the exception?
Try this instead:
static class Vertex implements Comparable<Vertex> {

Shortest path in a 2d array using Dijkstra's algorithm?

This is my first time implementing Dijkstra's algorithm. Okay so here I have a simple 2D 9 by 9 array:
Starting point is 1 and we're trying to get to any green square. Red squares are walls or lava (whatever satisfies your imagination).
How do I implement this in Java?
Computer science is not my field hence I'm not a seasoned programmer so I might not know how to do some stack pushing, only loops and recursion :( please keep it easy as possible and bear with me!
Here's something similiar that should get you started. However, the solution presented below attempts to get to the bottom right corner. You can relax that condition to find the bottom row. You will also need to change the encoding slightly to have a unique value that represents this row.
public class MazeSolver {
final static int TRIED = 2;
final static int PATH = 3;
// #formatter:off
private static int[][] GRID = {
{ 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1 },
{ 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0 },
{ 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1 },
{ 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
// #formatter:off
public static void main(String[] args) {
MazeSolver maze = new MazeSolver(GRID);
boolean solved = maze.solve();
System.out.println("Solved: " + solved);
System.out.println(maze.toString());
}
private int[][] grid;
private int height;
private int width;
private int[][] map;
public MazeSolver(int[][] grid) {
this.grid = grid;
this.height = grid.length;
this.width = grid[0].length;
this.map = new int[height][width];
}
public boolean solve() {
return traverse(0,0);
}
private boolean traverse(int i, int j) {
if (!isValid(i,j)) {
return false;
}
if ( isEnd(i, j) ) {
map[i][j] = PATH;
return true;
} else {
map[i][j] = TRIED;
}
// North
if (traverse(i - 1, j)) {
map[i-1][j] = PATH;
return true;
}
// East
if (traverse(i, j + 1)) {
map[i][j + 1] = PATH;
return true;
}
// South
if (traverse(i + 1, j)) {
map[i + 1][j] = PATH;
return true;
}
// West
if (traverse(i, j - 1)) {
map[i][j - 1] = PATH;
return true;
}
return false;
}
private boolean isEnd(int i, int j) {
return i == height - 1 && j == width - 1;
}
private boolean isValid(int i, int j) {
if (inRange(i, j) && isOpen(i, j) && !isTried(i, j)) {
return true;
}
return false;
}
private boolean isOpen(int i, int j) {
return grid[i][j] == 1;
}
private boolean isTried(int i, int j) {
return map[i][j] == TRIED;
}
private boolean inRange(int i, int j) {
return inHeight(i) && inWidth(j);
}
private boolean inHeight(int i) {
return i >= 0 && i < height;
}
private boolean inWidth(int j) {
return j >= 0 && j < width;
}
public String toString() {
String s = "";
for (int[] row : map) {
s += Arrays.toString(row) + "\n";
}
return s;
}
}
I would suggest you start with writing down a method of applying dijkstras algorithm (assuming you know it in the first place) here in natural language and then start transforming it to your programming language.
The basic questions you will need to answer for that:
What are the nodes?
What are the connections?
What is the weight of each connection?
Once you did this you should be able to find a (probably not efficient) solution.
The optimal solution is indeed to use Dijkstra or AStar with a different finish condition. So you need to write if(targetNodes.contains(u)) break; instead of if(target == u) break;
(see wikipedia: If we are only interested in a shortest path between vertices source and target, we can terminate the search at line 13 if u = target.)
This is already implemented in my project called ... oh is this homework ;) ?

Java for-loop problem

The problem is that I am trying to make certain tiles blocked so the player cannot walk on them. However, it's only reading the FIRST tile which is board[0][0] and everything else is not checked....
What am I doing wrong? :(
Here's my code:
import java.applet.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.Canvas.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.*;
public class gen extends Applet implements KeyListener {
Image[] tiles;
Image player;
int x;
int y;
int px;
int py;
boolean left;
boolean right;
boolean down;
boolean up;
int[][] board;
final int NUM_TILES = 5;
public final int BLOCKED = 1;
public void init() {
// Load board
board = loadBoard();
tiles = new Image[NUM_TILES];
for(int i = 0;i < NUM_TILES;i++) {
tiles[i] = getImage(getClass().getResource(String.format("tile%d.png", i)));
}
for (int y=0;y< NUM_TILES;y++) {
board[1][1] = BLOCKED;
board[1][2] = BLOCKED;
board[1][3] = BLOCKED;
}
player = getImage(getClass().getResource("player.png")); // our player
addKeyListener(this);
px = 0;
py = 0;
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = true;
px = px - 32;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = true;
px = px + 32;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = true;
py = py + 32;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = true;
py = py - 32;
}
repaint();
}
public void keyReleased(KeyEvent e) {
} // ignore
public void keyTyped(KeyEvent e) {
} // ignore
public void paint(Graphics g) {
x = 0;
y = 0;
// here's a sample map!
// but we're loading from a text file!
// so... why is this needed?
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
int index = board[row][col];
g.drawImage(tiles[index], 32 * col, 32 * row, this);
if (board[row][col] == BLOCKED) {
System.out.println("\n"+board[row][col] + "is BLOCKED!\n");
}else{System.out.println("\n"+board[row][col] + "is NOT Blocked!\n");}
}
}
g.drawImage(player, px, py, this);
try {
System.out.println("ON BLOCKED TILE?: " + blocked(px,py) + "\n");
}catch(ArrayIndexOutOfBoundsException e) {}
} // end paint method
public void update(Graphics g) {
paint(g);
}
private int[][] loadBoard() {
int[][] board = {
{ 0, 1, 2, 3, 4, 4, 3, 4 },
{ 0, 1, 2, 3, 4, 4, 3, 4 },
{ 2, 2, 4, 2, 2, 1, 1, 0 },
{ 0, 1, 2, 3, 4, 4, 3, 4 },
{ 0, 0, 0, 2, 3, 4, 4, 2 },
{ 2, 2, 4, 2, 2, 1, 1, 0 },
{ 0, 1, 2, 3, 4, 4, 3, 4 },
{ 0, 0, 0, 2, 3, 4, 4, 2 }
};
return board;
}
public boolean blocked(int tx, int ty) {
return board[tx][ty] == BLOCKED;
}
} // end whole thing
One of the problems in your code is on this line you are calling blocked with px, py which are multiples of 32:
blocked(px, py)
But you use these number as indexes into your array which would cause an ArrayIndexOutOfBoundsException:
public boolean blocked(int tx, int ty)
{
return board[tx][ty] == BLOCKED;
}
Which you've tried to "fix" by ignoring it:
try
{
System.out.println("ON BLOCKED TILE?: " + blocked(px,py) + "\n");
}
catch(ArrayIndexOutOfBoundsException e) {}
So I suspect that it only works for (0,0) because (32,32) is out of bounds. There are also other errors in your code, but this should be a good start for you.

Categories

Resources