I am attempting to create a program utilizing custom icons (appearing as cities on a map), where the icon displays a label (in this case City 1, City 2, etc.) when hovered over by the mouse. It works fine for cities 2, 3, and 4; however, city 1 has some unexpected behavior. When hovered over the first time, the label appears on the icon as it should, but if the mouse exits and reenters the city, the label appears behind the city's icon. If the mouse enters any other city first, than the label for city 1 defaults to behind the icon. Seeing as all four cities are instances of the same class, I am very confused as to why this is happening.
Hopefully that all makes sense. Here is the main window's code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
@SuppressWarnings("serial")
public class MainWindow extends JFrame{
static JLabel hoverText = new JLabel("", JLabel.CENTER);
static JLayeredPane panel = new JLayeredPane();
public MainWindow(){
ImageIcon cityImg = new ImageIcon("Resources/mock_city.png");
CustomButton city1, city2, city3, city4;
city1 = new CustomButton(cityImg, "City 1");
city2 = new CustomButton(cityImg, "City 2");
city3 = new CustomButton(cityImg, "City 3");
city4 = new CustomButton(cityImg, "City 4");
Dimension cityDims = new Dimension(cityImg.getIconWidth(),cityImg.getIconHeight());
city1.setSize(cityDims);
city1.setLocation(50,50);
city2.setSize(cityDims);
city2.setLocation(50,150);
city3.setSize(cityDims);
city3.setLocation(150,50);
city4.setSize(cityDims);
city4.setLocation(150,150);
panel.add(city1, new Integer(2));
panel.add(city2, new Integer(2));
panel.add(city3, new Integer(2));
panel.add(city4, new Integer(2));
add(panel);
setSize(300,300);
setVisible(true);
}
public static void setHoverText(String label, Point p) {
hoverText.setText(label);
hoverText.setForeground(Color.RED);
hoverText.setSize(77,13);
hoverText.setLocation(p);
panel.add(hoverText, new Integer(3));
}
public static void delHoverText() {
hoverText.setText("");
panel.add(hoverText, new Integer(3));
}
public static void main (String[] args) throws IOException {
new MainWindow();
}
}
And here is the code for the icons:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
@SuppressWarnings("serial")
public class CustomButton extends JComponent implements MouseListener{
ImageIcon buttonImg;
String label;
public CustomButton(ImageIcon buttonImg, String label){
super();
addMouseListener(this);
this.buttonImg = buttonImg;
this.label = label;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(buttonImg.getImage(), 0, 0, this);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
MainWindow.setHoverText(label, getLocation());
}
@Override
public void mouseExited(MouseEvent arg0) {
MainWindow.delHoverText();
}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
}