2D tutorial

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Re: 2D tutorial

Postby Mactm » Thu Jun 10, 2010 5:47 pm

I am getting an error about 'Source Not Found' for Instrumental.class when '(new Thread(new AnimationLoop())).start();' is executed.


Any ideas?
Mactm
Freshman
Freshman
 
Posts: 6
Joined: Mon Jun 07, 2010 4:03 pm

Top

Re: 2D tutorial

Postby Dhr » Mon Sep 06, 2010 7:10 am

Hello 2All,

Thanks for this useful tutorial.

I have a question regarding to this.
The question is like this: "If I want to move the box in TOP and DOWN direction, what changes I have to do in this code."

Please suggest me.

~Dhr
User avatar
Dhr
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Aug 19, 2010 11:59 am
Location: Ahd, Gujarat, India

Re: 2D tutorial

Postby Dhr » Mon Sep 06, 2010 7:10 am

Hello 2All,

Thanks for this useful tutorial.

I have a question regarding to this.
The question is like this: "If I want to move the box in TOP and DOWN direction, what changes I have to do in this code."

Please suggest me.

~Dhr
User avatar
Dhr
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Aug 19, 2010 11:59 am
Location: Ahd, Gujarat, India

Re:

Postby yhbae » Fri Oct 22, 2010 6:03 pm

Danuubz wrote:I've got a litte code sample for 2D animating on a simple UI extending View (wich is a little bit different from the SurfaceView approach, more like J2SE).

Red box is moving from one edge to the other. You can stop (and restart) him by pressing DPAD_CENTER.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // author: Robert Kanzamar
  2. package de.testing;
  3.  
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.*;
  7. import android.graphics.*;
  8. import android.content.*;
  9.  
  10. public class Testing extends Activity
  11. {      
  12.         public static final int DIRECTION_RIGHT = 0, DIRECTION_LEFT = 1;
  13.        
  14.         private Panel main;
  15.         private Bitmap scratch;
  16.         private Canvas offscreen;
  17.        
  18.         public boolean start = true;
  19.         private volatile boolean running = true;
  20.         private int direction = DIRECTION_RIGHT;
  21.        
  22.         private int box = 10;
  23.        
  24.     @Override
  25.     public void onCreate(Bundle savedInstanceState)
  26.     {
  27.         super.onCreate(savedInstanceState);  
  28.        
  29.         setOffscreenBitmap();
  30.        
  31.         main = new Panel(this);        
  32.         setContentView(main,new ViewGroup.LayoutParams(320,480));
  33.        
  34.         (new Thread(new AnimationLoop())).start();
  35.     }
  36.    
  37.     private void setOffscreenBitmap()
  38.     {
  39.          scratch = Bitmap.createBitmap(30,30,Bitmap.Config.ARGB_8888);
  40.          offscreen = new Canvas();
  41.          offscreen.setBitmap(scratch);
  42.          offscreen.drawColor(Color.RED);
  43.     }
  44.    
  45.     private synchronized void updatePhysics()
  46.     {
  47.         if(box < 10)
  48.         {
  49.                 direction = DIRECTION_RIGHT;
  50.         }
  51.         else if(box > 250)
  52.         {
  53.                 direction = DIRECTION_LEFT;
  54.         }
  55.        
  56.         if(direction == DIRECTION_RIGHT)
  57.         {
  58.                 box += 10;
  59.         }
  60.         else
  61.         {
  62.                 box -= 10;
  63.         }
  64.     }
  65.    
  66.     private synchronized void doDraw(Canvas canvas, Paint paint)
  67.     {
  68.         if(start)
  69.                 {
  70.                         canvas.drawColor(Color.BLACK);
  71.                         canvas.drawBitmap(scratch,10,10,paint);
  72.                         start = false;
  73.                 }
  74.                 else
  75.                 {              
  76.                 canvas.save();
  77.                 canvas.clipRect(box,8,box+32,40);              
  78.                 canvas.drawColor(Color.RED);
  79.                 // canvas.drawBitmap(scratch,box,10,paint);
  80.                 canvas.restore();              
  81.                 }
  82.     }
  83.    
  84.     @Override
  85.     public boolean onKeyDown(int keyCode, KeyEvent event)
  86.     {
  87.         if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
  88.         {
  89.                 if(running)
  90.                 {
  91.                         running = false;
  92.                 }
  93.                 else
  94.                 {
  95.                         running = true;
  96.                 }
  97.         }
  98.         else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
  99.         {
  100.                 finish();
  101.         }
  102.        
  103.         return true;
  104.     }
  105.    
  106.     class Panel extends View
  107.     {
  108.         Paint paint;
  109.        
  110.         public Panel(Context context)
  111.         {
  112.                 super(context);
  113.                 paint = new Paint();
  114.         }
  115.        
  116.         @Override
  117.         protected void onDraw(Canvas canvas)
  118.         {
  119.                 doDraw(canvas,paint);
  120.         }
  121.     }
  122.    
  123.     class AnimationLoop implements Runnable
  124.     {
  125.         public void run()
  126.         {
  127.                 while(true)
  128.                 {
  129.                         while(running)
  130.                         {                                      
  131.                                 try
  132.                                 {
  133.                                         Thread.sleep(30);
  134.                                 }
  135.                                 catch(InterruptedException ex) {}
  136.                                
  137.                                 updatePhysics();
  138.                                 main.postInvalidate();
  139.                         }
  140.                 }
  141.         }
  142.     }    
  143. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


I just tried creating a new project with this source code. Eclipse can launch this app without any errors but I get nothing in the emulator! The home screen remains the same. What could I have possibly done wrong?

Looks like everything went smoothly:

[2010-10-22 12:59:04 - test2D] Performing sync
[2010-10-22 12:59:04 - test2D] Automatic Target Mode: Preferred AVD 'AVD1.6' is available on emulator 'emulator-5554'
[2010-10-22 12:59:05 - test2D] Application already deployed. No need to reinstall.
[2010-10-22 12:59:05 - test2D] \test2D\bin\test2D.apk installed on device
[2010-10-22 12:59:05 - test2D] Done!

Thanks.
yhbae
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Oct 19, 2010 8:13 pm

Re: 2D tutorial

Postby amanni82 » Sat Apr 02, 2011 5:08 pm

HI everyone, this is my first time posting here. I'm working on a weather guide app and I don't know what type of graphics would be best to use 2D or 3D? I'm also looking for any tutorials on using graphics in Android. Thanks
amanni82
Freshman
Freshman
 
Posts: 3
Joined: Sat Apr 02, 2011 12:04 pm

Re: 2D tutorial

Postby zeroARM000 » Tue Apr 05, 2011 10:08 am

Hi guys!, i'm making a word search game right now... I've done with the algorithm and so on, but I'm facing new problem...

I want to paint the characters that are being choosen by user to match a word. actually, I'm using gridview to placed the characters...

Here's the code

Code: Select all
public class GridAlpha extends Activity{
int BOARD=8;
String[]rep = new String[50];
String[]def = new String[64];
String LETTERS = "abcdefghijklmnopqrstuvwxyz";
String[][]square = new String[8][8];

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    KamusDbAdapter x = new KamusDbAdapter(getApplicationContext());
    x.open();
    rep = x.playWord();
    x.close();


    Random round = new Random();int[]rec=new int[10];int m=0;
    for(int i=0; i<50; i++){
        //algorithm section, used to place the words inside the grid per char
    }
    //additional logic, used to store the char in string that will be use in gridview
    //Log.v("CC",String.valueOf(cc));

    ArrayAdapter<String> ad = new ArrayAdapter<String>(this,R.layout.form,def);
    GridView grid=(GridView)findViewById(R.id.grid);
    grid.setAdapter(ad);

    grid.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            String test = (ad.getItem(arg2)).toString();
            Log.v("CAPTURE",test);
        }
    });

    ListView wordlist = (ListView)findViewById(R.id.wordlist);
    ArrayAdapter<String> showWord = new ArrayAdapter<String>(this,R.layout.form2,store);
    wordlist.setAdapter(showWord);

}

}


I think that's all that I can explain right now... if you are confuse with my question, just go to the screenshot link below

http://stat001.ameba.jp/user_images/201 ... 405137.png

*I just want to paint the selected words inside the gridview, that's all...

Best regards,
zeroARM000
Freshman
Freshman
 
Posts: 5
Joined: Tue Apr 05, 2011 10:01 am

Top

Re: 2D tutorial

Postby jumico » Tue Apr 05, 2011 9:19 pm

Thanks for the template. It's helps make it easier to understand what the basics are compared to lunarlander.

In layout main.xml FILL_PARENT has been renamed MATCH_PARENT in android api level 8 and higher. I'm not sure if this matters but I thought I'd point it out.
jumico
Freshman
Freshman
 
Posts: 2
Joined: Tue Apr 05, 2011 9:15 pm

Re: 2D tutorial

Postby menseken » Thu May 12, 2011 10:03 am

Hi!
@ MrSnowflake:
I used your stripped LunarLander template to transform a javascript game I wrote for my SONY PRS500 ebook reader.
All works very fine - much, much nicer than on my old FOUR grayscale display!
Problem is, if I pause the game (home button, starting background app) it crashes upon resume/restart...
Do you think, you can point to the code that's missing now - even update your template?
Thanks, Clemens

EDIT:
I just found out that the original sdk-sample crashes, too ...
menseken
Once Poster
Once Poster
 
Posts: 1
Joined: Thu May 12, 2011 9:52 am

Re: 2D tutorial

Postby Obsquacious » Sun May 15, 2011 8:57 pm

Nevermind.. Thanks for the template!
Obsquacious
Once Poster
Once Poster
 
Posts: 1
Joined: Sun May 15, 2011 8:55 pm

Re: 2D tutorial

Postby dreamdelerium » Tue May 17, 2011 1:15 am

hello,
I was hoping someone here can help me out. I'm trying to use this template but I get an error when the program closes. It seems to be a problem with how the thread is paused and I cant figure out how to fix it. as i said the error seems to be here:
other then adding the error catching, i didnt change anything.
Code: Select all
        /**
         * Pauses the physics update & animation.
         */
        public void pause() {           
           try{             
           synchronized (mSurfaceHolder) {
                   setState(STATE_PAUSE);
            }
           }catch(Exception e){
              Log.d("error pausing" , "error: "  +e.toString());
           }
        }


Code: Select all
public void pause() {           
           try{                  
            synchronized (mSurfaceHolder) {
                   setState(STATE_PAUSE);
            }
           }catch(Exception e){
              Log.d("error pausing" , "error: "  +e.toString());
           }
        }


this error occurs with the app is closed down but clicking the home button. the stack trace is this:

05-17 00:05:08.161: INFO/ActivityManager(52): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.android.launcher/.Launcher }
05-17 00:05:08.191: DEBUG/AndroidRuntime(326): Shutting down VM
05-17 00:05:08.191: WARN/dalvikvm(326): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-17 00:05:08.191: ERROR/AndroidRuntime(326): Uncaught handler: thread main exiting due to uncaught exception
05-17 00:05:08.281: ERROR/AndroidRuntime(326): java.lang.RuntimeException: Unable to pause activity {eu.MrSnowflake.android.gametemplate/eu.MrSnowflake.android.gametemplate.GameTemplate}: java.lang.NullPointerException
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3162)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3119)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3102)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at android.app.ActivityThread.access$2400(ActivityThread.java:119)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1870)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at android.os.Looper.loop(Looper.java:123)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at java.lang.reflect.Method.invoke(Method.java:521)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at dalvik.system.NativeStart.main(Native Method)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): Caused by: java.lang.NullPointerException
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at eu.MrSnowflake.android.gametemplate.GameTemplate.onPause(GameTemplate.java:112)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at android.app.Activity.performPause(Activity.java:3782)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1190)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3149)
05-17 00:05:08.281: ERROR/AndroidRuntime(326): ... 12 more
05-17 00:05:08.321: INFO/Process(52): Sending signal. PID: 326 SIG: 3
05-17 00:05:08.321: INFO/dalvikvm(326): threadid=7: reacting to signal 3
05-17 00:05:08.321: ERROR/dalvikvm(326): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
05-17 00:05:08.851: WARN/ActivityManager(52): Activity pause timeout for HistoryRecord{43ba7338 eu.MrSnowflake.android.gametemplate/.GameTemplate}
05-17 00:05:08.871: INFO/ARMAssembler(52): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x34ee40:0x34eefc] in 586958 ns
05-17 00:05:14.721: DEBUG/dalvikvm(120): GC freed 495 objects / 19896 bytes in 169ms
05-17 00:05:17.241: ERROR/gralloc(52): [unregister] handle 0x2d2a48 still locked (state=40000001)
05-17 00:05:17.351: INFO/Process(326): Sending signal. PID: 326 SIG: 9
05-17 00:05:17.382: INFO/WindowManager(52): WIN DEATH: Window{43d52c08 eu.MrSnowflake.android.gametemplate/eu.MrSnowflake.android.gametemplate.GameTemplate paused=false}
05-17 00:05:17.382: INFO/WindowManager(52): WIN DEATH: Window{43cc42c8 SurfaceView paused=false}
05-17 00:05:17.391: INFO/ActivityManager(52): Process eu.MrSnowflake.android.gametemplate (pid 326) has died.
05-17 00:08:17.561: DEBUG/dalvikvm(52): threadid=15: bogus mon 1+0>0; adjusting
dreamdelerium
Developer
Developer
 
Posts: 30
Joined: Fri Jan 01, 2010 11:19 pm

Top
Previous

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests