1
I need to generate a map according to a txt file that I receive as input. Each font of my txt file corresponds to a type of map terrain. I created arrays of Image
that will store the images that I can use for each type of terrain and drawing these images in Jframe. I already checked and the path of the images are correct, but as I call the onPaint() method several times during the execution of the program, the map ends up being deleted. I need to draw the map just once and then move an avatar over it. How do I draw the map, set it up and just redesign the avatar as it moves?
Follows 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 tests and this way 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);
Only I can’t do it because the type of terrain depends on the letters in txt
Please add the example that is reproducible, so that it is possible to simulate the problem and propose a solution.
– user28595