質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Q&A

1回答

777閲覧

画像が表示されないです。

J_Mametsubu

総合スコア0

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

0グッド

0クリップ

投稿2023/01/12 05:10

編集2023/01/12 08:14

前提

学校で「じゃんけんゲーム」を作成しているのですが、相手が出した手の画像が表示されないです。

実現したいこと

  • 画像が表示されるようにする

該当のソースコード

Main.java

package RockPaperScissors; public class Main { public static void main(String[] args) { MainWindow window = new MainWindow(); window.show(); } }

Mainwindow.java

package RockPaperScissors; import javax.swing.*; import java.awt.event.*; import java.awt.*; import java.awt.Graphics.*; import RockPaperScissors.Hands; public class MainWindow { private final JFrame frame; private final JLabel messageLabel1; private final JLabel messageLabel2; private JLabel HandsImage; private final JButton rockButton; private final JButton scissorsButton; private final JButton paperButton; private final JButton retryButton; private Status gameStatus; private Hands opponentHand; static private int winning = 0; static private JPanel canvas; public MainWindow() { this.frame = new JFrame("じゃんけん対決"); this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.frame.setBounds(200, 200, 600, 400); var pane = this.frame.getContentPane(); canvas = new JPanel(); canvas.setLayout(null); this.messageLabel1 = new JLabel("じゃんけん..."); this.messageLabel1.setBounds(20, 20, 400, 40); canvas.add(this.messageLabel1); this.messageLabel2 = new JLabel("連勝回数:" + winning); this.messageLabel2.setBounds(500, 20, 400, 40); canvas.add(this.messageLabel2); this.rockButton = new JButton(Hands.Rock.getDisplay()); this.rockButton.setBounds(100,300, 100, 40); this.rockButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { selectHand(Hands.Rock); HandsImage = new JLabel(new ImageIcon("rock.png")); //HandsImage = new JLabel(opponentHand.getImage()); } }); this.HandsImage = new JLabel(new ImageIcon("paper.png")); this.HandsImage.setPreferredSize(new Dimension(300,300)); this.HandsImage.setHorizontalAlignment(JLabel.LEFT); this.HandsImage.setBounds(300, 300, 300, 300); canvas.add(HandsImage); canvas.add(rockButton); this.scissorsButton = new JButton(Hands.Scissors.getDisplay()); this.scissorsButton.setBounds(250, 300, 100, 40); this.scissorsButton.addActionListener((e) -> this.selectHand(Hands.Scissors)); canvas.add(this.scissorsButton); this.paperButton = new JButton(Hands.Paper.getDisplay()); this.paperButton.setBounds(400, 300, 100, 40); this.paperButton.addActionListener((e) -> this.selectHand(Hands.Paper)); canvas.add(this.paperButton); this.retryButton = new JButton("リトライ"); this.retryButton.setBounds(450, 200, 100, 40); this.retryButton.addActionListener((e) -> this.init()); canvas.add(this.retryButton); pane.add(canvas); } public void paint(Graphics g) { g.setColor(Color.black); } public void show() { this.init(); this.frame.setVisible(true); } public void init() { this.messageLabel1.setText("じゃんけん..."); this.opponentHand = Hands.getRandomHand(); this.HandsImage = new JLabel(new ImageIcon("paper.png")); this.HandsImage.setPreferredSize(new Dimension(300,300)); this.HandsImage.setHorizontalAlignment(JLabel.LEFT); canvas.add(HandsImage); canvas.add(rockButton); this.gameStatus = Status.Wait; } public void selectHand(Hands selected) { if(this.gameStatus != Status.Wait) { return; } switch((selected.getNumber() - opponentHand.getNumber() + 3) % 3) { case 0: this.init(); this.messageLabel1.setText("あいこで..."); break; case 1: this.messageLabel1.setText(String.format("相手が出したのは「%s」なのであなたの負けです。", this.opponentHand.getDisplay())); this.winning = 0; this.messageLabel2.setText(String.format("連勝回数:" + winning, this.opponentHand.getDisplay())); this.gameStatus = Status.Done; break; case 2: this.messageLabel1.setText(String.format("相手が出したのは「%s」なのであなたの勝ちです。", this.opponentHand.getDisplay())); ++ this.winning; this.messageLabel2.setText(String.format("連勝回数:" + winning, this.opponentHand.getDisplay())); this.gameStatus = Status.Done; break; } } }

Hands.java

package RockPaperScissors; import java.util.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import javax.swing.ImageIcon; public enum Hands { Rock("グー", 0, "rock.png"), Scissors("チョキ", 1,"scissors.png"), Paper("パー", 2, "paper.png"); private final String display; private final int number; private final ImageIcon image; Hands(String display, int number, String filename) { this.display = display; this.number = number; this.image = new ImageIcon(filename); } public static Hands getRandomHand() { Random rand = new Random(); return Hands.values()[rand.nextInt(3)]; } public String getDisplay() { return this.display; } public int getNumber() { return this.number; } public ImageIcon getImage() { return this.image; } }

試したこと

this.HandsImage = new JLabel(new ImageIcon("paper.png")); this.HandsImage.setPreferredSize(new Dimension(300,300)); this.HandsImage.setHorizontalAlignment(JLabel.LEFT); this.HandsImage.setBounds(300, 300, 300, 300);

の部分を様々な場所に移動させましたが、どれも失敗でした。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2023/01/12 05:21

javadoc は関係ありませんのでタグから外したほうが良いです。 >~の部分を様々な場所に移動させましたが プログラムがどう動いているかは考えずにテキトウに移動させていたのでしょうか。
J_Mametsubu

2023/01/12 05:27

コメントありがとうございます。 試した場所としては上記の場所の他に、MainWindow.java内のpublic void actionPerformed(ActionEvent e)、canvas.setLayout(null);の下で試しました。
m.ts10806

2023/01/12 08:13

質問は編集できますので
jimbe

2023/01/12 08:55

学校でということで恐らく Swing の前にコンソールプログラムをやっていると思いますが、 Swing はコンソールのように"表示"と言えば即表示するわけではありません。 JFrame→JPanel→JLabel といった各コンポーネントのツリー構造を正しく作って、後は Swing に表示をお願いすると Swing 側で適切なタイミングで表示する…という間接的な処理となります。 移動させてみたという HandsImage の生成処理は、処理フローのどこかで canvas に add されていれば表示対象とされ add されなければ対象とされないというだけのことで、処理のタイミングを変更しようというのであれば適切なタイミングがどこなのかを理解して変更するべきでしょう。 また、 shiketa さんのご指摘の通り、 そもそも画像ファイルが取得出来ているのかは確認する必要があると思います。 各ファイルがどこにあるのか、『取得出来る所に置いているはず』であれば、フォルダ構造も提示して頂けるとこちらも確認出来ます。
guest

回答1

0

相手が出した手の画像が表示されないです。

正しいパスに画像があれば、下のほうに表示はされてるんじゃないですかね?(パーだけですが^^;
アプリ画像


それはそれとしてコードの問題点です。

Java

1HandsImage = new JLabel(new ImageIcon("rock.png"));

HandsImageのインスタンスを作り直しても、元のHandsImageの画像が変わるわけではありません。
元のHandsImageは表示され続け、新しく作られたJLabelはどこにもaddされていないので宙ぶらりん(表示されずにメモリ上にあるだけ)の状態です。

Java

1this.HandsImage = new JLabel(new ImageIcon("paper.png")); 2canvas.add(HandsImage);

だからと言ってこのようにしてしまうと、今度はJLabelが増え続けます。

Java

1this.messageLabel1.setText("あいこで...");

messageLabel1と同じように、表示内容だけ変えればいいのです。
JLabel#setIcon (Java Platform SE 8 )

Java

1package RockPaperScissors; 2 3import javax.swing.*; 4import javax.swing.border.LineBorder; 5import java.awt.Color; 6import java.awt.Dimension; 7import java.util.Random; 8 9public class MainWindow extends JFrame { 10 public static void main(String[] args) { 11 new MainWindow().setVisible(true); 12 } 13 14 private final JLabel messageLabel1; 15 private final JLabel messageLabel2; 16 private final JLabel HandsImage; 17 18 private Status gameStatus = Status.Wait; 19 private int winning = 0; 20 21 public MainWindow() { 22 setTitle("じゃんけん対決"); 23 setSize(600, 400); 24 setLocationRelativeTo(null); 25 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 26 27 JPanel canvas = new JPanel(null); 28 29 messageLabel1 = new JLabel("じゃんけん..."); 30 messageLabel1.setBounds(20, 20, 400, 40); 31 canvas.add(messageLabel1); 32 33 messageLabel2 = new JLabel("連勝回数:" + winning); 34 messageLabel2.setBounds(500, 20, 400, 40); 35 canvas.add(messageLabel2); 36 37 HandsImage = new JLabel(new ImageIcon("paper.png")); 38 HandsImage.setPreferredSize(new Dimension(300, 300)); 39 HandsImage.setHorizontalAlignment(JLabel.LEFT); 40 HandsImage.setBounds(150, 0, 300, 300); 41// HandsImage.setBorder(new LineBorder(Color.RED, 2)); 42 canvas.add(HandsImage); 43 44 JButton rockButton = new JButton(Hands.Rock.getDisplay()); 45 rockButton.setBounds(100, 300, 100, 40); 46 rockButton.addActionListener(e -> selectHand(Hands.Rock)); 47 canvas.add(rockButton); 48 49 JButton scissorsButton = new JButton(Hands.Scissors.getDisplay()); 50 scissorsButton.setBounds(250, 300, 100, 40); 51 scissorsButton.addActionListener(e -> selectHand(Hands.Scissors)); 52 canvas.add(scissorsButton); 53 54 JButton paperButton = new JButton(Hands.Paper.getDisplay()); 55 paperButton.setBounds(400, 300, 100, 40); 56 paperButton.addActionListener(e -> selectHand(Hands.Paper)); 57 canvas.add(paperButton); 58 59 JButton retryButton = new JButton("リトライ"); 60 retryButton.setBounds(450, 200, 100, 40); 61 retryButton.addActionListener(e -> init()); 62 canvas.add(retryButton); 63 64 add(canvas); 65 } 66 67 private void init() { 68 messageLabel1.setText("じゃんけん..."); 69 HandsImage.setIcon(null); 70 gameStatus = Status.Wait; 71 } 72 73 private void selectHand(Hands selected) { 74 if (gameStatus != Status.Wait) { 75 return; 76 } 77 78 Hands opponentHand = Hands.getRandomHand(); 79 switch ((selected.getNumber() - opponentHand.getNumber() + 3) % 3) { 80 case 0: 81 messageLabel1.setText("あいこで..."); 82 break; 83 case 1: 84 messageLabel1.setText(String.format("相手が出したのは「%s」なのであなたの負けです。", opponentHand.getDisplay())); 85 winning = 0; 86 messageLabel2.setText("連勝回数:" + winning); 87 gameStatus = Status.Done; 88 break; 89 case 2: 90 messageLabel1.setText(String.format("相手が出したのは「%s」なのであなたの勝ちです。", opponentHand.getDisplay())); 91 ++winning; 92 messageLabel2.setText("連勝回数:" + winning); 93 gameStatus = Status.Done; 94 break; 95 } 96 97 // messageLabel1なんかと同じように内容を入れ替える 98 HandsImage.setIcon(selected.getImage()); 99 } 100} 101 102enum Status { Done, Wait, } 103 104enum Hands { 105 Rock("グー", 0, "rock.png"), 106 Scissors("チョキ", 1, "scissors.png"), 107 Paper("パー", 2, "paper.png"); 108 109 private final String display; 110 private final int number; 111 private final ImageIcon image; 112 113 private static final Random rand = new Random(); 114 115 Hands(String display, int number, String filename) { 116 this.display = display; 117 this.number = number; 118 image = new ImageIcon(filename); 119 } 120 121 public static Hands getRandomHand() { 122 return Hands.values()[rand.nextInt(Hands.values().length)]; 123 } 124 125 public String getDisplay() { return display; } 126 127 public int getNumber() { return number; } 128 129 public ImageIcon getImage() { return image; } 130}

投稿2023/01/12 09:04

TN8001

総合スコア9321

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問