-1
I am developing the home page of a simple game, what I wish is to add a button on the screen where when clicking the game start, follows below my code of how I am mounting the menu and bringing the background, the code brings the image I put as the button, but I’m failing to assign the click action to the same, I don’t know what would be the best way to do it, I accept ideas.
public Menu(){
setFocusable(true);
setDoubleBuffered(true);
ImageIcon referencia = new ImageIcon("res\\images\\fundo.png");
fundo = referencia.getImage();
emJogo = true; //iniciar jogo
timer = new Timer(5, this);
timer.start();
ImageIcon referencia2 = new ImageIcon("res\\images\\btn1.png");
jogar = referencia2.getImage();
}
public void paint(Graphics g){
Graphics2D graficos = (Graphics2D) g;
graficos.drawImage(fundo, 0, 0, null);
graficos.drawImage(jogar, 0, 0, null);
g.dispose();
}
Jbutton ? If it is, just call the actionperformed method and insert the action into it. See an example http://answall.com/questions/12471/calculator-com-interface-gr%C3%A1fica-java/12476#12476
– user28595
Try to play the image on one
JLabel
, just make sure she gets the right sizes and stuff.JLabel lb = new JLabel(referencia2);
. If it works, I’ll give you an answer.– Jéf Bueno