I need to generate a map according to a txt file that I get as input. Each font in my txt file corresponds to a terrain type of the map. I've created Image
arrays that will store the images I can use for each type of terrain and draw those images in the Jframe. I already checked and the path of the images are correct, but as I call the onPaint () method several times during program execution, the map ends up being erased. I need to draw the map only once, and do that, move an avatar over it. How do I draw the map, leave it mounted and just redraw the avatar as it moves?
Follow my code:
public class MapaInterface extends JPanel implements ActionListener {
BuscaHeuristicaGameOfThrones busca = new BuscaHeuristicaGameOfThrones();
private Image fundo;
private Image explosao = new ImageIcon("src/res/exp1.png").getImage();
private Image agua = new ImageIcon("/src/res/agua.png").getImage();
public static ArrayList<Image> grama = new ArrayList<>();
public static ArrayList<Image> terra = new ArrayList<>();
public static Agente daenerys;
private Timer timer;
FileReader arquivo = null;
BufferedReader leitor;
String linha = "";
public MapaInterface() {
try {
arquivo = new FileReader("mapaInvertido.txt");
leitor = new BufferedReader(arquivo);
} catch (FileNotFoundException ex) {
Logger.getLogger(MapaInterface.class.getName()).log(Level.SEVERE, null, ex);
}
ImageIcon referencia = new ImageIcon("src/res/mapaGot2.png");
fundo = referencia.getImage();
timer = new Timer(1, this);
preencheArrayDeImagens();
timer.start();
daenerys = new Agente();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graficos = (Graphics2D) g;
Random random = new Random();
int numAleatorio = random.nextInt(23);
int x = 0, y = 0;
String tipo = "";
graficos.drawImage(new ImageIcon("src/res/terrinha.png").getImage(), x,y, this);
try {
linha = leitor.readLine();
StringTokenizer tokenizer;
while(linha != null){
tokenizer = new StringTokenizer(linha, ",");
while(!tipo.equals("|")){
tipo = tokenizer.nextToken();
if (tipo.equals("T")) {
graficos.drawImage(terra.get(numAleatorio), x, y, null);
g.dispose();
}
if (tipo.equals("G")) {
graficos.drawImage(grama.get(numAleatorio), x, y, null);
}
if (tipo.equals("A")) {
graficos.drawImage(agua, x, y, null);
}
x += 16;
}
y+=16;
linha = leitor.readLine();
}
} catch (IOException ex) {
Logger.getLogger(MapaInterface.class.getName()).log(Level.SEVERE, null, ex);
}
graficos.drawImage(daenerys.getImagem(), daenerys.getPosicaoMI().getX(), daenerys.getPosicaoMI().getY(), this);
if (daenerys.chegouDestino == true) {
graficos.drawImage(explosao, daenerys.getPosicaoMI().getX(), daenerys.getPosicaoMI().getY(), this);
daenerys.chegouDestino = false;
}
g.dispose();
}
@Override
public void actionPerformed(ActionEvent e) {
if (daenerys.destinoMuralha == true) {
if (daenerys.chegouMuralha == false) {
daenerys.movimentaAgenteMuralha();
repaint();
} else {
}
} else {
daenerys.movimentaAgente();
}
repaint();
}
public static Agente getDaenerys() {
return daenerys;
}
public static void setDaenerys(Agente daenerys) {
MapaInterface.daenerys = daenerys;
}
public void preencheArrayDeImagens() {
Image blocoTerra = new ImageIcon("src/res/cogumeloterra.png").getImage();
terra.add(blocoTerra);
blocoTerra = new ImageIcon("src/res/fogueira.png").getImage();
terra.add(blocoTerra);
blocoTerra = new ImageIcon("src/res/tronco.png").getImage();
terra.add(blocoTerra);
blocoTerra = new ImageIcon("src/res/tronco2.png").getImage();
terra.add(blocoTerra);
Image blocoGrama = new ImageIcon("src/res/florbranca.png").getImage();
grama.add(blocoGrama);
blocoGrama = new ImageIcon("src/res/florvermelha.png").getImage();
grama.add(blocoGrama);
blocoGrama = new ImageIcon("src/res/matinho.png").getImage();
grama.add(blocoGrama);
blocoGrama = new ImageIcon("/src/res/cogumelovermelho.png").getImage();
grama.add(blocoGrama);
for (int i = 0; i < 20; i++) {
grama.add(new ImageIcon("src/res/graminha.png").getImage());
terra.add(new ImageIcon("src/res/terrinha.png").getImage());
}
}
}
I did some testing and this way it works normally:
graficos.drawImage(terra.get(numAleatorio), 16, y, this);
graficos.drawImage(terra.get(numAleatorio), 32, y, this);
graficos.drawImage(terra.get(numAleatorio), 48, y, this);
I can not do this because the terrain type depends on the letters in txt