Reputation: 620
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)];
}
}
Upvotes: 0
Views: 143
Reputation: 738
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.
Upvotes: 0