|
ファイル名「Box.java」
003 | import android.app.Activity; |
004 | import android.app.AlertDialog; |
005 | import android.content.DialogInterface; |
006 | import android.graphics.Color; |
007 | import android.graphics.PixelFormat; |
008 | import android.os.Bundle; |
009 | import android.view.Display; |
010 | import android.view.Menu; |
011 | import android.view.MenuItem; |
012 | import android.view.Window; |
013 | import android.view.WindowManager; |
014 | public class Box extends Activity { |
016 | private static final int |
023 | public float disp_w,disp_h; |
024 | private int col = Color.WHITE; |
025 | private boolean reset = false ; |
027 | public void onCreate(Bundle savedInstanceState) { |
028 | super.onCreate(savedInstanceState); |
029 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
030 | getWindow().setFormat(PixelFormat.TRANSLUCENT); |
032 | Window window = getWindow(); |
033 | WindowManager manager = window.getWindowManager(); |
034 | Display disp = manager.getDefaultDisplay(); |
035 | disp_w = disp.getWidth(); |
036 | disp_h = disp.getHeight(); |
038 | setContentView( new BoxView( this )); |
041 | public boolean onCreateOptionsMenu(Menu menu){ |
042 | super.onCreateOptionsMenu(menu); |
046 | MenuItem item0 = menu.add(0,MENU_ITEM0,0,R. string .menu_item0); |
048 | item0.setIcon(R.drawable.ic_reset); |
050 | MenuItem item1 = menu.add(0,MENU_ITEM1,0,R. string .menu_item1); |
051 | item1.setIcon(R.drawable.ic_white); |
053 | MenuItem item2 = menu.add(0,MENU_ITEM2,0,R. string .menu_item2); |
054 | item2.setIcon(R.drawable.ic_brack); |
056 | MenuItem item3 = menu.add(0,MENU_ITEM3,0,R. string .menu_item3); |
057 | item3.setIcon(R.drawable.ic_yellow); |
059 | MenuItem item4 = menu.add(0,MENU_ITEM4,0,R. string .menu_item4); |
060 | item4.setIcon(R.drawable.ic_blue); |
062 | MenuItem item5 = menu.add(0,MENU_ITEM5,0,R. string .menu_item5); |
063 | item5.setIcon(R.drawable.ic_gray); |
068 | public boolean onOptionsItemSelected(MenuItem item){ |
069 | switch (item.getItemId()){ |
072 | showDialog( this , "" , "BOXをすべて消しました" ); |
076 | showDialog( this , "" , "背景を白にしました" ); |
080 | showDialog( this , "" , "背景を黒にしました" ); |
084 | showDialog( this , "" , "背景を黄色にしました" ); |
088 | showDialog( this , "" , "背景を青にしました" ); |
092 | showDialog( this , "" , "背景をグレーにしました" ); |
098 | private void showDialog(final Activity activity, String title, String text) { |
099 | AlertDialog.Builder ad = new AlertDialog.Builder(activity); |
102 | ad.setPositiveButton( "OK" , new DialogInterface.OnClickListener() { |
104 | public void onClick(DialogInterface dialog, int which) { |
105 | activity.setResult(Activity.RESULT_OK); |
111 | public int getcolor(){ return col;} |
112 | public boolean getreset(){ return reset;} |
113 | public void setreset(){reset = false ;} |
|
|
|
ファイル名「BoxView」
03 | import android.R.color; |
04 | import android.content.Context; |
05 | import android.graphics.Canvas; |
06 | import android.graphics.Color; |
07 | import android.graphics.Paint; |
08 | import android.view.MotionEvent; |
09 | import android.view.SurfaceHolder; |
10 | import android.view.SurfaceView; |
12 | public class BoxView extends SurfaceView implements SurfaceHolder.Callback,Runnable{ |
13 | private SurfaceHolder holder; |
16 | private float disp_w,disp_h; |
19 | private BoxGamen gamen; |
21 | private static final int |
24 | gamen_col = Color.BLACK; |
27 | public BoxView(Context context) { |
30 | holder.addCallback( this ); |
31 | holder.setFixedSize(getWidth(), getHeight()); |
37 | public void init(Context context){ |
41 | gamen = new BoxGamen(gamen_x,gamen_y,( int )disp_w-5,( int )disp_h-35,gamen_col); |
50 | if (box.getreset() == true ) { |
52 | start = box.getreset(); |
55 | c = holder.lockCanvas(); |
56 | c.drawColor(box.getcolor()); |
58 | gamen.draw(c, p, start); |
60 | p.setColor(color.black); |
61 | c.drawText( "reset:" +box.getreset(), 10, 50, p); |
63 | holder.unlockCanvasAndPost(c); |
67 | } catch (Exception e){} |
71 | public boolean onTouchEvent(MotionEvent event ){ |
72 | int action = event .getAction(); |
73 | int x = ( int ) event .getX(); |
74 | int y = ( int ) event .getY(); |
76 | case MotionEvent.ACTION_DOWN: |
78 | case MotionEvent.ACTION_UP: |
79 | if (x < disp_w-95 && y < disp_h-95){ |
88 | public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {} |
90 | public void surfaceCreated(SurfaceHolder arg0) {thread = new Thread( this );thread.start();} |
92 | public void surfaceDestroyed(SurfaceHolder arg0) {thread = null ;} |
|
|
|
ファイル名「BoxGamen.java」
03 | import java.util.ArrayList; |
05 | import java.util.Random; |
07 | import android.R.color; |
08 | import android.graphics.Canvas; |
09 | import android.graphics.Color; |
10 | import android.graphics.Paint; |
11 | import android.graphics.Rect; |
13 | public class BoxGamen { |
14 | private int gamen_x,gamen_y,gamen_w,gamen_h,gamen_col; |
16 | private static final int |
19 | private ArrayList<boxob> box = new ArrayList<boxob>(); |
21 | BoxGamen( int x, int y, int w, int h, int col){ |
28 | public void init( int x, int y){ |
29 | Random r = new Random( new Date().getTime()); |
30 | int wh = r.nextInt(box_wh)+5; |
31 | int d = r.nextInt(box_d); |
32 | int col = Color.rgb(r.nextInt(240)+10, r.nextInt(240)+10, r.nextInt(240)+10); |
33 | int vxy = r.nextInt(20)+5; |
34 | box.add( new BoxOb(x,y,wh,d,vxy,col)); |
36 | public void draw(Canvas c, Paint p, boolean start){ |
37 | p.setStyle(Paint.Style.STROKE); |
38 | p.setColor(Color.BLACK); |
39 | c.drawRect( new Rect(gamen_x, gamen_y, gamen_w, gamen_h), p); |
40 | if (start == true ) for ( int i=0;i<box.size();i++) { |
42 | box. get (i).move(gamen_x, gamen_y, gamen_w, gamen_h); |
|
|
|
ファイル名「BoxOb.java」
03 | import android.graphics.Canvas; |
04 | import android.graphics.Paint; |
05 | import android.graphics.Rect; |
08 | private static final double PIE = 3.1415926; |
09 | private double obj_x,obj_y; |
12 | private int obj_xspeed; |
13 | private int obj_yspeed; |
17 | BoxOb( double x, double y, int wh, int d, int vxy, int col){ |
24 | obj_d = (obj_d+360)%360; |
25 | obj_xspeed = ( int ) (Math.cos(toRadian(obj_d))*obj_vxy); |
26 | obj_yspeed = ( int )-(Math.sin(toRadian(obj_d))*obj_vxy); |
29 | public void draw(Canvas c, Paint p){ |
30 | p.setStyle(Paint.Style.FILL); |
32 | c.drawRect( new Rect( ( int )obj_x, ( int )obj_y, ( int )obj_x + obj_wh, ( int )obj_y + obj_wh), p); |
34 | public void move( int x1, int y1, int x2, int y2){ |
37 | if (obj_x < x1 || obj_x > x2-obj_wh ) obj_xspeed *= -1; |
38 | if (obj_y < y1 || obj_y > y2-obj_wh ) obj_yspeed *= -1; |
40 | public float toRadian( float deg){ return ( float ) (deg * PIE / 180);} |
|
|
|

|
|
|
|
|
|
|
|