简体   繁体   中英

BufferedImage not clickable

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)];
    }
}

我认为作为 10 的入门者,您可以考虑将图像转换为 JButton - 扩展 JButton 并重载paintComponents 方法 - 这将使其可点击。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM