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

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

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

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

Q&A

解決済

1回答

8882閲覧

背景に画像を埋め込みたい

rink

総合スコア80

Java

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

0グッド

0クリップ

投稿2019/01/25 06:01

編集2019/01/25 06:21

背景に画像を入れ込もうとしていますが、エラーが出てしまいます。
コードは以下です。

java

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

エラーは以下の通りです。

java

1JFrameに適切なコンストラクタが見つかりません(ImageIcon) 2 myframe = new JFrame(backicon); 3 ^ 4 コンストラクタ JFrame.JFrame(GraphicsConfiguration)は使用できません 5 (引数の不一致: ImageIconGraphicsConfigurationに変換できません:) 6 コンストラクタ JFrame.JFrame(String)は使用できません 7 (引数の不一致: ImageIconStringに変換できません:) 8

java初心者です。よろしくお願いします。

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

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

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

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

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

m.ts10806

2019/01/25 06:14

冒頭のpackageやimportも含めてコード全体ご提示いただけますでしょうか。
rink

2019/01/25 06:18

了解しました。すいません…
rink

2019/01/25 06:23

実現したい点としましては、コメントアウトされている  /*ImageIcon backicon = new ImageIcon("./png/4.png"); myframe = new JFrame(backicon); myframe.setLayout(null); // does not use layout manager myframe.setSize(471,600); // window size : width = 400, height = 600 myframe.setVisible(true); // make the window visible //myframe.addWindowListener(new MyWindowAdapter()); */ です。
guest

回答1

0

ベストアンサー

Processing3.4で試しましたが

Java

1import javax.swing.*; 2JFrame mainFrame = new JFrame("サンプル"); 3mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 4mainFrame.setSize(320, 160); 5mainFrame.setLocationRelativeTo(null); 6JLabel background = new JLabel(new ImageIcon("画像パス")); 7mainFrame.add(background); 8mainFrame.setVisible(true);

でできましたよ

投稿2019/01/25 06:40

rururu3

総合スコア5545

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

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

rink

2019/01/25 11:02

貴方のおっしゃる通りに実行した所、コンパイルエラーは出なかったのですが、配列がどうも背景の後ろ側に入り込んでしまうようです。  どうすればよいでししょうか?
rink

2019/01/25 11:53

どうやらそうではなく、101行目のmyframe.add(Btn[j][q]);で、 Exception in thread "main" java.lang.NullPointerException at shooting2.main(shooting2.java:101) というエラーが出てしまっているようです。
rink

2019/01/25 12:04

このエラーへの対処方法が分かりました。すいませんでした…。とても分かりやすく教えてくださりありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問