A first example |
Let's look at a minimal example to get to know the framework and see how to run and test your "mini-apps".
Here is the source code for the first example (basic.kt):
// // Tiny mini-app for the CS109 Android framework // import org.otfried.cs109.Color import org.otfried.cs109.TextAlign import org.otfried.cs109.Context import org.otfried.cs109.MiniApp import org.otfried.cs109.Canvas class Main(val ctx: Context) : MiniApp { init { ctx.setTitle("Demo #1") } override fun onDraw(canvas: Canvas) { canvas.clear(Color(255, 255, 192)) canvas.setColor(Color.BLUE) canvas.setFont(48.0) canvas.drawText("CS109", canvas.width / 2.0, 200.0, TextAlign.CENTER) canvas.drawCircle(canvas.width / 2.0, 400.0, 60.0) } }
As you see, a mini-app must define a class whose name is Main, that implements the MiniApp framework, and that has a constructor taking an org.otfried.cs109.Context argument.
The class Main must define a method called onDraw(canvas). Since the method is required by the framework, we must use the override keyword, just like when you define toString for a class.
This method is responsible for drawing the appearance of the mini-app, using the methods of the provided canvas object. This canvas provides exactly the same methods as we met before in the drawing section.
We compile the mini-app as follows:
$ ktc-dex basic.kt
The result of the compilation are two files, basic.jar and basic.dex. These are packages that contain all the classes that are part of the mini-app.
We will first test the mini-app on a simple emulator that runs on your PC and that allows you to test mini-apps more easily:
$ kt-emulator basic.jar
A window like the following should appear:
Copy the file basic.dex to your phone, and start the app. As long as no mini-app is loaded, it will greet you with a screen like this:
If all goes well, your mini-app should now be running on your phone:
A first example |