こんにちは私は自分のゲームの一時停止メニューを作成し、キーボードの矢印キーでそれをナビゲートします。私の質問は、どうすればマウスでナビゲートして、矢印キーを使わずにボタンをクリックすることができるのですか?ここキーボードではなくマウスでボタンを選択
はコードです:
public class InGameMenu implements KeyListener {
private String[] string = { "Resume Game", "Options", "Save Game", "Load Game", "Exit Game" };
private String[] optionStrings = { "Help", "Back" };
public static int selected = 0;
private int space = 25;
public int width = 800;
public int height = 600;
public static boolean isInMenu = true;
public static boolean isInOptions = false;
public static boolean saving = false;
public static boolean loading = false;
public InGameMenu() {
}
public void tick() {
}
public void render(Graphics g) {
g.setColor(new Color(0, 0, 0, 90));
g.fillRect(0, 0, Component.width, Component.height);
if (isInMenu) {
g.setColor(Color.LIGHT_GRAY);
if (saving) {
g.drawString("Saving", Component.width/2/Component.pixelSize - (int) (Component.width/25), 35);
}
if (loading) {
g.drawString("Loading", Component.width/2/Component.pixelSize - (int) (Component.width/25), 35);
}
for (int i = 0; i < string.length; i++) {
if (selected == i) {
g.setColor(Color.RED);
} else {
g.setColor(Color.WHITE);
}
g.drawString(string[i], Component.width/2/Component.pixelSize - (int) (Component.width/17.5), Component.height/8 + (i * space));
}
} else if (isInOptions) {
for (int i = 0; i < optionStrings.length; i++) {
if (selected == i) {
g.setColor(Color.RED);
} else {
g.setColor(Color.WHITE);
}
g.drawString(optionStrings[i], Component.width/2/Component.pixelSize - (int) (Component.width/17.5), Component.height/8 + (i * space));
}
}
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (isInMenu) {
if (key == KeyEvent.VK_UP) {
selected--;
if (selected < 0) {
selected = string.length - 1;
}
}
if (key == KeyEvent.VK_DOWN) {
selected++;
if (selected > string.length - 1) {
selected = 0;
}
}
if (key == KeyEvent.VK_ENTER) {
if (selected == 0) {
Component.isInMenu = false;
} else if (selected == 1) {
isInMenu = false;
isInOptions = true;
selected = 0;
} else if (selected == 2) {
saving = true;
SaveLoad.save();
saving = false;
} else if (selected == 3) {
loading = true;
SaveLoad.load();
loading = false;
} else if (selected == 4) {
System.exit(0);
}
}
} else if (isInOptions) {
if (key == KeyEvent.VK_UP) {
selected--;
if (selected < 0) {
selected = optionStrings.length - 1;
}
}
if (key == KeyEvent.VK_DOWN) {
selected++;
if (selected > optionStrings.length - 1) {
selected = 0;
}
}
if (key == KeyEvent.VK_ENTER) {
if (selected == 0) {
System.out.println("HELP");
} else if (selected == 1) {
isInOptions = false;
isInMenu = true;
}
}
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
'getButton'はマウスボタンが押された*を指定します。 *プレスがどこで発生したかは決まっていません。これをテストしましたか?どのアイテムが選択されたかはどのように決定されますか? –
@VinceEmighニースキャッチ!申し訳ありません。私はそれを修正します。 –
ヒント: 'Font'を使って' FontMetrics'を使って[テキストを測定する]ようにしてください(http://docs.oracle.com/javase/tutorial/2d/text/measuringtext.html)。メトリックの 'getWidth'と' getHeight'とテキストの 'x'と' y'の位置を使って、マウスがその領域をクリックしたかどうかを調べることができます。 –