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

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

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

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

Q&A

解決済

1回答

984閲覧

タイミングよくボタンをクリックすると得点が与えられるプログラム

ophelia

総合スコア2

Java

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

0グッド

0クリップ

投稿2021/05/25 20:34

前提・実現したいこと

3つのボタン(緑黄赤)をタイミングよくクリックすると得点が与えられるプログラムを作成しています。
得点を得ると押したボタンの色が白に変わります。
タイミングを間違えると得点を失って押したボタンの色が灰色に変わります。

発生している問題・エラーメッセージ

赤色のボタンにしかノーツが流れず、赤をクリックしても緑のボタンの色が変わってしまいます。

該当のソースコード

Java

1import java.awt.*; 2import java.awt.event.*; 3 4class YPanel extends XPanel { 5 Panel innerPanel3; 6 Panel innerPanel4; 7 8 public YPanel() { 9 // setLayout(null); 10 innerPanel3 = new Panel(); 11 add(innerPanel3); 12 innerPanel3.setBackground(Color.black); 13 14 innerPanel4 = new Panel(); 15 add(innerPanel4); 16 innerPanel4.setBackground(Color.black); 17 } 18 public void setBounds(int x, int y, int width, int height) { 19 super.setBounds(x,y,width,height); 20 innerPanel3.setBounds(4,height-8,width-8,8); 21 innerPanel4.setBounds(width-8,4,8,height-8); 22 } 23} 24 25class XPanel extends Panel { 26 Panel innerPanel1; 27 Panel innerPanel2; 28 29 public XPanel() { 30 setLayout(null); 31 innerPanel1 = new Panel(); 32 add(innerPanel1); 33 innerPanel1.setBackground(Color.white); 34 35 innerPanel2 = new Panel(); 36 add(innerPanel2); 37 innerPanel2.setBackground(Color.white); 38 } 39 public void setBounds(int x, int y, int width, int height) { 40 super.setBounds(x,y,width,height); 41 innerPanel1.setBounds(0,0,width-8,8); 42 innerPanel2.setBounds(0,0,8,height-8); 43 } 44} 45 46class dpp6_4 { 47 static Frame myframe; 48 static YPanel mypanel1; 49 static YPanel mypanel2; 50 static YPanel mypanel3; 51 static YPanel mypanelline1[]; 52 static YPanel mypanelline2[]; 53 static YPanel mypanelline3[]; 54 static int score = 0; 55 56 public static void main(String args[]) { 57 int i; 58 59 myframe = new Frame(); 60 mypanel1 = new YPanel(); 61 mypanel2 = new YPanel(); 62 mypanel3 = new YPanel(); 63 64 myframe.setLayout(null); // does not use layout manager 65 myframe.setSize(400,600); // window size : width = 400, height = 600 66 myframe.setVisible(true); // make the window visible 67 68 myframe.add(mypanel1); 69 mypanel1.setBounds(30,40,100,100); 70 mypanel1.setBackground(Color.green); 71 mypanelline1 = new YPanel[5]; 72 for( i = 0; i < 5; i++ ) { 73 mypanelline1[i] = new YPanel(); 74 myframe.add(mypanelline1[i]); 75 mypanelline1[i].setBounds(270+25,130+i*80,50,50); 76 mypanelline1[i].setBackground(Color.gray); 77 mypanelline1[i].setVisible(false); 78 } 79 80 myframe.add(mypanel2); 81 mypanel2.setBounds(150,40,100,100); 82 mypanel2.setBackground(Color.yellow); 83 mypanelline2 = new YPanel[5]; 84 for( i = 0; i < 5; i++ ) { 85 mypanelline2[i] = new YPanel(); 86 myframe.add(mypanelline2[i]); 87 mypanelline2[i].setBounds(270+25,130+i*80,50,50); 88 mypanelline2[i].setBackground(Color.gray); 89 mypanelline2[i].setVisible(false); 90 } 91 92 myframe.add(mypanel3); 93 mypanel3.setBounds(270,40,100,100); 94 mypanel3.setBackground(Color.red); 95 mypanelline3 = new YPanel[5]; 96 for( i = 0; i < 5; i++ ) { 97 mypanelline3[i] = new YPanel(); 98 myframe.add(mypanelline3[i]); 99 mypanelline3[i].setBounds(270+25,130+i*80,50,50); 100 mypanelline3[i].setBackground(Color.gray); 101 mypanelline3[i].setVisible(false); 102 } 103 104 // o ma ji na i ! 105 mypanel1.addMouseListener(new MouseAdapter() { 106 public void mouseReleased(MouseEvent e) { 107 mypanel1MouseReleased(e); 108 } 109 } ); 110 111 mypanel2.addMouseListener(new MouseAdapter() { 112 public void mouseReleased(MouseEvent e) { 113 mypanel2MouseReleased(e); 114 } 115 } ); 116 mypanel3.addMouseListener(new MouseAdapter() { 117 public void mouseReleased(MouseEvent e) { 118 mypanel3MouseReleased(e); 119 } 120 } ); 121 122 while(true) { 123 sleep(300); 124 if( mypanelline1[0].isVisible() ) { 125 score -= 10; 126 System.out.print("SCORE:" + score + "\n"); 127 mypanel1.setBackground(Color.gray); 128 } else if ( mypanelline2[0].isVisible() ) { 129 score -= 10; 130 System.out.print("SCORE:" + score + "\n"); 131 mypanel2.setBackground(Color.gray); 132 } else if ( mypanelline3[0].isVisible() ) { 133 score -= 10; 134 System.out.print("SCORE:" + score + "\n"); 135 mypanel3.setBackground(Color.gray); 136 } else { 137 mypanel1.setBackground(Color.green); 138 mypanel2.setBackground(Color.yellow); 139 mypanel3.setBackground(Color.red); 140 } 141 for( i = 0; i < 4; i++ ) { 142 boolean v = mypanelline1[i+1].isVisible(); 143 mypanelline1[i].setVisible(v); 144 boolean w = mypanelline2[i+1].isVisible(); 145 mypanelline2[i].setVisible(v); 146 boolean x = mypanelline3[i+1].isVisible(); 147 mypanelline3[i].setVisible(v); 148 } 149 mypanelline1[4].setVisible(Math.random() > 0.7); 150 mypanelline2[4].setVisible(Math.random() > 0.7); 151 mypanelline3[4].setVisible(Math.random() > 0.7); 152 } 153 154 } 155 public static void mypanel1MouseReleased(MouseEvent e) { 156 if( mypanelline1[0].isVisible() == true ) { 157 mypanel1.setBackground(Color.white); 158 mypanelline1[0].setVisible(false); 159 score += 30; 160 System.out.print("SCORE:" + score + "\n"); 161 } else { 162 mypanel1.setBackground(Color.gray); 163 score -= 5; 164 System.out.print("SCORE:" + score + "\n"); 165 } 166 167 } 168 public static void mypanel2MouseReleased(MouseEvent e) { 169 if( mypanelline2[0].isVisible() == true ) { 170 mypanel2.setBackground(Color.white); 171 mypanelline2[0].setVisible(false); 172 score += 30; 173 System.out.print("SCORE:" + score + "\n"); 174 } else { 175 mypanel2.setBackground(Color.gray); 176 score -= 5; 177 System.out.print("SCORE:" + score + "\n"); 178 } 179 180 } 181 public static void mypanel3MouseReleased(MouseEvent e) { 182 if( mypanelline3[0].isVisible() == true ) { 183 mypanel3.setBackground(Color.white); 184 mypanelline3[0].setVisible(false); 185 score += 30; 186 System.out.print("SCORE:" + score + "\n"); 187 } else { 188 mypanel3.setBackground(Color.gray); 189 score -= 5; 190 System.out.print("SCORE:" + score + "\n"); 191 } 192 193 } 194 static void sleep(long msec) { 195 try{ 196 Thread.sleep(msec); 197 }catch(InterruptedException ie) { 198 } 199 } 200} 201

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

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

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

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

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

guest

回答1

0

ベストアンサー

赤色のボタンにしかノーツが流れず、赤をクリックしても緑のボタンの色が変わってしまいます。

全てのYPanelを右端(270+25)にしています。

Java

1mypanelline1[i].setBounds(270+25,130+i*80,50,50); 2mypanelline2[i].setBounds(270+25,130+i*80,50,50); 3mypanelline3[i].setBounds(270+25,130+i*80,50,50);

気になった点

  • AWTからSwingにする
    Swingのほうが少し便利です。基本的にはJをつける程度です。
  • 無限ループをタイマーに
    まあ動きはしますが素直にイベントベースで書きましょう。
  • ループ変数は使いまわさない
    ほとんど意味がないうえバグの元です。

ほか私の好みで書き換えました^^;

Java

1import java.awt.Color; 2import java.awt.event.ActionEvent; 3import java.awt.event.ActionListener; 4import java.awt.event.MouseAdapter; 5import java.awt.event.MouseEvent; 6import javax.swing.JFrame; 7import javax.swing.JPanel; 8import javax.swing.Timer; 9import javax.swing.border.BevelBorder; 10import javax.swing.border.Border; 11import javax.swing.border.CompoundBorder; 12 13 14public class MyFrame extends JFrame { 15 public static void main(String[] args) { 16 new MyFrame().setVisible(true); 17 } 18 19 private MyPanel panel1 = new MyPanel(); 20 private MyPanel panel2 = new MyPanel(); 21 private MyPanel panel3 = new MyPanel(); 22 private MyPanel[] line1 = new MyPanel[5]; 23 private MyPanel[] line2 = new MyPanel[5]; 24 private MyPanel[] line3 = new MyPanel[5]; 25 private int score = 0; 26 27 MyFrame() { 28 setLayout(null); 29 setSize(400, 600); 30 setLocationRelativeTo(null); // 画面中央に表示 31 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // MyFrameが閉じたらプログラムも終了 32 33 add(panel1); 34 panel1.setBounds(30, 40, 100, 100); 35 panel1.setBackground(Color.GREEN); 36 for (int i = 0; i < 5; i++) { 37 line1[i] = new MyPanel(); 38 add(line1[i]); 39 line1[i].setBounds(30 + 25, 130 + i * 80, 50, 50); 40 line1[i].setBackground(Color.GRAY); 41 line1[i].setVisible(false); 42 } 43 44 add(panel2); 45 panel2.setBounds(150, 40, 100, 100); 46 panel2.setBackground(Color.YELLOW); 47 for (int i = 0; i < 5; i++) { 48 line2[i] = new MyPanel(); 49 add(line2[i]); 50 line2[i].setBounds(150 + 25, 130 + i * 80, 50, 50); 51 line2[i].setBackground(Color.GRAY); 52 line2[i].setVisible(false); 53 } 54 55 add(panel3); 56 panel3.setBounds(270, 40, 100, 100); 57 panel3.setBackground(Color.RED); 58 for (int i = 0; i < 5; i++) { 59 line3[i] = new MyPanel(); 60 add(line3[i]); 61 line3[i].setBounds(270 + 25, 130 + i * 80, 50, 50); 62 line3[i].setBackground(Color.GRAY); 63 line3[i].setVisible(false); 64 } 65 66 panel1.addMouseListener(new MouseAdapter() { 67 @Override public void mouseClicked(MouseEvent e) { 68 panel1MouseClicked(); 69 } 70 }); 71 panel2.addMouseListener(new MouseAdapter() { 72 @Override public void mouseClicked(MouseEvent e) { 73 panel2MouseClicked(); 74 } 75 }); 76 panel3.addMouseListener(new MouseAdapter() { 77 @Override public void mouseClicked(MouseEvent e) { 78 panel3MouseClicked(); 79 } 80 }); 81 82 Timer timer = new Timer(500, new ActionListener() { 83 @Override public void actionPerformed(ActionEvent e) { 84 update(); 85 } 86 }); 87 timer.start(); 88 } 89 90 private void update() { 91 if (line1[0].isVisible()) { 92 score -= 10; 93 System.out.println("SCORE:" + score); 94 panel1.setBackground(Color.GRAY); 95 } else { 96 panel1.setBackground(Color.GREEN); 97 } 98 99 // else if では同時に流れてきた場合、ひとつしか判定されないですよね? 100 // (そういう仕様ということかもしれませんが^^; 101 if (line2[0].isVisible()) { 102 score -= 10; 103 System.out.println("SCORE:" + score); 104 panel2.setBackground(Color.GRAY); 105 } else { 106 panel2.setBackground(Color.YELLOW); 107 } 108 109 if (line3[0].isVisible()) { 110 score -= 10; 111 System.out.println("SCORE:" + score); 112 panel3.setBackground(Color.GRAY); 113 } else { 114 panel3.setBackground(Color.RED); 115 } 116 117 for (int i = 0; i < 4; i++) { 118 boolean b1 = line1[i + 1].isVisible(); 119 line1[i].setVisible(b1); 120 boolean b2 = line2[i + 1].isVisible(); 121 line2[i].setVisible(b2); 122 boolean b3 = line3[i + 1].isVisible(); 123 line3[i].setVisible(b3); 124 } 125 126 line1[4].setVisible(Math.random() > 0.7); 127 line2[4].setVisible(Math.random() > 0.7); 128 line3[4].setVisible(Math.random() > 0.7); 129 } 130 131 private void panel1MouseClicked() { 132 if (line1[0].isVisible()) { 133 panel1.setBackground(Color.WHITE); 134 line1[0].setVisible(false); 135 score += 30; 136 } else { 137 panel1.setBackground(Color.GRAY); 138 score -= 5; 139 } 140 System.out.println("SCORE:" + score); 141 } 142 143 private void panel2MouseClicked() { 144 if (line2[0].isVisible()) { 145 panel2.setBackground(Color.WHITE); 146 line2[0].setVisible(false); 147 score += 30; 148 } else { 149 panel2.setBackground(Color.GRAY); 150 score -= 5; 151 } 152 System.out.println("SCORE:" + score); 153 } 154 155 private void panel3MouseClicked() { 156 if (line3[0].isVisible()) { 157 panel3.setBackground(Color.WHITE); 158 line3[0].setVisible(false); 159 score += 30; 160 } else { 161 panel3.setBackground(Color.GRAY); 162 score -= 5; 163 } 164 System.out.println("SCORE:" + score); 165 } 166 167 private static class MyPanel extends JPanel { 168 public MyPanel() { 169 170 // BevelBorder(立体的に見える縁)を2個重ねて付けた 171 Border border = new CompoundBorder( 172 new BevelBorder(BevelBorder.RAISED, Color.WHITE, Color.BLACK), 173 new BevelBorder(BevelBorder.RAISED, Color.WHITE, Color.BLACK)); 174 setBorder(border); 175 } 176 } 177}

投稿2021/05/28 08:52

TN8001

総合スコア9317

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問