/* iアプリ, MIDP での円弧の描画. 拡張 API または三角関数の数表 Time-stamp: "2003/11/13 Thu 19:16 hig" Saburo Higuchi 2003 http://www.math.ryukoku.ac.jp/~hig/ プログラム解説等 http://sparrow.math.ryukoku.ac.jp/~hig/course/juniors_2003/07/ */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; /** IApplication または MIDlet を継承するクラス */ public class ArcSample extends MIDlet { /** 起動するときに呼ばれる メソッド. 必須. */ public void startApp(){ ArcCanvas cc = new ArcCanvas(this); Display.getDisplay(this).setCurrent(cc); } public void pauseApp(){} public void destroyApp(boolean unconditional){} } class ArcCanvas extends Canvas implements Runnable{ MIDlet parent; int radius; int theta=0; Graphics offgr; Image offimg; ArcCanvas(MIDlet parent){ this.parent=parent; radius=getHeight()/2; if( radius > getWidth()/2 ){ radius=getWidth()/2; } Thread th = new Thread(this); th.start(); } public void paint(Graphics g){ // 画面をいったん固定(ダブルバッファリング) // offimg=Image.createImage(getWidth(),getHeight());//midp // offgr=offimg.getGraphics(); // midp // 最後の drawImage とペアでアンコメントし, // 下の g. を offgr. としてみよう. g.setColor(( (255<<16) + (255<<8) + (255)) ); g.fillRect(0,0,getWidth(),getHeight()); // 画面を消す g.setColor(0 ); // 数表による円弧 for(int i=0; i < theta ; i+=5){ /* (x1,y1) と (x2,y2) の間に線分をひく */ g.drawLine( radius + (radius * cos(i)) /magfactor,//x1 radius - (radius * sin(i)) /magfactor,//y1 radius + (radius * cos(i+5)) /magfactor,//x2 radius - (radius * sin(i+5)) /magfactor//y2 ); /* g.drawLine( radius + radius * (cos(i) /magfactor) ,//x1 radius - radius * (sin(i) /magfactor) ,//y1 radius + radius * (cos(i+5) /magfactor),//x2 radius - radius * (sin(i+5))/magfactor) //y2 ); だとどうなるかな */ } g.fillRect(radius + (radius * cos(theta)) / magfactor, radius - (radius * sin(theta)) / magfactor, 4,4); // DoJa3.0 または MIDP ならアンコメントしてみよう. g.setColor(( (255<<16) + (0<<8) + (0)) ); g.drawArc(radius/2,radius/2,radius,radius,0,theta); //画面を一括表示(ダブルバッファリング終了) // g.drawImage(offimg,0,0,g.LEFT|g.TOP); } /** スレッドが start されると このメソッドに制御が移る */ public void run(){ while(true){ // 無限ループ theta=(theta+10) % period; // 角度を進め repaint(); // paint() を再度呼び出す. try { Thread.sleep(100); // 100ミリ秒休む. } catch ( Exception e ) { // 割り込みがあっても何もしない. } repaint(); } } // Vodafone の場合, FixedPoint クラスの中で sin, cos などは提供されている. int sin(int n){ while( n<0 ){ n+=period; } return sin[n%period]; } int cos(int n){ while( n<0 ){ n+=period; } return cos[n%period]; } /** 2pi に相当する 度 */ private final int period=360; /** 分母 */ private final int magfactor=100000; // 100000.0*sin(n度) を長さ 360 の整数配列にしたもの // 本当は, sin の始め90個から sin, cos すべてを簡単に計算できるはず. private final int sin[]={ 0,1745,3489,5233,6975,8715,10452,12186,13917,15643, 17364,19080,20791,22495,24192,25881,27563,29237,30901,32556, 34202,35836,37460,39073,40673,42261,43837,45399,46947,48480, 49999,51503,52991,54463,55919,57357,58778,60181,61566,62932, 64278,65605,66913,68199,69465,70710,71933,73135,74314,75470, 76604,77714,78801,79863,80901,81915,82903,83867,84804,85716, 86602,87461,88294,89100,89879,90630,91354,92050,92718,93358, 93969,94551,95105,95630,96126,96592,97029,97437,97814,98162, 98480,98768,99026,99254,99452,99619,99756,99862,99939,99984, 100000,99984,99939,99862,99756,99619,99452,99254,99026,98768, 98480,98162,97814,97437,97029,96592,96126,95630,95105,94551, 93969,93358,92718,92050,91354,90630,89879,89100,88294,87461, 86602,85716,84804,83867,82903,81915,80901,79863,78801,77714, 76604,75470,74314,73135,71933,70710,69465,68199,66913,65605, 64278,62932,61566,60181,58778,57357,55919,54463,52991,51503, 49999,48480,46947,45399,43837,42261,40673,39073,37460,35836, 34202,32556,30901,29237,27563,25881,24192,22495,20791,19080, 17364,15643,13917,12186,10452,8715,6975,5233,3489,1745, 0,-1745,-3489,-5233,-6975,-8715,-10452,-12186,-13917,-15643, -17364,-19080,-20791,-22495,-24192,-25881,-27563,-29237,-30901,-32556, -34202,-35836,-37460,-39073,-40673,-42261,-43837,-45399,-46947,-48480, -50000,-51503,-52991,-54463,-55919,-57357,-58778,-60181,-61566,-62932, -64278,-65605,-66913,-68199,-69465,-70710,-71933,-73135,-74314,-75470, -76604,-77714,-78801,-79863,-80901,-81915,-82903,-83867,-84804,-85716, -86602,-87461,-88294,-89100,-89879,-90630,-91354,-92050,-92718,-93358, -93969,-94551,-95105,-95630,-96126,-96592,-97029,-97437,-97814,-98162, -98480,-98768,-99026,-99254,-99452,-99619,-99756,-99862,-99939,-99984, -100000,-99984,-99939,-99862,-99756,-99619,-99452,-99254,-99026,-98768, -98480,-98162,-97814,-97437,-97029,-96592,-96126,-95630,-95105,-94551, -93969,-93358,-92718,-92050,-91354,-90630,-89879,-89100,-88294,-87461, -86602,-85716,-84804,-83867,-82903,-81915,-80901,-79863,-78801,-77714, -76604,-75470,-74314,-73135,-71933,-70710,-69465,-68199,-66913,-65605, -64278,-62932,-61566,-60181,-58778,-57357,-55919,-54463,-52991,-51503, -50000,-48480,-46947,-45399,-43837,-42261,-40673,-39073,-37460,-35836, -34202,-32556,-30901,-29237,-27563,-25881,-24192,-22495,-20791,-19080, -17364,-15643,-13917,-12186,-10452,-8715,-6975,-5233,-3489,-1745}; // 100000.0*cos(n度) を長さ 360 の整数配列にしたもの private final int cos[]={ 100000,99984,99939,99862,99756,99619,99452,99254,99026,98768, 98480,98162,97814,97437,97029,96592,96126,95630,95105,94551, 93969,93358,92718,92050,91354,90630,89879,89100,88294,87461, 86602,85716,84804,83867,82903,81915,80901,79863,78801,77714, 76604,75470,74314,73135,71933,70710,69465,68199,66913,65605, 64278,62932,61566,60181,58778,57357,55919,54463,52991,51503, 50000,48480,46947,45399,43837,42261,40673,39073,37460,35836, 34202,32556,30901,29237,27563,25881,24192,22495,20791,19080, 17364,15643,13917,12186,10452,8715,6975,5233,3489,1745, 0,-1745,-3489,-5233,-6975,-8715,-10452,-12186,-13917,-15643, -17364,-19080,-20791,-22495,-24192,-25881,-27563,-29237,-30901,-32556, -34202,-35836,-37460,-39073,-40673,-42261,-43837,-45399,-46947,-48480, -49999,-51503,-52991,-54463,-55919,-57357,-58778,-60181,-61566,-62932, -64278,-65605,-66913,-68199,-69465,-70710,-71933,-73135,-74314,-75470, -76604,-77714,-78801,-79863,-80901,-81915,-82903,-83867,-84804,-85716, -86602,-87461,-88294,-89100,-89879,-90630,-91354,-92050,-92718,-93358, -93969,-94551,-95105,-95630,-96126,-96592,-97029,-97437,-97814,-98162, -98480,-98768,-99026,-99254,-99452,-99619,-99756,-99862,-99939,-99984, -100000,-99984,-99939,-99862,-99756,-99619,-99452,-99254,-99026,-98768, -98480,-98162,-97814,-97437,-97029,-96592,-96126,-95630,-95105,-94551, -93969,-93358,-92718,-92050,-91354,-90630,-89879,-89100,-88294,-87461, -86602,-85716,-84804,-83867,-82903,-81915,-80901,-79863,-78801,-77714, -76604,-75470,-74314,-73135,-71933,-70710,-69465,-68199,-66913,-65605, -64278,-62932,-61566,-60181,-58778,-57357,-55919,-54463,-52991,-51503, -50000,-48480,-46947,-45399,-43837,-42261,-40673,-39073,-37460,-35836, -34202,-32556,-30901,-29237,-27563,-25881,-24192,-22495,-20791,-19080, -17364,-15643,-13917,-12186,-10452,-8715,-6975,-5233,-3489,-1745, 0,1745,3489,5233,6975,8715,10452,12186,13917,15643, 17364,19080,20791,22495,24192,25881,27563,29237,30901,32556, 34202,35836,37460,39073,40673,42261,43837,45399,46947,48480, 50000,51503,52991,54463,55919,57357,58778,60181,61566,62932, 64278,65605,66913,68199,69465,70710,71933,73135,74314,75470, 76604,77714,78801,79863,80901,81915,82903,83867,84804,85716, 86602,87461,88294,89100,89879,90630,91354,92050,92718,93358, 93969,94551,95105,95630,96126,96592,97029,97437,97814,98162, 98480,98768,99026,99254,99452,99619,99756,99862,99939,99984}; } /* Local Variables: mode: java End: */