Tuesday, August 28, 2012

Android: Activity, View, onDraw, Canvas

Drawing on canvas overriding onDraw() method
Assuming using RelativeLayout in xml and with id "layout1"


public class SecondActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        //setContentView(new MyView(this));
        RelativeLayout layout = (RelativeLayout)(this.findViewById(R.id.layout1));
        layout.addView(new MyView(this));
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_second, menu);
        return true;
    }
    class MyView extends View{
   public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

// if required to add this custom view using xml or the graphical layout in eclipse. have to add this constructor
         public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

protected void onDraw(Canvas canvas){
    System.out.println("draw canvas");
    Paint paint = new Paint();
    paint.setColor(0xFFFF0000);
    Rect rect = new Rect();
    rect.set(100, 100, 200, 150);
    canvas.drawRect(rect, paint);
   }
    }
}


C#: Console


To capture key press, print at specific position of console window

class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            bool running = true;
            int pos = 0;
            int lengthOfLine = 1;
            int diff = 0;
            while (running)
            {
                //Console.Clear();
                if (Console.CursorTop - pos > diff)
                {
                    pos = Console.CursorTop;
                }
                else
                {
                    Console.CursorTop = pos;
                }
                Console.WriteLine("i = " + (i++));
                diff = Console.CursorTop - pos;
             
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey(true);
                    if (key.Key == ConsoleKey.I)
                    {
                        Console.WriteLine("I pressed");
                        //running = false;
                    }
                    else if (key.Key == ConsoleKey.X)
                    {
                        Console.WriteLine("Exit");
                        running = false;
                    }
                }
                Thread.Sleep(40);
            }

        }
    }

Tuesday, August 21, 2012

android: loading and displaying images on canvas


copy and paste image into res\drawable folder in project folder
R.java in "gen" should include this image with an id. Note: filename to be all lower-case

// loading, assuming written in a View class
Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.image_id); 
// displaying
canvas.drawBitmap(bmp, 50, 30, null);

Android: 2d drawing surface

To create a canvas for custom drawing, eg,: for 2d games without using opengl
Create a new class extending SurfaceView, add constructor with 2 arguments (so that it can be used in graphical editor in eclipse too), implement interface SurfaceHolder.Callback

import android.content.Context;
import android.util.AttributeSet;
import android.view.*;

public class MySurface extends SurfaceView implements SurfaceHolder.Callback{
SurfaceHolder holder;

public MySurface(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
holder = this.getHolder();
holder.addCallback(this);

}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
System.out.println("surface changed");
}

public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
System.out.println("surface created");
}

public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
System.out.println("surface destroyed");
}

}


This new custom UI will be available for the xml layout file in Eclipse

can create a draw() method to contain all drawing code in there. then call draw() when surfaceCreated(...)

void draw(){
canvas = holder.lockCanvas();
Paint paint = new Paint();
paint.setColor(0xFFFF0000); // ARGB, red
canvas.drawRect(new Rect(10, 20, 100, 50), paint);
holder.unlockCanvasAndPost(canvas);
}


Threading can be used if animation is needed, for example for games.

Sample code snippet:

public class MySurface extends SurfaceView implements SurfaceHolder.Callback, Runnable{
SurfaceHolder holder;
Canvas canvas;
MySprite s = new MySprite();
boolean isRunning = true;
long time = 0;

static final int DELAY = 10;

public MySurface(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
holder = this.getHolder();
holder.addCallback(this);

}

synchronized void draw(){
canvas = holder.lockCanvas();
canvas.drawColor(0xFFDDDDDD); // ARGB , fill whole canvas

// call draw in sprite
s.draw(canvas);

holder.unlockCanvasAndPost(canvas);
}

synchronized void update(){
s.update();
System.out.println((System.currentTimeMillis() - time) + " ms");
time = System.currentTimeMillis();
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
System.out.println("surface changed");
}

public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
System.out.println("surface created");

isRunning = true;
Thread thread = new Thread(this);
thread.start();

time = System.currentTimeMillis();
}

public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
System.out.println("surface destroyed");
isRunning = false;
}

public void run() {
// TODO Auto-generated method stub
try{
while(isRunning){
update();
draw();
Thread.sleep(DELAY);
}
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
}

MySprite is a custom class which contains x, y, width, height, its own draw(), update(), init(), cleanup() methods.


Monday, August 20, 2012

wxpython with new WebView

using version 2.9.4

import wx
import wx.html2

app = wx.App(False)
frame = wx.Frame(None, -1, 'A Simple Frame')
browser = wx.html2.WebView.New(frame)
browser.LoadURL("http://www.google.com")
frame.Show()
app.MainLoop()




tried loading local html file with some javascript.
worked so far. tested with jquery. ok too.

import os
browser.LoadURL(os.path.realpath("test.html"))