• 締切済み

エラーの対処法を教えてください2

続き a[0][0] = a[5][5]; a[0][6] = a[5][1]; a[6][0] = a[1][5]; a[6][6] = a[1][1]; for (int i=1; i<6; i++){ for (int j=1; j<6; j++){ if(a[i][j] == 0){ g.setColor(Color.black); g.fillRect(40 + i * 10, 40 + j * 10, 10, 10); }else{ g.setColor(Color.white); g.fillRect(40 + i * 10, 40 + j * 10, 10, 10); } } } repaint(); } } どなたかお教えください。

みんなの回答

回答No.1

前の方もご回答頂いたとおり、このソースは、Javaソースです。 public Window(int width, int height){ setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(width, height); setVisible(true); } public static void main(String[] args) { Window window = new Window(640, 480); } Windowはメソッドとして宣言していますが、main内にインスタンスとして生成しているように見えるので、 そもそものソースがおかしいので、全体を見直した方が良いと思います。 単純にご質問された内容を回答すると、 public void Window(int width, int height){ にすれば良いかと思います。

関連するQ&A

  • エラーの対処法を教えてください

    メソッドの宣言が不正です。戻り値の型が必要です。 public Window(int width, int height){ というエラーメッセージが出たのですが解決方法をどなたかお教えください。 import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class Kadai extends JFrame implements ActionListener { private Timer timer; private int a[][] = new int[7][7]; private int b[][] = new int[7][7]; public Window(int width, int height){ setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(width, height); setVisible(true); } public static void main(String[] args) { Window window = new Window(640, 480); } public void paint(Graphics g){ g.setColor(Color.black); for (int i=5; i<26; i++){ g.drawLine(i * 10, 50, i * 10, 250); g.drawLine(50, i * 10, 250, i * 10); } } public static void main(String args[]) { Random rand = new Random(); for (int i=1; i<6; i++){ for (int j=1; j<6; j++){ a[i][j] = rand.nextInt(2); } } for (int i=1; i<6; i++){ a[i][0] = a[i][5]; a[0][i] = a[5][i]; a[i][6] = a[i][1]; a[6][i] = a[1][i]; } a[0][0] = a[5][5]; a[0][6] = a[5][1]; a[6][0] = a[1][5]; a[6][6] = a[1][1]; for (int i=1; i<6; i++){ for (int j=1; j<6; j++){ if(a[i][j] == 0){ g.setColor(Color.black); g.fillRect(40 + i * 10, 40 + j * 10, 10, 10); }else{ g.setColor(Color.white); g.fillRect(40 + i * 10, 40 + j * 10, 10, 10); } } } } public Kadai() { timer = new Timer( 50, this ); timer.start(); } public void actionPerformed( ActionEvent e ) { for (int i=1; i<6; i++){ for (int j=1; j<6; j++){ if(a[i][j] = 0){ if(a[i-1][j-1]+a[i-1][j]+a[i-1][j+1]+a[i][j-1]+a[i][j+1]+a[i+1][j-1]+a[i+1][j]+a[i+1][j+1] == 2 && a[i-1][j-1]+a[i-1][j]+a[i-1][j+1]+a[i][j-1]+a[i][j+1]+a[i+1][j-1]+a[i+1][j]+a[i+1][j+1] == 3){ b[i][j] = 0; }else{ b[i][j] = 1; } }else{ if(a[i-1][j-1]+a[i-1][j]+a[i-1][j+1]+a[i][j-1]+a[i][j+1]+a[i+1][j-1]+a[i+1][j]+a[i+1][j+1] == 3){ b[i][j] = 0; }else{ b[i][j] = 1; } } } } for (int i=1; i<6; i++){ for (int j=1; j<6; j++){ b[i][j] = a[i][j]; } } for (int i=1; i<6; i++){ a[i][0] = a[i][5]; a[0][i] = a[5][i]; a[i][6] = a[i][1]; a[6][i] = a[1][i]; } 2に続きます。

  • ライフゲームにボタンを押すと何度も自動で更新されるように作りたいのです

    ライフゲームにボタンを押すと何度も自動で更新されるように作りたいのですが ↓を変えて更新されるようにするにはどうすればいいですか? public class Lifegame extends Applet implements ActionListener{ int hani = 30; int xy = 15; int sikaku = xy-2; int [][]a = new int[hani][hani]; int [][]b = new int[hani][hani]; Button button; public void init(){ for(int i=0; i < hani; i++) { for(int j = 0;j < hani; j++) { if(i==0 || j == 0 || i == hani || j == hani){ b[i][j] =0; }else{ b[i][j] = (int)(Math.random()*2); } } } button = new Button("更新"); button.addActionListener(this); add(button); } public void paint(Graphics g){ for(int i = 1; i < hani - 1; i++){ for(int j = 1; j < hani - 1; j++){ if(b[i][j] == 1){ g.setColor(Color.green); g.fillRect(i*xy, 30+j*xy, sikaku, sikaku); }else if(b[i][j] == 0){ g.setColor(Color.black); g.fillRect(i*xy, 30+j*xy, sikaku, sikaku); } } } } public void actionPerformed(ActionEvent arg0){ for(int i = 1; i < hani - 1; i++){ for(int j = 1; j < hani - 1; j++){ a[i][j] = b[i][j]; } } for(int i = 1; i < hani - 1; i++) { for(int j = 1; j < hani - 1; j++) { int hantei = a[i - 1][j - 1] + a[i - 1][j] + a[i - 1][j + 1] + a[i][j-1] + a[i][j + 1] + a[i+1][j-1] + a[i+1][j] + a[i+1][j+1]; if(b[i][j] == 1){ if(hantei == 2 || hantei == 3){ b[i][j] = 1; }else{ b[i][j] = 0; } }else if (b[i][j] == 0){ if(hantei == 3){ b[i][j] = 1; }else{ b[i][j] = 0; } } } } repaint(); } } ご回答よろしくお願いします。

    • ベストアンサー
    • Java
  • javaのエラーの意味がわかりません、お願いします

    今日は、javaを勉強している初心者です。 以下のコードを書いてやりましたが、「sleep(Graphics)は引数()に適用できません」、「Color.Whiteを解決できません」とエラーがでます、エラーの意味が分かりません。 一体何処が間違っているのでしょうか、宜しくお願いします。 ===================================================================== public class ani_Moving_Ball extends Applet { int x; public void paint(Graphics g) { for(x=0 ; x<180 ; ++x) { clear(g); g.drawOval(x,90,19,19); sleep(); } } public void clear(Graphics g) { g.setColor(Color.White); g.fillRect(0,80,200,40); g.setColor(Color.Black); } public void sleep(Graphics g) { double s=0.0; for (int j=1 ; j<100 ; ++j) { for (int k=1 ; k<100 ; ++k) { s =+ Math.sin((double)j); } } } }

    • ベストアンサー
    • Java
  • Start:アプレットは初期化されていません、、というエラーが出る。

    アプレットを勉強している初心者です。 落ちものゲームを作ろうと思い、本に載っているサンプルを分かるところから書いていこうとしています。 そうしたら(もちろんまだ全体の途中、というか始めたばかりですが)タイトルのようなエラーが出てしまいました。 今の段階でのソースを載せるので、もし分かる方がいたら、よろしくお願いします。 import java.applet.Applet; import java.awt.*; import java.util.*; import java.awt.event.*; public class jisaku extends Applet{ int square = 8; int nw = 23; int nh = 24;//ブロック位置の最大数 int block[][] = new int[4][4];//動くブロックの形 int board[][] = new int[nh+1][nw+1];//盤面にあるブロックの有無 Dimension d;//表示スクリーン Image offs;//オフスクリーン Graphics grf; public void init(){ //********盤面のブロックの初期化(board[][]の初期化(=フィールドと枠)だけ)************ //フィールドにはブロックはない for(int i = 0; i <= nh; i++){ for(int j = 0; i <= nw; j++){ board[i][j] = 0;//盤面にはブロックはない } } //左右下の固定ブロック for(int i = 0; i <= nh; i++){ for(int j = 0; j <= nw; j++){ board[i][j] = 2; board[i][nw-j] = 1; } } for(int i = 0; i < 3; i++){ for(int j = 0; j <= nw; j++){ board[nh-i][j] = 2; } } //************オフスクリーンの設定********* d = getSize(); offs = createImage(d.width, d.height); grf = offs.getGraphics(); } public void paint(Graphics g){ update(g); } public void update(Graphics g){ //[grf]はオフスクリーンのグラフィックスオブジェクト(というのかな?) //バックをオレンジで塗る grf.setColor(Color.orange); grf.fillRect(0,0,d.width,d.height); //ブロックを描く grf.setColor(Color.blue); for(int i = 0; i <= nh; i++){ for(int j = 0; j <= nw; j++){ if(board[i][j] >= 1){ grf.fillRect(j*square, i*square, square, square); } } } //左右下の枠を描く grf.setColor(Color.red); for(int i = 0; i <= nh; i++){ for(int j = 0; j <3; j++){ grf.fillRect(j*square, i*square, square, square); grf.fillRect((nw-j)*square, i*square, square, square); } } for(int i =0; i < 3; i++){ for(int j = 0; j <= nw; j++){ grf.fillRect(j*square, (nh-i)*square, square, square); } } g.drawImage(offs, 0, 0, this); } }

    • ベストアンサー
    • Java
  • エラーも無いのにボールが動きません

     JAVA初心者です、宜しくお願いします。  今晩は、以下のようなコードを書いてやりましたが、動きません。  何故でしょう、特にエラーははいていません。  原因が分かりません、宜しくお願いします。 =============================================================== public class Ball_1 extends Applet implements Runnable { Thread thr = null; Color BGcolor; int appWidth; int appHeight; int x; int vel; public void inti() { BGcolor = Color.white; appWidth = 400; appHeight = 400; x = 30; vel = 20; } public void paint(Graphics g) { g.setColor(BGcolor); g.fillRect(0,0,appWidth,appHeight); g.setColor(Color.blue); g.fillOval(x,20,100,100); } public void start() { if(thr == null) { thr = new Thread(this); thr.start(); } } public void stop() { if(thr != null) { thr.stop(); thr = null; } } public void run() { while(true) { x = x + vel; if(x > appWidth) { x = x - appWidth; } repaint(); try { thr.sleep(50); } catch(InterruptedException e) { thr.stop(); } } } }

    • ベストアンサー
    • Java
  • <identifier>エラーについて

    paintメソッド内ではなく、public class直下で図形の表示をしなければならないのですが、どうしても <identifier>がありません とエラーが出てしまいます; 調べてみたのですがわからなかったので指摘をお願いしますorz プログラムは以下の通りです /* <applet code="Reidai25kai.class" width=500 height=600> </applet> */ import java.applet.Applet; import java.awt.*; public class kadaiM extends Applet implements Runnable { int xichi = 100, yichi = 250, xido = 3, yido = 4; int i, mx=225, my=495; int[] bx = {100,150,200,250,300,350, 100,150,200,250,300,350, 100,150,200,250,300,350}; int[] by = {250,250,250,250,250,250, 300,300,300,300,300,300, 350,350,350,350,350,350}; g.setColor(new Color( 0, 255, 0)); g.fillRect(mx, my, mx+50, my+10); g.drawRect(0, 0, 450, 550); for(i=0; i<18; i++) { g.setColor(new Color(255,170,10*i)); g.fillOval(bx[i]-20,by[i]-20,50,50); } public void init() { Thread hyoji; hyoji = new Thread(this); hyoji.start(); } public void run() { for( ; ; ) { try { xichi = xichi + xido; //ボールx座標の更新 yichi = yichi + yido; //ボールy座標の更新 repaint(); if(xichi < 10 || xichi > 440)xido = -xido; //横方向跳ね返り if(yichi < 10 || yichi > 540)yido = -yido; //縦方向跳ね返り Thread.sleep(30); } catch(InterruptedException e){} } } public void paint(Graphics g) { g.setColor(new Color( 0, 0, 255)); g.fillOval(xichi-10, yichi-10, 20, 20); } }

  • 意味がわかりません

    この部分の意味がわからなくて困っています。わかる方いましたら解説をお願いします。m(_ _)m public void paint(Graphics g){ int w,h; w = (int)getSize().width; h = (int)getSize().height; if(f==0){ bufferGraphics.drawImage(image,0,0,this); bufferGraphics.setColor(Color.white); for(int i=0;i<h;i+=2){ bufferGraphics.drawLine(0,i,w,i); } g.drawImage(buffer,0,0,null); f = 1; } else if(f==1) { bufferGraphics.drawImage(image,0,0,this); bufferGraphics.setColor(Color.white); for(int i=1;i<h;i+=2){ bufferGraphics.drawLine(0,i,w,i); } g.drawImage(buffer,0,0,null); f = 0; } }

    • ベストアンサー
    • Java
  • 数字を動くプログラムについて

    番組の最後に流れるスタッフロールのように 数字が左から右に動くプログラムを以下のようなかんじで作りました。 class MyDrawPanel extends JPanel{ public void paintComponent(Graphics g){ g.setColor(Color.WHITE); g.fillRect(0,0,this.getWidth(),this.getHeight()); g.setColor(Color.BLACK); g.setFont(font); g.drawString("12345",x,y); } } というコンポーネントを x=-200; for(int i=0;i<500;i++){ x++; drawPanel.repaint(); try{ Thread.sleep(1); }catch(Exception ex){ } }というふうに動かしました。 ふたつ質問があります。 1.異なるスペックのパソコンで実行してみたところ表示(動く)スピードに大きな差が出ました。  これはしょうがないことなのでしょうか?(解決する方法があるのでしょうか?) 2.インクリメントをする際にThread.sleepで1msec休止させていますが   これをもっと短くすることはできないのでしょうか?   動くスピードが遅いのでもう少し速くしたいです。  (xのインクリメントをx+=2でやってみましたがこれくらいしか方法はないのでしょうか?)

    • ベストアンサー
    • Java
  • 「シンボルを見つけれません」というエラーへの対処法

    下記のコードの If(type.compareTo("名詞") == 0); で「シンボルを見つけれません」「シンボル:メソッドif(boolean)」というエラーが出てしまいます。 なぜ、他のif文ではエラーが出ないのにこの行ではエラーが出るのか分かりません。 対処法を教えていただけないでしょうか? public void set_dictionary(String dict_filename) { } public int run(byte[] text, int start, int length, byte[] terms) { String type; //品詞の比較用 byte[] buf1 = new byte[1000]; //textの内容を一時的に格納しておくための配列1 byte[] buf2 = new byte[1000]; //textの内容を一時的に格納しておくための配列2 int i, j, k, count1, count2, FLG; count1 = 0; count2 = 0; k = 0; FLG = 0; for(i = start; i < length; i++) { if(text[i] == '\t') { FLG = 1; count2 = 0; } else if(text[i] == ' ') { FLG = 0; count1 = 0; } else { if(FLG == 1) { buf2[count2++] = text[i]; } else if(FLG == 0) { buf1[count1++] = text[i]; } } if((count2 == 5)&&(FLG == 1)) { type = new String(buf2, 0, 4); If(type.compareTo("名詞") == 0); { for(j = 0; j < count1; j++) { terms[k++] = buf1[j]; } terms[k++] = '/'; } count2 = 0; FLG = 2; } } return k; //term中のデータの長さを返す }

  • appletを使ったJavaプログラミング

    Java初心者なのですが、ランダムユニークナンバーをarrayに代入し、長方形の高さが全て違う様に表記したいのですが、全く表記出来ません。 おそらく、arrayに代入する時点までは大丈夫だとは思うのですが、どのようにしたら長方形に高さを代入してappletで表記出来るのでしょうか? import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.Random; import java.io.*; public class GUI extends Applet { public void update(Graphics g) { paint(g); } Button button = new Button("Sort Me"); Label text = new Label("Pink: Selection Sort" + "\n" + "Green: Inseration Sort"); int[] store = new int[20]; public static int[] findValue(int [] store){ int rand; for (int i = 0; i < store.length; i ++){ do{ rand = (int)(Math.random()*21)+10; } while(doesExists(rand, store, i)); store[i] = rand; } return store; } private static boolean doesExists(int rand, int[] arr, int i){ if(i != 0){ for(int j =0; j < i; j++){ if(rand == arr[j]){ return true; } } } return false; } int Counter = 0; int xScale = 0; public void displayRectangles(Graphics g) { if(Counter < 20) { xScale += 15; int x = 80 + xScale; int H = store[Counter]; g.setColor(Color.pink); g.fillRect(x, (140 - H), 10, H); g.setColor(Color.green); g.fillRect(x, 140, 10, H); Counter++; } } public void init() { setSize(500, 350); setBackground(Color.WHITE); add(button); add(text); button.addActionListener(new buttonHandler()); } int c = 0; public void paint(Graphics g) { c++; displayRectangles(g); if(c < 20) { repaint(); } } int count = 0; class buttonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ count ++; button.setLabel("pass " + count); if(e.getActionCommand()=="pass") repaint(); } } } 最終的にはボタンをクリックするたびに、長方形の長さが右側になるにつれて大きくなってソートされます。 http://hills.ccsf.cc.ca.us/~cconner/Java/Sorts/SortsWithButton.html どうしたら良いのかどうしても分かりません。 よろしくお願い致します。

    • ベストアンサー
    • Java

専門家に質問してみよう