图像/图标使用相对路径

时间:2017-02-02 14:34:41

标签: java eclipse relative-path

我真的不知道如何在Eclipse中正确使用相对路径。我在src文件夹旁边创建了一个res文件夹,里面有一个图像文件夹。

这是我目前的代码:

import os 
import random
import time
from utilities import roll


class Character(object):
    # A Class for ALL characters - player, enemy etc.
    def __init__(self, name, hp, armor, damage, strength,
                 intelligence, wisdom, dexterity, constitution, charisma,
                 inventory, profession):
        self.name = name
        self.hp = hp
        self.armor = armor
        self.damage = damage
        self.strength = strength
        self.intelligence = intelligence
        self.wisdom = wisdom
        self.dexterity = dexterity
        self.constitution = constitution
        self.charisma = charisma
        self.inventory = inventory
        self.profession = profession


class Player(Character):
    # A class for the Player character only.
    # We will use a few parameters to set initial variables
    def __init__(self):
        super().__init__(name="", hp=10, armor=10, damage=10, strength=7,
                         intelligence=7, wisdom=7, dexterity=7, constitution=7,
                         charisma=7, inventory=[], profession="")
    maxhp = 10
    level = 1
    exp = 0


class Enemy(Character):
    # a class for any enemy.
    def __init__(self):
        super().__init__(name=enemyname, hp=10, armor=10, damage=10,
                         strength=7, intelligence=7, wisdom=7, dexterity=7,
                         constitution=7, charisma=7, inventory=[],
                         profession="")

playerchar = Player()
# simply call playerchar rather than calling the Class each time


def menu():
                            # Running the Main Menu under a single Function.
                            # This is the simplest method of running the menu
    while True:
        print("|==================|")
        print("Welcome to Py RPG!")
        print("Please select an option!")
        print("1. Start a Game\n")
        print("2. Settings\n")
        print("3. Quit")
        choice = input("\n>")
        if choice == 1 or choice == "start":
            gameloop()
        elif choice == 2 or choice == "settings":
            settings()
        elif choice == 3 or choice == "quit":
            break
        else:
            print("Please select an option from the menu.")


def settings():
    # Settings page for all amendments
    # TODO - create a 'settings' file and have this read/amend it?
    print("Nothing exists here at the moment!")
    menu()


def gameloop():
    # the main game loop, contains all calls to relevant functions
    while True:
        print("This is the main game")
        print("Let's begin. What is your name?")
        playerchar.name = input(">")
        print("Well then, " + playerchar.name + ", let us begin.")
        statchoice()


def choosestat(statname, max, min):
    # Takes the name of the stat
    # as mentioned in the Player/Character class and
    # a min/max value for the length. Allows the player
    # to set their stats for the char.
    print("Your stat choices are: " + str(stats))
    choice = int(input("Please select a strength score ("+min+":"+max+")\n"))
    if type(choice) == int and choice < (max+1) and choice > (min-1):
        self.statname = stats[choice-1]
        stats.pop(choice-1)
    else:
        print("Please select a valid option.\n")


def displaystats(entity):
            # quick function to display all relevant stats in-line.
        print("Are you happy with your choices?\n")
        print("Strength: " + str(entity.strength))
        print("Intelligence: " + str(entity.intelligence))
        print("Wisdom: " + str(entity.wisdom))
        print("Dexterity: " + str(entity.dexterity))
        print("Constitution: " + str(entity.constitution))
        print("Charisma: " + str(entity.charisma))


def statchoice():
    # Using the roll function, we get 6 ability scores, append them to 'stats',
    # and run the choosestat function for each to set stats.
    stats = []
    stats.append(roll(4, 6))
    stats.append(roll(4, 6))
    stats.append(roll(4, 6))
    stats.append(roll(4, 6))
    stats.append(roll(4, 6))
    stats.append(roll(4, 6))

    choosestat(strength, 6, 1)
    choosestat(intelligence, 5, 1)
    choosestat(wisdom, 4, 1)
    choosestat(dexterity, 3, 1)
    choosestat(constitution, 2, 1)
    choosestat(charisma, 1, 1)

    displaystats(playerchar)
    reroll = input("Do you wish to re-roll?")
    if reroll == "yes" or reroll == "y":
        statchoice()


menu()

4 个答案:

答案 0 :(得分:1)

尝试这样做

JButton addButton = new JButton(new ImageIcon(getClass().getResource("images\button.png")));

答案 1 :(得分:1)

您可以使用getClass().getResource(...)创建一个图标,如下所示:

 Icon icon = new ImageIcon(getClass().getResource("/images/button.png"));
 JButton b = new JButton(icon);

答案 2 :(得分:1)

Based on the forum无需写下 /res/image.png ,而只需写下 /image.png 即可。 (顺便说一下,该图像位于src / main / resources文件夹中)

答案 3 :(得分:0)

你可以这样写。

JButton addButton = new JButton(new ImageIcon("images/button.png"));