簡體   English   中英

無法繪制多個實例

[英]Can't draw more than one instance

我正在嘗試設置一個帶按鈕的菜單系統; 但是,只顯示一個按鈕。

好吧,我發現了問題,我不能從一個類或它的子類創建Button類的多個實例。 如果我這樣做,它不會創建正確的第二個實例,然后它將缺少背景圖像。 這可能與我使Button類成為標准類的事實有關嗎?

這是Button類的主要部分,我拿出的所有get方法都返回了這個類中的東西的值。

public class Button {
private int x, y;
private int width, height;
private Image sprite;
private data.ImageControl Image = new data.ImageControl();
private String text = "";

public Button() {
    sprite = Image.getImage("game/menu/btn.png");
}

public void setImage(String file) {
    sprite = Image.getImage(file);
}

public void draw(Graphics2D g) {
    g.drawImage(sprite, x, y, null);
    Font_LARGE font = new Font_LARGE();

    //Find text pos
    int stringX, stringY;
    int textWidth;
    textWidth = text.length() * 14;

    stringX = x + ((width / 2) - (textWidth / 2));
    stringY = y + ((height / 2) - 8);

    font.drawString(g, text, stringX, stringY);
}

以下是我從中獲取圖像的代碼:

public Image getImage(String filename) {
    Image img;
    try {
        ImageIcon i = new ImageIcon(getClass().getResource("sprite/" + filename));
        img = i.getImage();
    } catch(Exception e) {
        e.printStackTrace();
        System.err.println("ERROR - Unable to load image at " + filename + " loading empty image.");
        ImageIcon i = new ImageIcon(getClass().getResource("sprite/Physix/noImage.png"));
        img = i.getImage();
    }

    return img;
}

x和y的位置是什么?

在我看來,你在另一個上面畫了一個按鈕。

我現在通過在按鈕類之外繪制按鈕背景來解決問題。 我仍然不知道為什么它不起作用,但這種方式有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM