Friday, 6 September 2013

Only one image shows up when i load a list of images

Only one image shows up when i load a list of images

I am writing a tower defense game, and I need to place a list of JPanels
with Buffered images loaded onto them onto a JFrame. I am using a for loop
to place the JPanels onto the JFrame, however, when I run the program,
only the last JPanel in the list shows up. I have checked to make sure
that the images aren't being placed onto one another, and they are being
placed in three separate locations.
here is the code for my class which creates a JFrame:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class GameScreen extends JPanel
implements MouseListener {
private ArrayList< JPanel > images;
private TDGame currentGame;
private JFrame frame;
private static final int width = 700;
private static final int length = 700;
public void mouseExited(MouseEvent e) {
this.repaint();
}

public void mouseEntered(MouseEvent e) {
this.repaint();
}

public void mouseReleased(MouseEvent e) {
this.repaint();
}

public void mousePressed(MouseEvent e) {
this.repaint();
}

public void mouseClicked(MouseEvent e) {
this.repaint();
}

public GameScreen() {
super();
images = new ArrayList<JPanel>();
currentGame = new TDGame();
frame = new JFrame("Tower Defense");
frame.setSize(width, length);
frame.setVisible(true);
frame.pack();
frame.getContentPane().add(this,BorderLayout.CENTER);
currentGame.EnemyForce.add(currentGame.EnemyForce.size(),
new BasicEnemy(0, 0, LoadImage()));
currentGame.EnemyForce.add(currentGame.EnemyForce.size(),
new BasicEnemy(250, 250, LoadImage2()));
currentGame.EnemyForce.add(currentGame.EnemyForce.size(),
new BasicEnemy(0, 0, LoadImage1()));
this.repaint();
}
public BufferedImage LoadImage() {
BufferedImage img = null;
try {
img = ImageIO.read(new File("Saturn-Intruder.png"));
} catch (IOException e) {
}
return img;
}

public BufferedImage LoadImage1() {
BufferedImage img = null;
try {
img = ImageIO.read(new File("Level1-TD.jpeg"));
} catch (IOException e) {
}
return img;
}

public BufferedImage LoadImage2() {
BufferedImage img = null;
try {
img = ImageIO.read(new File("BlackBird-Intruder.jpeg"));
} catch (IOException e) {
}
return img;
}
@Override
protected void paintComponent(Graphics g) {
for(int i = 0; i < currentGame.Defense.size(); i++) {
frame.add(currentGame.Defense.get(i));
}
for(int i = 0; i < currentGame.EnemyForce.size(); i++) {
frame.add( currentGame.EnemyForce.get(i));
}
for(int i = 0; i < currentGame.ShotsFired.size(); i++) {
frame.add(currentGame.ShotsFired.get(i));
}
}
}
Here is my code for the JPanels being placed onto the JFrame:
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
public class BasicEnemy extends Entity {
JPanel ePanel;
Graphics g;
public BasicEnemy(double x, double y, BufferedImage image) {
super(x, y, image);
ePanel = new JPanel();
ePanel.setVisible(true);
}
protected void paintComponent(Graphics g) {
g.drawImage(getImage(), (int)(getXCoord()),
(int)(getYCoord()), null);
}
}
How could I fix this problem so that all the images are being placed, not
only one?

No comments:

Post a Comment