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)
というエラーが同時に表示されます。長くすみません。解決案が一つでもおわかりでしたらお助け願います。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー