I simply don't understand where all of those errors are coming from:
public class GameScreen extends JPanel {
private int Width,Height,Gwidth,Gheight;//JFrame width and height and the game screen container widh and height
private int Mx, My;// dragged mouse coordinates
public static Player pl;
public static Wave w;
private Map map;
private PathFinding dijkstra;
private DepthFirstSearch dfs;
private Path Enemypaths[];
private boolean Redirect, BlockedPath, Pause, Hold, PathFound;
private LinkedList<Point> Menu;// unit selection menu
private int SelectedUnit;
private int Clock;
private DrawGameScreen DGS;
public static int CubeSize = 40;
public GameScreen(int Width, int Height)
{
this.Width = Width;
this.Height = Height;
Gwidth = 1000;
Gheight = 500;
pl = new Player();
w = new Wave();
Enemypaths = new Path[50];
Menu = new LinkedList<Point>();
Pause = false;
SelectedUnit = -1;
Clock = 0;
Menu.add(new Point(60,540));//in those points we put the unit selection menu
Menu.add(new Point(120,540));
Menu.add(new Point(180,540));
for(int i = 0; i < Enemypaths.length; i )// initializing enemy paths
Enemypaths[i] = new Path();
map = new Map(pl.getUnits());
dijkstra = new PathFinding(map);
PathFound = true;
dfs = new DepthFirstSearch(map);
Redirect = true;
BlockedPath = dfs.Traverse();
DGS = new DrawGameScreen();
this.setLayout(new BorderLayout());
this.add(DGS,BorderLayout.CENTER);
addMouseListener(new ClickListener());
addMouseMotionListener(new ClickListener());
Hold = false;
gameStart();
Clock();
}
public void StartNewGame()
{
pl.getUnits().clear();
pl.setLife(20);
pl.setMoney(10);
Wave.WaveId = 0;
Clock = 0;
for(int i = 0; i < 15; i )
{
for(int j = 0; j < 25; j )
{
map.UpdateTile(j, i, 0);
dfs.UpdateTile(j, i, 0);
dijkstra.UpdateTile(j, i, 0);
}
}
}
public void gameStart()
{
Thread gameThread = new Thread()
{
public void run()
{
while(true)// to make sure our game thread wont stop.
{
if(!Pause)
{
if(pl.getLife() > 0 )// not a game over
{
if(Clock == 1) w.NewWave(5, 25);// starting our enemy waves with 5 enemies at the start
if(PathFound)
{
repaint();
if(w.AddEnemy(pl))// checking if we have an active wave of enemies before adding more.
{// for each new enemy summoned by the thread:
Enemy e = w.getEnemeis().get(w.getEnemyCount() - 1);
Enemypaths[w.getEnemyCount() - 1].setP(new LinkedList<Point>(dijkstra.FindShortestPath((int)e.getEx()/CubeSize, (int)e.getEy()/CubeSize)));// find the shortest path for that enemy
e.setMoves(Enemypaths[w.getEnemyCount() - 1].getP());//set this path as his path.
}
for(int i = 0; i <w.getEnemyCount(); i )//making each enemy move
{
Enemy e = w.getEnemeis().get(i);
if(e != null) e.Move();
}
if(Redirect)// redirecting the enemy path if found a shorter way
{
PathFinding();
Redirect = false;
}
for(int i = 0; i < pl.getUnits().size(); i )// making each unit shoot an enemy
{
Unit u = pl.getUnits().get(i);
if(u != null) u.Shoot(w.getEnemeis(), pl);
}
}
}
}
}
}
};
gameThread.start();
}
public void PathFinding()
{
Thread dijkstraThread = new Thread()
{
public void run()
{
PathFound = false;
for(Enemy e : w.getEnemeis())
{
if(e != null)
{
if(e.getType() == 1 || e.getType() == 3)
{
//Enemy e = w.getEnemeis().get(w.getEnemyCount() - 1);
Enemypaths[w.getEnemyCount() - 1].setP(new LinkedList<Point>(dijkstra.FindShortestPath((int)e.getEx()/CubeSize, (int)e.getEy()/CubeSize)));// find the shortest path for that enemy
e.setMoves(Enemypaths[w.getEnemyCount() - 1].getP());
}
}
}
PathFound = true;
}
};
dijkstraThread.start();
}
public void Clock()
{
Thread T = new Thread()
{
public void run()
{
while (true)
{
if (!Pause && pl.getLife() > 0)
Clock ;
try
{
Thread.sleep(1000);
}
catch (InterruptedException ex) {}
}
}
};
T.start();
}
class DrawGameScreen extends JPanel
{
Unit u;
Enemy e;
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(Toolkit.getDefaultToolkit().getImage("GameAssets/BackGroundMap.png"),0,0,Gwidth,Gheight,this);// drawing the background
g.drawImage(Toolkit.getDefaultToolkit().getImage("GameAssets/Enemy Base.png"),0,7*CubeSize,this);//drawing the player and enemy bases
g.drawImage(Toolkit.getDefaultToolkit().getImage("GameAssets/Enemy Base.png"),24*CubeSize,7*CubeSize,this);
//drawing game components
DrawGrid(g);
DrawUnits(pl,g);
DrawEnemies(w,g);
DrawUnitPlacement(Mx,My,g);
DrawIcons(g);
//drawing prices
g.setColor(Color.BLACK);
g.setFont(new Font("Arial",Font.BOLD,16));
g.drawString("5",60,600);
g.drawString("20",120,600);
g.drawString("50",180,600);
//drawing player info
g.drawString("money: " pl.getMoney(),300,545);
g.drawString("life: " pl.getLife(),300,565);
//drawing game info
g.setFont(new Font("Arial",Font.BOLD,50));
g.drawString("Wave: " Wave.getWaveId(),450,580);
g.drawString("Timer: " (Clock/10) "." (Clock),475,600);
if(Clock < 10)
{
g.setFont(new Font("Arial",Font.BOLD,150));
g.drawString("New Game!",200,250);
}
if(pl.getLife() <= 0)//Game Over accrued
{
g.setColor(Color.RED);
g.setFont(new Font("Arial",Font.BOLD,105));
g.drawString("Game Over!",200,250);
g.setFont(new Font("Arial",Font.BOLD,80));
g.drawString("Score: " pl.getMoney(),200,150);
}
}
public void DrawGrid(Graphics g)
{
g.setColor(Color.GRAY);
for(int i = CubeSize; i <= Gheight; i = CubeSize)
g.drawLine(0,i,Gwidth,i);// drawing the lines of the grid from left to right
for(int i = CubeSize; i <= Gwidth; i = CubeSize)
g.drawLine(i,0,i,Gheight);// drawing the lines of the grid from down to MaxHeight.
}
public void DrawUnits(Player pl,Graphics g)
{
for(int i = 0; i < pl.getUnits().size(); i )// drawing the units on the screen in the correct position
{
u = pl.getUnits().get(i);
u.drawBullet(g, this);
g.drawImage(u.getImage(),u.getX()*CubeSize,u.getY()*CubeSize,CubeSize,CubeSize,this);
}
}
public void DrawEnemies(Wave w,Graphics g) //drawing the enemies on screen with health bars.
{
for(int i = 0; i < w.getEnemyCount(); i )
{
e = w.getEnemeis().get(i);
if(e != null)
{
int healthbar = (int)e.getHp();
int losthealth = (int)e.getMaxHp();
g.drawImage(e.getImage(),(int)e.getEx(),(int)e.getEy(),CubeSize,CubeSize,this);
g.setColor(Color.RED);
g.fillRect((int)e.getEx(),(int)e.getEy()-5,losthealth*CubeSize,5);
g.setColor(Color.GREEN);
g.fillRect((int)e.getEx(),(int)e.getEy()-5,healthbar*CubeSize,5);
}
}
}
public void DrawUnitPlacement(int Mx, int My, Graphics g) //Checking if we can place a unit in this location
{
if(Mx < Gwidth && My < Gheight)
{
if(Mx/CubeSize == 0 && My/CubeSize == 7 || Mx/CubeSize == 24 && My/CubeSize == 7)
BlockedPath = true;//wont allow to place units in the start or the finish of the map.
else
{
dfs.ResetVisits();
if(dfs.Nodes[My/CubeSize][Mx/CubeSize].getType() == 0)//if the mouse is over a clear tile
{
dfs.UpdateTile(Mx/CubeSize, My/CubeSize, 1);//Set the tile that we want to place our unit in as blocked
BlockedPath = dfs.Traverse();//traverse through the graph with our wanted tile as blocked to check if there is a way to bypass it
dfs.UpdateTile(Mx/CubeSize, My/CubeSize, 0);// return the tile to its original state to make sure it wont intervene with the enemies path finding.
}
else BlockedPath = true;//if the mouse is over a blocked tile(type == 1)
}
for(int i = 0; i < Menu.size(); i ) //setting the mouse courser to Green or Red depending on the tile for placement.
{ // also drawing the dragging animation for the unit.
if(SelectedUnit != 0)// checking if a unit was chosen
{
if(My < Gheight && Mx < Gwidth)// in bounds of the game screen
{
if(map.GetType(Mx/CubeSize, My/CubeSize) == 0 && !BlockedPath) g.setColor(Color.GREEN);
else g.setColor(Color.RED);
g.fillRect((Mx/CubeSize)*CubeSize,My*CubeSize,CubeSize,CubeSize);// painting the block
}
if(SelectedUnit == 1) g.drawImage(Toolkit.getDefaultToolkit().getImage("GameAssets/Artillery.png"),Mx*CubeSize,My*CubeSize,CubeSize,CubeSize,this);
if(SelectedUnit == 2) g.drawImage(Toolkit.getDefaultToolkit().getImage("GameAssets/Rockets.png"),Mx*CubeSize,My*CubeSize,CubeSize,CubeSize,this);
if(SelectedUnit == 3) g.drawImage(Toolkit.getDefaultToolkit().getImage("GameAssets/AoeGun.png"),Mx*CubeSize,My*CubeSize,CubeSize,CubeSize,this);
}
}
}
}
public void DrawIcons(Graphics g)// drawing paused, resume and new game icons
{
if(Pause) {
g.drawImage(Toolkit.getDefaultToolkit().getImage("GameAssets/resume.png"),700,540,this);
}
else {
g.drawImage(Toolkit.getDefaultToolkit().getImage("GameAssets/resume.png"),700,540,this);
}
g.drawImage(Toolkit.getDefaultToolkit().getImage("GameAssets/NewGame.png"),800,540,this);
}
@Override
public Dimension getPreferredSize()
{
return (new Dimension(Width,Height));
}
}
class ClickListener extends MouseAdapter
{
public void MousePressed(MouseEvent e)
{
if (e.getX() >= 800 && e.getX() <= 960 && e.getY() >= 550 && e.getY() <= 610)
if(pl.getLife() > 0)// don't do these actions unless the game is still going on
{
if (e.getX() >= 700 && e.getX() <= 765 && e.getY() >= 540 && e.getY() <= 620)// setting click pause
{
if (Pause) Pause = false;
else Pause = true;
}
for(int i = 0; i < Menu.size(); i )// selecting a unit from the unit menu
{
if(e.getX() >= Menu.get(i).getX() && e.getX() <= Menu.get(i).getX() CubeSize && e.getY() >= Menu.get(i).getY() && e.getY() <= Menu.get(i).getY() CubeSize)
{
SelectedUnit = i;
Hold = true;
updateLocation(e);
break;
}
else Hold = false;
}
}
}
public void mouseDragged(MouseEvent e)// dragging the unit into the game screen
{
if(Hold) updateLocation(e);
}
public void mouseReleased(MouseEvent e)// releasing units into the game screen
{
if(SelectedUnit != -1 && pl.getLife() > 0)
{
if(My <= Gheight && Mx <= Gwidth)
{
if(map.GetType(Mx/CubeSize, My/CubeSize) == 0 && !BlockedPath)
{
if(SelectedUnit == 0 && pl.getMoney() >= 5)// if an artillery is selected
{
pl.setMoney(pl.getLife() - 5);
pl.getUnits().add(new Artillery(Mx/CubeSize,My/CubeSize));
Redirect = true;
map.UpdateTile(Mx/CubeSize, My/CubeSize, 1);
dfs.UpdateTile(Mx/CubeSize, My/CubeSize, 1);
dijkstra.UpdateTile(Mx/CubeSize, My/CubeSize, 1);
}
if(SelectedUnit == 1 && pl.getMoney() >= 20)// if an artillery is selected
{
pl.setMoney(pl.getLife() - 20);
pl.getUnits().add(new Rockets(Mx/CubeSize,My/CubeSize));
Redirect = true;
map.UpdateTile(Mx/CubeSize, My/CubeSize, 1);
dfs.UpdateTile(Mx/CubeSize, My/CubeSize, 1);
dijkstra.UpdateTile(Mx/CubeSize, My/CubeSize, 1);
}
}
}
}
Hold = false;
Mx = 0;
My = 0;
}
public void updateLocation(MouseEvent e)// save dragged mouse coordinates
{
Mx = e.getX();
My = e.getY();
}
}
}
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
at java.base/java.util.Objects.checkIndex(Objects.java:359)
at java.base/java.util.ArrayList.get(ArrayList.java:427)
at gameRelated.GameScreen$DrawGameScreen.DrawEnemies(GameScreen.java:271)
at gameRelated.GameScreen$DrawGameScreen.paintComponent(GameScreen.java:214)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1074)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1083)
at java.desktop/javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
at java.desktop/javax.swing.JComponent.paintChildren(JComponent.java:907)
at java.desktop/javax.swing.JComponent.paintToOffscreen(JComponent.java:5271)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBufferedImpl(RepaintManager.java:1643)
at java.desktop/javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1618)
at java.desktop/javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1556)
at java.desktop/javax.swing.RepaintManager.paint(RepaintManager.java:1323)
at java.desktop/javax.swing.JComponent.paint(JComponent.java:1060)
at java.desktop/java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at java.desktop/sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:75)
at java.desktop/sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:112)
at java.desktop/java.awt.Container.paint(Container.java:2003)
at java.desktop/java.awt.Window.paint(Window.java:3949)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:876)
at java.desktop/javax.swing.RepaintManager$4.run(RepaintManager.java:848)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)
at java.desktop/javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)
at java.desktop/javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)
at java.desktop/javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1884)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
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)
all of those errors repeat them self like 3 more times in a row for some reason.
CodePudding user response:
As Jim Garrison said in comments it seems that your enemies list is empty. Although you add enemies, you call the function repaint before adding enemies. And in paint you call DrawEnemies with the empty list.
CodePudding user response:
"Blockquote Exception in thread "AWT-EventQueue-0""
This error means that your .exe can't find the class. This is probably due to the way you exported your project as a runnable JAR.
"java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 at"
Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range
https://docs.oracle.com/javase/8/docs/api/java/lang/IndexOutOfBoundsException.html