teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

コード追加

2022/01/20 11:24

投稿

jimbe
jimbe

スコア13357

answer CHANGED
@@ -1,4 +1,54 @@
1
1
  JFrame の paint では super.paint(g) を呼び出す必要があると思います。
2
2
  ただ、JFrame の paint はウインドウ全体を描画しますので、ウインドウタイトルやエッジ等を考慮する必要があり、結構面倒では無いでしょうか。
3
3
 
4
- 可能であれば、背景を含んでゲーム画面としてパネル化し、そのパネルを JFrame に設定して、 JFrame の paint は触らないほうが良いように思います。
4
+ 可能であれば、背景を含んでゲーム画面としてパネル化し、そのパネルを JFrame に設定して、 JFrame の paint は触らないほうが良いように思います。
5
+
6
+ コンストラクタのみ(JFrame のオーバーライトしていた paint() はコメント化)
7
+ ```java
8
+ public QuizGame3(String title, int width, int height) {
9
+ super(title);
10
+ //setVisible(true); //setVisible は最後に行うこと
11
+ setSize(width, height);
12
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
13
+ setResizable(false);
14
+
15
+ JPanel background = new JPanel(new BorderLayout()) {
16
+ @Override
17
+ protected void paintComponent(Graphics g) {
18
+ super.paintComponent(g);
19
+ g.drawImage(img, 0, 0,500,500,this);
20
+ }
21
+ };
22
+ add(background);
23
+
24
+ JPanel headerPanel = new JPanel();
25
+ headerPanel.setBackground(Color.BLACK);
26
+ headerPanel.setPreferredSize(new Dimension(500,50));
27
+ background.add(headerPanel, BorderLayout.NORTH);
28
+
29
+ questionLabel = new JLabel("");
30
+ questionLabel.setHorizontalAlignment(JLabel.CENTER);
31
+ background.add(questionLabel, BorderLayout.CENTER);//中心に問題を表示
32
+
33
+ JPanel answerPanel = new JPanel();
34
+ for(int i=0; i<answerButtons.length; i++) {
35
+ answerButtons[i] = new JButton();
36
+ answerButtons[i].addActionListener(e -> {
37
+ decide(((JButton)e.getSource()).getText());
38
+ });
39
+ answerPanel.add(answerButtons[i]);
40
+ }
41
+
42
+ southPanel = new SouthPanel(answerPanel, e->drawQuiz(), e->end());
43
+ background.add(southPanel, BorderLayout.SOUTH);
44
+
45
+ //クイズをシャッフルして山にする
46
+ List<Quiz> quizList = Arrays.asList(quizArray.clone());
47
+ Collections.shuffle(quizList);
48
+ quizStack.addAll(quizList);
49
+
50
+ drawQuiz();
51
+
52
+ setVisible(true);
53
+ }
54
+ ```