Hallo zusammen
Ich bin es nochmal. Beim Verbessern der Objektorientierung bin ich nun auf folgenden Fehler gestoßen, welcher für mich keinen wirklichen Sinn ergibt. Der Fehler sei eine Cast Exception. Ich wüsste aber nicht, dass ich irgendwo caste. Zudem wird keine Zeile angegeben, in welcher der Fehler ist. Ich bitte um eure Hilfe. Hier ist erstmal der Fehler:
Ich bin es nochmal. Beim Verbessern der Objektorientierung bin ich nun auf folgenden Fehler gestoßen, welcher für mich keinen wirklichen Sinn ergibt. Der Fehler sei eine Cast Exception. Ich wüsste aber nicht, dass ich irgendwo caste. Zudem wird keine Zeile angegeben, in welcher der Fehler ist. Ich bitte um eure Hilfe. Hier ist erstmal der Fehler:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
at java.desktop/javax.swing.LayoutComparator.compare(LayoutComparator.java:96)
at java.desktop/javax.swing.LayoutComparator.compare(LayoutComparator.java:49)
at java.desktop/javax.swing.SortingFocusTraversalPolicy.mergeSort(SortingFocusTraversalPolicy.java:650)
at java.desktop/javax.swing.SortingFocusTraversalPolicy.mergeSort(SortingFocusTraversalPolicy.java:665)
at java.desktop/javax.swing.SortingFocusTraversalPolicy.mergeSort(SortingFocusTraversalPolicy.java:664)
at java.desktop/javax.swing.SortingFocusTraversalPolicy.mergeSort(SortingFocusTraversalPolicy.java:664)
at java.desktop/javax.swing.SortingFocusTraversalPolicy.mergeSort(SortingFocusTraversalPolicy.java:664)
at java.desktop/javax.swing.SortingFocusTraversalPolicy.mergeSort(SortingFocusTraversalPolicy.java:664)
at java.desktop/javax.swing.SortingFocusTraversalPolicy.legacySort(SortingFocusTraversalPolicy.java:161)
at java.desktop/javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:150)
at java.desktop/javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(SortingFocusTraversalPolicy.java:123)
at java.desktop/javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:476)
at java.desktop/javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:172)
at java.desktop/javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:569)
at java.desktop/java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:174)
at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:494)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4877)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4828)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:98)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744)
at java.desktop/java.awt.SequencedEvent.dispatch(SequencedEvent.java:206)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:98)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Java:
package net.tim.gui;
import javax.swing.*;
public class Frame extends JFrame {
public Frame() {
setTitle("Tower Defense");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
add(new GamePanel());
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
Java:
package net.tim.gui;
import javax.swing.*;
import java.awt.*;
public class GamePanel extends JPanel {
//screen Settings
public static final int maxCol = 15;
public static final int maxRow = 10;
final int TileSize = 75;
final int screenWidth = maxCol * TileSize;
final int screenHeight = maxRow * TileSize;
//Tiles
Tile[][] tile = new Tile[maxRow][maxCol];
public GamePanel() {
setPreferredSize(new Dimension(screenWidth, screenHeight));
setBackground(Color.BLACK);
setLayout(new GridLayout(maxRow, maxCol));
setFocusable(true);
//create tiles
int col = 0;
int row = 0;
while (col < maxCol && row < maxRow) {
tile[row][col] = new Tile(col, row);
this.add(tile[row][col]);
col++;
if (col == maxCol) {
col = 0;
row++;
}
}
}
}
Java:
package net.tim.gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Tile extends JButton implements ActionListener {
Tile parent;
int col;
int row;
int gCost;
int hCost;
int fCost;
boolean start;
boolean goal;
boolean solid;
boolean open;
boolean checked;
public Tile(int col, int row) {
this.col = col;
this.row = row;
setBackground(Color.WHITE);
setForeground(Color.BLACK);
addActionListener(this);
}
public void setAsStart() {
start = true;
setText("Start");
}
public void setAsGoal() {
goal = true;
setText("Goal");
}
public void setAsSolid() {
solid = true;
}
public void setAsOpen() {
open = true;
}
public void setAsChecked() {
checked = true;
}
public void setAsPath() {
}
@Override
public void actionPerformed(ActionEvent e) {
}
//Getters and Setters
@Override
public Tile getParent() {
return parent;
}
public void setParent(Tile parent) {
this.parent = parent;
}
public int getCol() {
return col;
}
public void setCol(int col) {
this.col = col;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getgCost() {
return gCost;
}
public void setgCost(int gCost) {
this.gCost = gCost;
}
public int gethCost() {
return hCost;
}
public void sethCost(int hCost) {
this.hCost = hCost;
}
public int getfCost() {
return fCost;
}
public void setfCost(int fCost) {
this.fCost = fCost;
}
public boolean isStart() {
return start;
}
public void setStart(boolean start) {
this.start = start;
}
public boolean isGoal() {
return goal;
}
public void setGoal(boolean goal) {
this.goal = goal;
}
public boolean isSolid() {
return solid;
}
public void setSolid(boolean solid) {
this.solid = solid;
}
public boolean isOpen() {
return open;
}
public void setOpen(boolean open) {
this.open = open;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
}
Java:
package net.tim.pathfinding;
import net.tim.gui.Tile;
import java.util.ArrayList;
import java.util.List;
import static net.tim.gui.GamePanel.maxCol;
import static net.tim.gui.GamePanel.maxRow;
public class Pathfinding {
Tile[][] tile;
Tile startTile, goalTile, currentTile;
List<Tile> openList = new ArrayList<>();
List<Tile> checkedList = new ArrayList<>();
//other
boolean goalReached = false;
public Pathfinding(Tile[][] tile) {
this.tile = tile;
}
public void autoSearch() throws RuntimeException {
int step = 0;
while (!goalReached && step < 1000) {
search();
step++;
}
if (goalReached) {
System.out.println("Goal reached");
} else {
throw new RuntimeException("No path found");
}
}
private void search(){
if (!goalReached) {
int col = currentTile.getCol();
int row = currentTile.getRow();
currentTile.setAsChecked();
checkedList.add(currentTile);
openList.remove(currentTile);
//open the node left
if (col -1 >= 0) {
openNode(tile[row][col - 1]);
}
//open the node above
if (row -1 >= 0) {
openNode(tile[row - 1][col]);
}
//open the node right
if (col + 1 < maxCol) {
openNode(tile[row][col + 1]);
}
//open the node below
if (row + 1 < maxRow) {
openNode(tile[row + 1][col]);
}
int bestNodeIndex = -1;
int bestNodeFCost = Integer.MAX_VALUE;
for (int i = 0; i < openList.size(); i++) {
if (openList.get(i).getfCost() < bestNodeFCost) {
bestNodeFCost = openList.get(i).getfCost();
bestNodeIndex = i;
} else if (openList.get(i).getfCost() == bestNodeFCost) {
if (openList.get(i).getgCost() < openList.get(bestNodeIndex).getgCost()) {
bestNodeIndex = i;
}
}
}
if (bestNodeIndex == -1) {
throw new RuntimeException("No best node found most likely no path");
}
currentTile = openList.get(bestNodeIndex);
if (currentTile == goalTile) {
goalReached = true;
trackThePath();
}
}
}
private void setStartNode(int col, int row) {
tile[row][col].setAsStart();
startTile = tile[row][col];
currentTile = startTile;
}
private void setGoalNode(int col, int row) {
tile[row][col].setAsGoal();
goalTile = tile[row][col];
}
private void setSolidNode(int col, int row) {
tile[row][col].setAsSolid();
}
private void setCostOnNodes() {
int col = 0;
int row = 0;
while (col < maxCol && row < maxRow) {
getCost(tile[row][col]);
col++;
if (col == maxCol) {
col = 0;
row++;
}
}
}
private void getCost(Tile tile){
//gCost
int xDistance = Math.abs(tile.getCol() - startTile.getCol());
int yDistance = Math.abs(tile.getRow() - startTile.getRow());
tile.setgCost(xDistance + yDistance);
//hCost
xDistance = Math.abs(tile.getCol() - goalTile.getCol());
yDistance = Math.abs(tile.getRow() - goalTile.getRow());
tile.sethCost(xDistance + yDistance);
//fCost
tile.setfCost(tile.getgCost() + tile.gethCost());
//display costs
if (tile != startTile && tile != goalTile) {
tile.setText("<html>F: " + tile.getfCost() + "<br>G: " + tile.getgCost() + "</html>");
}
}
private void openNode(Tile tile) {
if (!tile.isOpen() && !tile.isChecked() && !tile.isSolid()) {
tile.setAsOpen();
tile.setParent(currentTile);
openList.add(tile);
}
}
private void trackThePath() {
Tile parent = goalTile.getParent();
while (parent != startTile) {
parent.setAsPath();
parent = parent.getParent();
}
}
}