ボタン付きのメニューシステムをセットアップしようとしています。ただし、右に表示されるボタンは 1 つだけです。
問題が見つかりました。1 つのクラスまたはサブクラスから Button クラスの複数のインスタンスを作成することはできません。私がそうすると、2番目のインスタンスが正しく作成されず、背景画像が欠落します。Button クラスを標準クラスにしたことが関係しているのでしょうか。
これが Button クラスの主要部分です。このクラス内のものの値を返す where 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;
}