• 締切済み

初心者です。Circle

class Circle { Paint pat; int xpos, ypos, radius; public Circle(Paint p, int x, int y, int r) { pat = p; xpos = x; ypos = y; radius = r; } public void draw(Graphics2D g) { g.setPoint(pat); g.fillCircle(xpos-radius, ypos-radius, radius); } } というソースを作りましたが、 コンパイルの際に 以下のエラーが出ます。 シンボルを見つけられません シンボル:メソッド fill Circle(int,int,int) 場所:g.fillCircle(xpos-radius, ypos-radius, radius); どこが間違っていて、どう直せばいいのか、教えてください。 お願いします。

  • Java
  • 回答数3
  • ありがとう数1

みんなの回答

  • ssr-y6
  • ベストアンサー率71% (5/7)
回答No.3

 以下のように変更/追加すれば円が書き出されるでしょう。 import java.awt.*; import java.awt.event.*; class Circle { Paint pat; int xpos, ypos, radius; public Circle(Paint p, int x, int y, int r) { pat = p; xpos = x; ypos = y; radius = r; }; public void draw(Graphics2D g) { // g.setPoint(pat); g.setPaint(pat); // g.fillCircle(xpos-radius, ypos-radius, radius); g.fillOval(xpos-radius, ypos-radius, radius*2, radius*2); }; } class drawcircle extends Frame { Circle C; public drawcircle() { super("Draw Circle"); setSize(200, 200); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); }; }); C = new Circle(new GradientPaint((float)0.0, (float)0.0, Color.RED, (float)10.0, (float)10.0, Color.BLUE, true), 100, 100, 50); }; public void paint(Graphics g) { C.draw((Graphics2D)g); }; public void update(Graphics g) { paint(g); }; public static void main(String args[]) { new drawcircle(); }; }

noname#49664
noname#49664
回答No.2

class Oval { Paint pat; int xpos, ypos, radius; public Oval(Paint p, int x, int y, int r) { pat = p; xpos = x; ypos = y; radius = r; } public void draw(Graphics2D g) { g.setPaint(pat); g.drawOval(xpos-radius, ypos-radius, 2*radius, 2*radius); } } ↑ 何度も読み返しましたが、何をやりたいのかがまったくわかりませんでした。Circleでも大丈夫なものもあるようだとか意味がわかりません。ひょっとして、何か他の言語と勘違いしているところがありませんか? とりあえず、Javaのごく基礎的な入門書などで初歩をきちんと勉強されることをお勧めします。Webサイトでも今はいい入門のコンテンツがたくさんありますから、とりあえずそうしたもので基礎的な部分だけでもしっかり学んでください。

noname#49664
noname#49664
回答No.1

Javaには、fillCircleというメソッドは存在しません。 円を描くなら、fillOvalです。 リファレンスぐらいはチェックしておきましょう。

参考URL:
http://java.sun.com/j2se/1.5.0/ja/docs/ja/api/index.html
stealther
質問者

お礼

まったく分からなくて質問させていただきました。 OvalじゃなくてもCircleでも大丈夫なものもあるみたいですが... 一応Ovalで以下のように自分なりに作り直してみましたが、 コンパイルはできましたが実際に円が描かれません。 どう直したらよいか、教えていただけますか。 class Oval { Paint pat; int xpos, ypos, radius; public Oval(Paint p, int x, int y, int r) { pat = p; xpos = x; ypos = y; radius = r; } public void draw(Graphics2D g) { g.setPaint(pat); g.drawOval(xpos-radius, ypos-radius, 2*radius, 2*radius); } }

関連するQ&A

  • 初心者です。 コンパイルのエラー

    import java.awt.*; import javax.swing.*; public class R11Sample1 extends JFrame { Rect r1 = new Rect(Color.red, 100, 100, 80, 60); Rect r2 = new Rect(new Color (0.5f, 1f, 0f, 0.7f), 150, 120, 60, 90); Oval = new Oval(Color.blue, 60, 50, 10, 10); JPanel panel = new JPanel() { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; r1.draw(g2); r2.draw(g2); } }; public R11Sample1() { setSize(400, 350); setDefaultCloseOperation(EXIT_ON_CLOSE); getContentPane().add(panel); } public static void main(String[] args) { new R11Sample1().setVisible (true); } } class Rect { Paint pat; int xpos, ypos, width, height; public Rect(Paint p, int x, int y, int w, int h) { pat = p; xpos = x; ypos = y; width = w; height = h; } public void draw(Graphics2D g) { g.setPaint(pat); g.fillRect(xpos-width/2, ypos-height/2, width, height); } } class Oval { Paint pat; int xpos, ypos, radius; public Oval(Paint p, int x, int y, int width, int height) { pat = p; xpos = x; ypos = y; width = w; height = h; } public void draw(Graphics2D g) { g.setPaint(pat); g.fillOval(xpos-width/2, ypos-height/2, width, height); } } これでコンパイルすると、 Identifierがありません といわれました。 どこを直せばいいのでしょうか。 また、全体的に間違ったところがあったら教えてください。

    • ベストアンサー
    • Java
  • 初心者です 三角形を描きます

    class Triangle { Paint pat; int xpoints0, ypoints0, xpoints1, ypoints1, xpoints2, ypoints2; public Triangle(Paint p, int x0, int y0, int x1, int y1, int x2, int y2) { pat = p; xpoints0 = x0; ypoints0 = y0; xpoints1 = x1; ypoints1 = y1; xpoints2 = x2; ypoints2 = y2; } public void draw(Graphics2D g) { g.setPaint(pat); g.fillTriangle( ); } } ここまでは書けたのですが、一番最後の g.fillTriangle( ); で、( )に何を書いたらいいのかが、分かりません。 教えてください!お願いします。

    • ベストアンサー
    • Java
  • アプレットの配列をテキスト形式で保存する

    ある画像の上をマウスクリックするとそこに赤いマーカーが表示され、さらにそのマーカーの座標をテキスト形式で保存できるプログラムを組みたいと考えています。マーカー表示はできましたが、保存方法がまったくわかりません。35箇所のマーカー座標を保存したいのですが…。 どなたか教えていただけませんでしょうか。 ブラウザ上で動作させたいので、JavaAppletにて組んでいます。 import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class sample extends Applet{ private int[] xpos = new int[35]; private int[] ypos = new int[35]; private int num = 0; Image imgs; public sample(){ addMouseListener(new MyMouseAdapter()); } public void init(){ imgs = getImage(getDocumentBase(),"sample.gif"); } public void paint(Graphics g){ g.drawImage(imgs,0,0,this); g.setColor(Color.red); for(int i = 0; i < num; i++){ g.fillOval(xpos[i]-2, ypos[i]-2, 4, 4); } } class MyMouseAdapter extends MouseAdapter { public void mouseClicked(MouseEvent e){ if(num >= 35){ return; } int x,y; x = e.getX(); y = e.getY(); xpos[num] = x; ypos[num] = y; num++; Graphics g = getGraphics(); g.setColor(Color.red); g.fillOval(x-2, y-2, 4, 4); } } }

  • http://csharpimage.blog60.fc2.com/b

    http://csharpimage.blog60.fc2.com/blog-entry-19.html をみて、直線補間をC言語で記述したのですが、結果が思う様にいきません。(_____) プログラムは以下です。間違っているのは、おそらく、座標を示すところだとはおもっているのですが、 そこをどうすればいいのか。。。。レベルが低くてごめんなさい(>_< どなたかご教授願います。 FILE *fpt; _wfopen_s(&fpt,Common_Data_Raw->filename,L"rb"); FILE *fpt_output; int width=Common_Data_Raw->width; int height=Common_Data_Raw->height; // 拡大縮小後の画像サイズ int hxSize=Common_Data_Raw->width_rescale;//拡大、縮小後の幅が入っている int hySize=Common_Data_Raw->height_rescale;//拡大、縮小後の高さが入っている。 // 拡大縮小用 int xSize=width; int ySize=height; double xpos, ypos; double hokanX = (double)(xSize - 1) / hxSize; double hokanY = (double)(ySize - 1) / hySize; int i,j; unsigned char **layer,**bufdata,**rescale; //メモリの確保→省略します。 if ((xSize < hxSize)&&(ySize<hySize)) { // X方向の補間 for (i=0;i<ySize;i++) { xpos = 0.0; for (j = 0; j < hxSize; j+=3) { bufdata[i][j] = (unsigned char)(((double)layer[i][(int)xpos + 1] - (double)layer[i][(int)xpos]) *(xpos - (int)xpos) +(double)layer[i][(int)xpos]); bufdata[i][j+1] = (unsigned char)(((double)layer[i][(int)xpos + 1+1] - (double)layer[i][(int)xpos+1]) *(xpos - (int)xpos) +(double)layer[i][(int)xpos+1]); bufdata[i][j+2] = (unsigned char)(((double)layer[i][(int)xpos + 1+2] - (double)layer[i][(int)xpos+2]) *(xpos - (int)xpos) +(double)layer[i][(int)xpos+2]); xpos += hokanX;} } // Y方向の補間 for (i = 0; i < hxSize; i+=3) {ypos = 0.0;for (j = 0; j <hySize; j++) {rescale[j][i] = (unsigned char)(((double)bufdata[(int)ypos + 1][i] - (double)bufdata[(int)ypos][i]) *(ypos - (int)ypos) + (double)bufdata[(int)ypos][i]);rescale[j][i+1] = (unsigned char)(((double)bufdata[(int)ypos + 1][i+1] - (double)bufdata[(int)ypos][i+1]) *(ypos - (int)ypos) + (double)bufdata[(int)ypos][i+1]); rescale[j][i+2] = (unsigned char)(((double)bufdata[(int)ypos + 1][i+2] - (double)bufdata[(int)ypos][i+2]) * (ypos - (int)ypos) + (double)bufdata[(int)ypos][i+2]); ypos += hokanY;}} } else { //rescaledata = SimpleRescaleImage(data, hxSize, hySize);

  • ドラッグアンドドロップアンドスムーズ移動

    x:0,y:0の座標に置いてあるムービークリップを 好きな場所にドラックができて話した瞬間x:0,y:0の座標に戻るという スクリプトが書きたいのですが、戻り方をだんだんとスムージングしていくような形で戻したいと思います。 以下をうまく組み合わせて実行したいと思っています。 どなたかご教授お願いします。 on (press) { this.startDrag(); } on (release) { this.stopDrag(); } onClipEvent (enterFrame) { xpos = 0; ypos = 0; this._x += (xpos-this._x)/2; this._y += (ypos-this._y)/2; }

    • ベストアンサー
    • Flash
  • picからsg12864を制御しています。

    picからsg12864を制御しています。 直線の描画を行った際、まっすぐな縦線が点線のようになってしまいます。 横線・斜線は問題なく描画できます。また、ビットマップをそのまま表示した場合も問題なく描画できます。 後閑さんのプログラムを使用しています。下記のlcd_Lineを使用しています。 for文を使用して、1秒間隔でlcd_Pixelで点を打って直線を描画て動作を確認したところ、一度は黒いドットが表示されますが、消えてしまう箇所がありました。消えてしまう箇所はいつも同じです。 どなたか、解決方法がわかる方はいらっしゃいませんでしょうか? #define abs(a) (((a)>0) ? (a) : -(a)) void lcd_Line(int x0, int y0, int x1, int y1) { int steep, t; int deltax, deltay, error; int x, y; int ystep; /// 差分の大きいほうを求める steep = (abs(y1 - y0) > abs(x1 - x0)); /// x、yの入れ替え if(steep){ t = x0; x0 = y0; y0 = t; t = x1; x1 = y1; y1 = t; } if(x0 > x1) { t = x0; x0 = x1; x1 = t; t = y0; y0 = y1; y1 = t; } deltax = x1 - x0; // 傾き計算 deltay = abs(y1 - y0); error = 0; y = y0; /// 傾きでステップの正負を切り替え if(y0 < y1) ystep = 1; else ystep = -1; /// 直線を点で描画 for(x=x0; x<x1; x++) { if(steep) lcd_Pixel(y,x,1); else lcd_Pixel(x,y,1); error += deltay; if((error << 1) >= deltax) { y += ystep; error -= deltax; } } } void lcd_Pixel(int Xpos, int Ypos, char On){ char cs, data, page, pos, count, i; /* if colum >127 then do nothing */ if(Xpos<128){ if(Xpos>63){ // 64=<colum<=127? Xpos = Xpos-64; // shift 64 dot cs = 1; } else cs = 2; page = (char)(7-Ypos/8); // set page lcd_Write(cs, 0xB8+page, 1); lcd_Write(cs, 0x40+Xpos, 1); // set colum data = lcd_Read(cs,0); // get current data lcd_Write(cs, 0x40+Xpos, 1); // set colum ???? data = lcd_Read(cs,0); // get current data ???? pos =1; // set bit position count = (char)(7-Ypos%8); // set bit for(i=0; i<count; i++) // caluculate 2^n pos *= 2; lcd_Write(cs, 0x40+Xpos, 1); // back address if(On==1) // set or reset bit lcd_Write(cs, data | pos, 0); // set 1 else lcd_Write(cs, data & ~pos, 0); // set 0 } }

  • シンボルを見つけられません

    javaについて勉強しているのですが、円の中に四角を作るというプログラムを作ろうとしたところ tes.java:43:シンボルを見つけられません。 シンボル:メソッド drawSquare(int,int,int,java.awt.Graphics) 場所:Assortのクラス a.drawSquare(x,y,w,g); ^ というエラーが出てコンパイルできません。drawCircleからdrawSquareを実行するようにしたいのですが、どうしたらよいか教えていただけないでしょうか。 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class tes { static MyCanvas mc; public static void main(String[] args) { tes ac_listener = new tes(); JFrame jf = new JFrame("tes"); JPanel p = new JPanel(); mc = new MyCanvas(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mc.setPreferredSize(new Dimension(700,700)); jf.getContentPane().add(mc); p.add(mc); jf.getContentPane().add(p); jf.pack(); jf.setVisible(true); } } abstract class Assort{ double x,y; } class Square extends Assort{ public void drawSquare(int x, int y, int w, Graphics g) { double k=Math.sqrt(2); double _w=w/2/k; g.drawRect(x-(int)_w/2, y-(int)_w/2, (int)_w, (int)_w); } } abstract class Circle extends Assort{ } class Circle1 extends Circle { Assort a; Circle1(Assort _a) { a = _a; } void drawCircle(int x, int y, int w, Graphics g){ g.drawOval(x-w/4,y-w/4,w/2,w/2); a.drawSquare(x,y,w,g); } } class MyCanvas extends JPanel { public void paintComponent( Graphics g ) { super.paintComponent(g); Circle1 d1 = new Circle1 ( new Square() ); d1.drawCircle(350,350,700,g); } }

    • ベストアンサー
    • Java
  • Bresenhamのアルゴリズムを用いた円描画

    一つ下の質問をした後に、いろいろ考えて作ったプログラムなのですが、コンパイルが通りません。 頭がこんがらがってしまったので、教えていただけるとうれしいです。 main(){    int x, y, r, d;    printf("Enter coordinates of radius of circle. R = ");    scanf("%d\n", &r);    x = 0;    y = r;    d = 3 - 2 * r;    while(y > x){      plotcircle(x, y, r);      if(d < 0){ /* select U */         d = d + 4 * x + 6;      }else{ /* select D */         d = d + 4 * (x - y) + 10;         y = y - 1;      }      x = x + 1;    }    if(x == y){      plotcircle(x, y, r);    } } plotcircle(int x, int y, int r){    putpixel( x, y, r);    putpixel( y, x, r);    putpixel( y, -x, r);    putpixel( x, -y, r);    putpixel(-x, -y, r);    putpixel(-y, -x, r);    putpixel(-y, x, r);    putpixel(-x, y, r); }

  • Java・プログラムが正常に動きません。

    Javaで、シミュレーションを行った結果を出力するようなプログラムを作っています。コンパイルはできるのですが、Exceptionn in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 というようなメッセージが出てしまいます。 とても初心者で恥ずかしいですがソースはこうです。 import java.awt.*; import java.awt.event.*; public class TestFrame extends Frame{ public static void main(String [] args){ new TestFrame(); } TestFrame(){ super ("TestFrame"); setSize(500,500); setLayout(new BorderLayout()); MyCanvas mc1 = new MyCanvas(); add(mc1, BorderLayout.CENTER); setVisible(true); } } class MyCanvas extends Canvas{ static boolean upsidedown; Color fClr = Color.blue; Color bClr = Color.pink;; int by = 30; int t=0; int R=5; int n=2; public void paint(Graphics g){ Dimension d = getSize(); int radius = R*by; int ox = d.width/2-radius; int oy = d.height/2-radius; g.setColor(bClr); g.fillOval(ox, oy, radius*2,radius*2); Test test = new Test(); ReturnData data = test.method(t,R,n); int i; double x1[], y1[]; x1 = new double [n]; y1 = new double [n]; int x[], y[]; x = new int [n]; y = new int [n]; for(i=0;i<n;i++){ x1[i] = data.x[i]*by + d.width/2; y1[i] = data.y[i]*by + d.height/2; x[i] = (int) x1[i]; y[i] = (int) y1[i]; } for(i=0;i<n;i++){ g.setColor(fClr); g.fillOval(x[i], y[i], 5, 5); } } } ピンクの丸の上に、Test.classで計算した内容を返して青い点を表示させたいのですが、青い点は表示されません。でもなぜかn=1にすると表示されます。 原因の分かりそうな方いらっしゃいませんでしょうか?お願いします。

  • direct3dで当たり判定

    現在direct3dでフライトゲームを作っており、当たり判定部分を考えているのですがうまくいきません。メタセコイアでxファイルを作成し、ビルボードで表示している(敵も自弾も)のですが、そもそも基準点はどこに来るのか、プログラム上で指定する縦横高さがわからないなど当たり判定以前の問題のような気もするのですが・・・。とりあえず、該当部分のコードを載せるのでご享受お願いします。 ↓ゲームメインの攻撃部分↓ if(GetKeyboardTrigger(DIK_Z)){ _for(int i = 0; i < ATTACKNUM; i++){ __if( man_bullet[i].flag.bullet == 0){ ___man_bullet[i].flag.bullet = 1; ___//自機の座標セット ___mat_world[15+i]._41 = mat_world[0]._41; ___mat_world[15+i]._42 = mat_world[0]._42+2; ___mat_world[15+i]._43 = mat_world[0]._43; ___break; __} _} } for(int i = 0; i < ATTACKNUM; i++){ _if(man_bullet[i].flag.bullet == 1){ __mat_world[15+i]._43 ++; __man_bullet[i].cnt.bullet++; __if(man_bullet[i].cnt.bullet >= 100){ ___man_bullet[i].flag.bullet = 0; ___man_bullet[i].cnt.bullet = 0; ___mat_world[15+i]._42 = -100; __} __for(int j = 0; j < ENEMYNUM; j++){ ___man_bullet[i].flag.atk_result = ___Hit_Judging(&mat_world[5+j]._41, &mat_world[5+j]._42, &mat_world[5+j]._43, ________enemy[j].x, enemy[j].y, enemy[j].z, ________&mat_world[15+i]._41 , &mat_world[15+i]._42, &mat_world[15+i]._43, ________man_bullet[i].x, man_bullet[i].y, man_bullet[i].z ); ___if(man_bullet[i].flag.atk_result == 1){ ____man_bullet[i].flag.atk_result = 0; ____man_bullet[i].flag.bullet = 0; ____man_bullet[i].cnt.bullet = 0; ____mat_world[5+j]._43 = 150; ____mat_world[15+i]._42 = -100; ____break; ___} __} _} } ↓当たり判定部分↓ int Hit_Judging( float *xpos1 , float *ypos1 , float *zpos1 , ________float x1, float y1, float z1, ________float *xpos2 , float *ypos2 , float *zpos2 , ________float x2, float y2, float z2 ) { _for(int i = 0; i < ATTACKNUM; i++){ __if(man_bullet[i].flag.bullet == 1){ ___for(int j = 0; j < ENEMYNUM; j++){ ____if( ((xpos1[j] - (x1/2) ) < (xpos2[i] + (x2/2) ) && ______(xpos1[j] + (x1/2) ) > (xpos2[i] - (x2/2) )) && ______((ypos1[j] + y1 ) < ypos2[i] && ypos1[j] > (ypos2[i] + y2 )) && _______(zpos1[j] == zpos2[i]) ){ _____return 1; ____}else _____return 0; ___} __} _} }

専門家に質問してみよう