pandaJuan pandaJuan - 1 month ago 11
Java Question

cannot always load the picture on click and hover

I am making a launcher application, i wish to change the button depending on the action taken, right now i want to change the button when the following actions happen, either the player hovers over the image, clicks on the image, releases the image click or exits the hover. The issue i have is that it doesn't always load correctly, which is odd.

package LostStory;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;

@SuppressWarnings("serial")
public class LaunchPanel extends JComponent {

Image image;
JButton play;
SystemTray tray;

ImageIcon icon = new ImageIcon("res/images/buttons/playNonHover.png");
ImageIcon iconHover = new ImageIcon("res/images/buttons/playHover.png");
ImageIcon iconClick = new ImageIcon("res/images/buttons/playClick.png");

public LaunchPanel(Image img) {
this.image = img;

}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
init();

}

public void init() {
play = new JButton("Play Lost Story", icon);

play.setBounds(85, 210 - 75, getWidth() - 165, 50);

play.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Runtime run = Runtime.getRuntime();
try {
run.exec("notepad");
if (SystemTray.isSupported()) {
tray = SystemTray.getSystemTray();
play.setIcon(play.getIcon());
// Main.getClient().getJFrame().setVisible(false);
// tray.add(img);
}
} catch (IOException e) {
e.printStackTrace();
}

}
});

play.addMouseListener(new MouseListener() {

@Override
public void mouseClicked(MouseEvent arg0) {
} // no use

@Override
public void mouseEntered(MouseEvent e) {
if (iconHover.getImageLoadStatus() == 8) {
play.setIcon(iconHover);
play.setIcon(play.getIcon());
System.out.println("Entered");
}
System.out.println(iconHover.getImageLoadStatus());
} // use

@Override
public void mouseExited(MouseEvent arg0) {
if (icon.getImageLoadStatus() == 8) {
play.setIcon(icon);
play.setIcon(play.getIcon());
System.out.println("Exited");
}
System.out.println(icon.getImageLoadStatus());
} // use

@Override
public void mousePressed(MouseEvent arg0) {
if (iconClick.getImageLoadStatus() == 8) {
play.setIcon(iconClick);
play.setIcon(play.getIcon());
System.out.println("Pressed");
}
System.out.println(iconClick.getImageLoadStatus());

} // use

@Override
public void mouseReleased(MouseEvent arg0) {
if (icon.getImageLoadStatus() == 8) {
play.setIcon(icon);
play.setIcon(play.getIcon());
System.out.println("Released");
}
System.out.println(icon.getImageLoadStatus());
} // use

});

add(play);
}

}


The first button always loads (because it is the default one) it returned an '8' when i tried using the IconImage.getImageLoadStatus() so i expected that to be '8' if it was succesfully loaded. (This is not the case though, it will always return 8 as far as i know).

So TL;DR: How do i make sure the ImageIcons are always loaded and will apply correctly?

Answer

Don't try to manipulate the icon using the setIcon() method.

A JButton has methods to set the icon for various states setRolloverIcon, setPressedIcon, etc.

Read the API for other properties.

Also, a painting method is for painting only. Never create components from a painting method. Get rid of the init() method from your paintComponent() method.

Components should be created and added to the panel in the constructor of the panel.

Comments