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

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

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

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

Q&A

解決済

1回答

403閲覧

透明な配列を作りたい

rink

総合スコア80

Java

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

0グッド

0クリップ

投稿2019/01/25 12:54

java初心者です。
表示される背景画像の上に透明な配列を作りたくて挑戦しています。
いかにソースを貼ります。

java

1import java.awt.*; 2import java.awt.event.*; 3import javax.swing.ImageIcon; 4import javax.swing.*; 5import javax.swing.JButton; 6import javax.swing.JLabel; 7import javax.swing.JFrame; 8 9class YPanel extends Panel { 10 //Panel innerPanel3; 11 //Panel innerPanel4; 12 Color bgcolor; 13 static int puyomatrix[][]; 14 // static Panel mypanels[][]; 15 //int i,Y; 16 public YPanel() { 17 18 bgcolor = super.getBackground();//親クラスのBackgroundを参照しているらしい 19 } 20 public void setBounds(int x, int y, int width, int height) { 21 super.setBounds(x,y,width,height); 22 // innerPanel3.setBounds(4,height-4,width-8,8); 23 // innerPanel4.setBounds(width-8,4,8,height-8); 24 } 25 public Color getBackground() { 26 return bgcolor; 27 } 28 public void setBackground(Color color) { 29 bgcolor = color; 30 super.setBackground(color);//一連の動き確認 31 } 32} 33 34public class shooting2 { 35 static Frame myframe; 36 static JLabel mylabel; 37 static Panel mypanels[][]; 38 static int puyomatrix[][]; 39 static Color colorList[]; 40 static int score = 0; 41 static int puyoX = 3, puyoY = 1;//上の方のブロックのx座標とY座標 42 //static int puyoX2 = 3, puyoY2 = 2; 43 //static int puyoX = 9, puyoY = 2; 44 //static int puyoX2 = 9, puyoY2 = 3; 45 static int color3 = (int)(Math.random()*3)+2; 46 static int color4 = (int)(Math.random()*3)+2; 47 static int color1 = 0, color2 = 0; 48 static int rotate = 0; 49 static boolean lock = false; 50 static boolean lock2 = false; 51 52 static JButton[][] Btn;//ボタン 53 54 55/* static int puyoX3=9; 56 static int puyoY3=2; 57 static int puyoX4=9; 58 static int puyoY4=3;*/ 59 60 public static void main(String args[]) { 61 int i,x,y; 62 final int puyoSize = 32; 63 64 /*myframe = new Frame(); 65 myframe.setLayout(null); // does not use layout manager 66 myframe.setSize(400,600); // window size : width = 400, height = 600 67 myframe.setVisible(true); // make the window visible 68 //myframe.addWindowListener(new MyWindowAdapter()); 69 */ 70 71 JFrame myframe = new JFrame("ウンチシューティングゲーム"); 72 myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 73 myframe.setSize(471, 600); 74 myframe.setLocationRelativeTo(null); 75 JLabel background = new JLabel(new ImageIcon("./png/3.png")); 76 myframe.add(background); 77 myframe.setVisible(true); 78 79 /*ImageIcon backicon = new ImageIcon("./png/4.png"); 80 myframe = new JFrame(backicon); 81 myframe.setLayout(null); // does not use layout manager 82 myframe.setSize(471,600); // window size : width = 400, height = 600 83 myframe.setVisible(true); // make the window visible 84 //myframe.addWindowListener(new MyWindowAdapter()); 85 */ 86 87 ImageIcon labelicon = new ImageIcon("./png/3.png");//背景? 88 mylabel = new JLabel(labelicon); 89 mylabel.setLayout(null); // does not use layout manager 90 mylabel.setSize(400,600); // window size : width = 400, height = 600 91 mylabel.setVisible(true); // make the window visible 92 //mylabel.addWindowListener(new MyWindowAdapter()); 93 94 95 96 ImageIcon buttonicon = new ImageIcon("./png/1.png");//配列上に出る、透明のやつ 97 Btn = new JButton[13][6];//真ん中 98 for(int j = 0; j < 13; j ++) { 99 for(int q = 0; q < 6; q ++) { 100 Btn[j][q] = new JButton(buttonicon); 101 myframe.add(Btn[j][q]);//myframeにBtnを加える 102 Color transparent = new Color(0, 0, 0, 0); 103 Btn[j][q].setBackground(transparent); 104 Btn[j][q].setBounds(80+(32*q),200+(32*j),32,32); 105 } 106 } 107 mypanels = new Panel[13][6]; 108 puyomatrix = new int[13][6]; 109 colorList = new Color[5]; 110 colorList[0] = Color.gray; 111 colorList[1] = Color.white; 112 colorList[2] = Color.yellow; 113 colorList[3] = Color.red; 114 colorList[4] = Color.green; 115 116 for( x = 0; x < 6; x++ ) { 117 for( y = 0; y < 13; y++ ) { 118 Panel p = new YPanel(); 119 mypanels[y][x] = p; 120 myframe.add(p); 121 p.setBounds(80+puyoSize*x,200+puyoSize*y,puyoSize,puyoSize); 122 setpuyo(x,y,0); // no puyo is here 123 } 124 } 125 for( x = 0; x < 6; x++ ) { 126 setpuyo(x,12,1); // ground is here、 この値を221行目の仮引数に代入 127 } 128 129 for( y = 0; y < 12; y++ ) { 130 setpuyo(0,y,1); // wall is here 131 setpuyo(7,y,1); // wall is here 132 } 133 134 boolean firstPlacing = true; 135 boolean gameIsOver = false; 136 int fallCount = 0; 137 138 while( gameIsOver != true) { 139 lock = false; 140 sleep(100);//降りてくるのが遅くなる 141 lock = true; 142 while( lock2 ) { sleep(10); } 143 if( firstPlacing ) { 144 firstPlacing = false; 145 color1 = (int)(Math.random()*3)+2; 146 //color2 = (int)(Math.random()*3)+2; 147 rotate = 0; 148 puyoX = 3; puyoY = 1; // initialPlace 149 150 if( puyomatrix[puyoY][puyoX] == 0//上のぷよの座標が0になる=上に当たってしまった 151 ) {//下の‥↑ 152 setpuyo(puyoX,puyoY,color1); 153 //setpuyo(puyoX2,puyoY2,color2); 154 //System.out.print("next puyo is here.\n"); 155 } else { 156 // GAME OVER 157 // ImageIcon icon7 = new ImageIcon("./img/7.png"); 158 setpuyo(puyoX,puyoY,color1); 159 //setpuyo(puyoX2,puyoY2,color2); 160 System.out.print("batan kyu-\n"); 161 gameIsOver = true; 162 System.exit(0); 163 } 164 } 165 if( fallCount > 5 ) { 166 fallCount = 0; // counter reset; 167 puyoY++; // fall it for one block 168 //puyoY2++; 169 if( (rotate == 0 || puyomatrix[puyoY][puyoX] == 0) 170 ) { 171 setpuyo(puyoX,puyoY-1,0); 172 //setpuyo(puyoX2,puyoY2-1,0); 173 setpuyo(puyoX,puyoY,color1); 174 //setpuyo(puyoX2,puyoY2,color2); 175 } else { 176 firstPlacing = true; 177 // undo falling 178 puyoY--; 179 // puyoY2--; 180 // fall each puyo 181 while(rotate != 0 && puyomatrix[puyoY+1][puyoX] == 0) { 182 setpuyo(puyoX,puyoY,0); 183 puyoY++; 184 setpuyo(puyoX,puyoY,color1); 185 } 186 /*while(puyomatrix[puyoY2+1][puyoX2] == 0) { 187 setpuyo(puyoX2,puyoY2,0); 188 puyoY2++; 189 setpuyo(puyoX2,puyoY2,color2); 190 } 191 */ 192 while(rotate == 0 && puyomatrix[puyoY+1][puyoX] == 0) { 193 setpuyo(puyoX,puyoY,0); 194 puyoY++; 195 setpuyo(puyoX,puyoY,color1); 196 } 197 // clear connected puyos and falldown other puyos... 198 // !!--check here--!! 199 200 // !!--check here--!! 201 } 202 } else { 203 fallCount++; 204 } 205 } 206 207 } 208 209 210 static void setpuyo(int x, int y, int color) { 211 puyomatrix[y][x] = color; 212 mypanels[y][x].setBackground(colorList[color]); 213 } 214 static int getpuyo(int x, int y) { // why don't you use this!? 215 // System.out.print(puyomatrix[y][x]); 216 return puyomatrix[y][x]; 217} 218 219static void fallPuyos() { 220 int fallcount; 221 int nx,ny,nr; 222 223 do { 224 fallcount = 0; 225 int x,y; 226 for( y = 10; y > 0; y-- ) { 227 for( x = 1; x <= 6; x++ ) { 228 if( puyomatrix[y][x] != 0 && puyomatrix[y+1][x] == 0 ) { 229 setpuyo(x,y+1,puyomatrix[y][x]); // fall the puyo 230 setpuyo(x,y,0); // the last position should be empty 231 fallcount++; // count-up fallout counter 232 } 233 } 234 } 235 } while( fallcount != 0 ); 236 237} 238 239 240static void sleep(long msec) { 241 try{ 242 Thread.sleep(msec);//指定した時間の長さにわたって現在のスレッドを中断 243 }catch(InterruptedException ie) { 244 } 245} 246} 247

実行していただくとわかるのですが、配列のところがグレーでおおわれてしまいます。96行目の
ImageIcon buttonicon = new ImageIcon("./png/1.png");のところを
ImageIcon buttonicon = new ImageIcon("./png/5.png");に変えていただいて、少しウィンドウのサイズを大きくしてもらってから先ほどグレーとなっていたところをなぞっていただくと、配列が浮き出てくるという現象が起きてしまいます。浮き出てしまうのも止めたいのですが、自分の中では、背景の上にある透明な配列の上をブロックが落ちてくるはずなのですが、できません。

長くなってすみません。
僕が質問させていただきたいのは、
① なぜ配列がグレーになってしまうのか。(言い忘れていました。1.pngは透過させてある画像を用いていて、それを配列の上に置けば背景が配列を透けると考えました。まず根本からおかしいのでしょうか?)
② なぞると、なぜ配列が浮かんできてしまうのか。

実行すると、

java

1Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 2 at shooting2.setpuyo(shooting2.java:211) 3 at shooting2.main(shooting2.java:131)

というエラーが同時に表示されます。長くすみません。解決案が一つでもおわかりでしたらお助け願います。よろしくお願いします。

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

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

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

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

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

jimbe

2019/01/25 13:05

言葉の使われ方がプログラミング的に間違っています. コード上に xxx[] と書いた"配列"は, "透明" になったり"グレー"になったりするような, 画面に表示するモノではありません. しいて言うなら画面に表示するモノも入れておける"入れ物"です.
jimbe

2019/01/25 13:11

「実行していただくとわかるのですが」と言われましても, 作者さんの開発環境以外では ./png/1.png や ./png/5.png 等は無いことはご理解頂けるでしょうか.
rink

2019/01/25 13:18

確かにそうですよね…。 ありがとうございます。。
jimbe

2019/01/25 13:40

苦言だけではなんなので. for( y = 0; y < 12; y++ ) { setpuyo(0,y,1); // wall is here setpuyo(7,y,1); // wall is here } という箇所がありますが, この setpuyo メソッドは static void setpuyo(int x, int y, int color) { puyomatrix[y][x] = color; mypanels[y][x].setBackground(colorList[color]); } となっており, 二つの二次元配列は mypanels = new Panel[13][6]; puyomatrix = new int[13][6]; です. setpuyo(7,y,1); とした場合, 配列の範囲を超えています. それが Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 というエラーです. '7' という配列インデックスは範囲を越えているという意味です. コードのどの位置でエラーなのかも続けて表示されています.
jimbe

2019/01/25 13:51

実行できないので実際に確認できませんが, Java の GUI において swing と AWT のオブジェクトを混ぜるのは(Color等を除き)基本禁止です. 混ぜると表示が乱れます. このコードでは Panel が AWT なので, 対応する swing の JPanel に置き換えてください. 使われていませんが宣言されている myframe も, 将来使われるつもりでしたら, Frame ではなく JFrame に置き換えておいたほうが良いかもしれません.
jimbe

2019/01/25 14:06

見直しましたら, static Frame myframe; と宣言されているのは, main 内で JFrame myframe として使用されているんですね. これは宣言自体削除でしょうか.
rink

2019/01/25 15:19 編集

ご丁寧にありがとうございます!!! そうなんですね、swingとawtのオブジェクトは混ぜてはいけないのですね。お勉強になります。。 置き換えて実行してみたらうまく動くようになりました!!やる気も出てきました、頑張ります! ありがとうございました!!!
jimbe

2019/01/25 15:43

まだ作成途中ですがとりあえずおめでとうございます. 追記修正依頼で終わってしまうと解決済みに出来ないと思いますので, 修正したコードを載せた回答を書いて頂いて, それで解決済みにして頂いてよいでしょうか?
jimbe

2019/01/25 15:48

あ、私が上の記事をコピペしちゃえば良いんですね。
guest

回答1

0

ベストアンサー

java

1for( y = 0; y < 12; y++ ) { 2 setpuyo(0,y,1); // wall is here 3 setpuyo(7,y,1); // wall is here 4}

という箇所がありますが, この setpuyo メソッドは

java

1static void setpuyo(int x, int y, int color) { 2 puyomatrix[y][x] = color; 3 mypanels[y][x].setBackground(colorList[color]); 4}

となっており, 二つの二次元配列は

java

1mypanels = new Panel[13][6]; 2puyomatrix = new int[13][6];

です.
setpuyo(7,y,1); とした場合, 配列の範囲を超えています.
それが

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7

というエラーです.
'7' という配列インデックスは範囲を越えているという意味です.
コードのどの位置でエラーなのかも続けて表示されています.

Java の GUI において swing と AWT のオブジェクトを混ぜるのは(Color等を除き)基本禁止です. 混ぜると表示が乱れます.
このコードでは Panel が AWT なので, 対応する swing の JPanel に置き換えてください.

投稿2019/01/25 15:53

編集2019/01/25 15:56
jimbe

総合スコア12659

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

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

rink

2019/01/25 16:26

ご丁寧にありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問