0

I'm currently having trouble making a BufferedImage into a clickable object.
I'd like the user to be able to click the image which is in its own class, and then painted via drawImage.

Can anyone give me any support in making this happen?

Also is it possible to do this in the monster class and maintain the clickable feature even after painting?

I've tried researching on this, and it would seem I need a mouse listener, but I'm struggling to understand how.

import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.util.*;
      
public class Monster {

      static final double BASE_MONSTER_HEALTH = 10;
         
      static double monsterHealth;
      static double monsterDamage;
            
      BufferedImage monsterSprite;
      String monsterName;
      Random rand = new Random();
         
      public Monster() {
          monsterHealth = Math.pow(RPGClicker.room, 2) * BASE_MONSTER_HEALTH;
          monsterDamage = RPGClicker.room + 1 - RPGClicker.defenceLevel;

          String monster[] = {"Ork", "Mermaid", "Goblin"};
          String monsterType = monster[rand.nextInt(monster.length)];
          monsterSprite = ImageLoader.loadImage("rec/alpha/monster/" + monsterType + ".png");

          String[] firstName = {"Oliver", "George", "Harry"};
          String connection1 = " the ";
          String[] secondName = {"Powerful ", "Unstoppable ", "Almighty "};
          String connection2 = " of ";
          String[] thirdName = {"Sloth", "Wrath", "Pride"};
               
          monsterName = firstName[rand.nextInt(firstName.length)] + connection1 + secondName[rand.nextInt(secondName.length)] + monsterType + connection2 +  thirdName[rand.nextInt(thirdName.length)];
    }
}
Seth Falco
  • 313
  • 6
  • 22

1 Answers1

0

I think as a starter for 10 you could consider turning the image into a JButton - extend JButton and overload the paintComponents method - this would make it clickable.

Lee
  • 738
  • 3
  • 13
  • I think as a starter for 10 you could consider turning the image into a JButton - extend JButton and overload the paintComponents method - this would make it clickable. I was thinking that! But I want to be able to change the graphic on it too, I haven't tried but could I make an JButton change the image on it self for half a second when it's clicked and change back? Regardless, I'm thinking about doing that as a temporary solution so I can at least continue on with coding the foundations of the game it self. It was something I simply thought to ask before doing so. – Seth Falco May 04 '16 at 13:46
  • Overloading JButton is a standard Swing axiom if you want something that listens for the mouse click events. You will need to override the paintComponent method to render the image. – Lee May 04 '16 at 13:50
  • Oh! now that I think about it! I can't put a JButton in a custom location either, can I? Like my monsterSprite is at x, y coordinates. As far as I know I'd have to add a JButton to a JPanel; so it wouldn't be as flexible as to where said monster could be placed in the gameScene? – Seth Falco May 04 '16 at 13:50
  • The other option is to intercept the mouse events over the image. – Lee May 04 '16 at 13:51
  • I apologise but I didn't seem to understand the last message you had sent. What do you mean by intercept the mouse events over the image? – Seth Falco May 04 '16 at 14:04
  • If you display the BufferedImage in a JFrame then you can listen to the mouse events of the JFrame and calculate based on what you know is happening on the BufferedImage. – Lee May 04 '16 at 14:14