在使BufferedImage可点击时遇到一些麻烦

时间:2016-05-04 13:36:37

标签: java mouseevent bufferedimage mouselistener actionevent

我是java新手,目前正在开发我的第一款游戏!我想知道,我知道如何制作按钮和类似的东西感谢我读过的一本书。 但是将BufferedImage变成可点击的对象? 这从来没有被覆盖,但我需要它,因为它是一个点击游戏,我希望用户必须点击它自己的类中的怪物,然后通过drawImage绘制。

任何人都可以给我任何支持吗? 也可以在怪物类中执行此操作并在绘制后保持可点击功能吗? 我已经尝试过对此进行研究,看起来我需要一个鼠标监听器,但它并没有涵盖如何;反正不是我的理解水平。我道歉;我相当愚蠢。

  //Imports
  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;
  //Types of monsters.
        String monster[] = {"Ork", "Mermaid", "Goblin", "Fish", "Griffen", "Cerberus", "Empousai", "Gorgon", "Stymphalian Bird", "Chimera", "Ichthyocentaur", "Typhon", "Minotaur", "Fury", "Hydra", "Sphinx"};
        String monsterType = monster[rand.nextInt(monster.length)];
        monsterSprite = ImageLoader.loadImage("rec/alpha/monster/" + monsterType + ".png");
  //Friends. <3
        String[] firstName = {"Lucas", "Ben", "Caytlynn", "Fay", "Seth", "Jostein", "Paige", "Luis", "Josefiina", "Patricia", "Angus", "David", "Manjit", "Matt", "Maya", "Richard", "Roman", "Rudy", "Téa", "Fi"};
        String connection1 = " the ";
  //Adjectives
        String[] secondName = {"Powerful ", "Unstoppable ", "Almighty ", "Vigourous ", "Formidible ", "Mighty ", "Cowardly ", "Intoxicated "};
        String connection2 = " of ";
  //The seven deadly sins/the sins of which each demon represents.
        String[] thirdName = {"Sloth", "Wrath", "Pride", "Greed", "Lust", "Envy", "Gluttony"};

        monsterName = firstName[rand.nextInt(firstName.length)] + connection1 + secondName[rand.nextInt(secondName.length)] + monsterType + connection2 +  thirdName[rand.nextInt(thirdName.length)];
     }
  }

1 个答案:

答案 0 :(得分:0)

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