I have a picture(a map) drawn to a panel using a BufferedImage and I want to draw circle on different parts of the map. I'm using a Graphics object to draw the circles onto the map but everytime I call the repaint function, everything I've drawn on the map disappears. Really need help with this. The following functions are the functions that draws the map unto the panel and the function that draws the images unto the map. Am I doing something wrong here???
Code:
public void loadBImage(){
bImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_RGB);
bImage2 = new BufferedImage(image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics bImageG = bImage.getGraphics();
Graphics bImage2 = bImage.getGraphics();
bImageG.drawImage(image, 0,0,null, null);
bImage2.drawImage(image, 0,0,null, null);
}
public void drawCard(int ax, int ay){
Graphics g = this.getGraphics();
g.setColor(new Color(150, 150, 150));
g.fillRect(ax, ay, 10, 10);
}
public void drawArmy(Country country, Player player){
country = currentCountry;
int xLoc = country.getXLoc();
int yLoc = country.getYLoc();
if (xLoc == 0){
xLoc = x;
country.setXLoc(x);
}
if (yLoc == 0){
yLoc = y;
country.setYLox(y);
}
int numArmies = country.getNumArmies();
char armies = (char)(numArmies + '0');
char army[] = {armies};
Graphics g = this.getGraphics();
String color = player.getColor();
Color aColor = null;
if (color == "Red"){
aColor = Color.RED;
}
else if (color == "Yellow"){
aColor = Color.YELLOW;
}
else if (color == "Blue"){
aColor = Color.BLUE;
}
else if (color == "Green"){
aColor = Color.GREEN;
}
else if (color == "Gray"){
aColor = Color.GRAY;
}
else if (color == "Orange"){
aColor = Color.ORANGE;
}
g.setColor(aColor);
g.fillOval(xLoc, yLoc, 10, 10);
Color black = Color.BLACK;
g.setColor(black);
g.drawChars(army, 0, 1, xLoc, yLoc);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (!panelSized) {
scaleImage(getWidth(), getHeight());
loadBImage();
panelSized = true;
}
g.drawImage(bImage, 0, 0, image.getWidth(null), image.getHeight(null), this);
}