Code

All articles about programming are collected here. Otherwise, they are lost in backups.

Builder-Patterns in Android

When I started with android, I almost always asked Google how to do the smallest things. I want to highlight the examples I found for starting an activity.

Example for Method-Chaining

When you learn Java, you may stumble across some principle called "Chaining". To use chaining of methods, you have to return this. To show this in a short example, I will give you an example with rational numbers. In the class Rational, you can find the method add. The innocent approach is usally to use void as return type. Hence you will have this code:

jQuery animate() and its completion callback

Using jQuery to animate HTML objects is fun. These animations are easy to create and there are many good documents with explanations and examples.

Unfortunately I haven't noticed the information in the jQuery API about "this" in its callback function. The animate-Method replaces "this" with the used DOM-element. If you are trying to program object-oriented with JavaScript, you can easily run into problems.

Here is a little snippet from our game "Doggie Dodge" (working title) to illustrate the problem. The wrong code:

Draw cube with OpenGL in a for-loop

As long as you use glutSolidCube in OpenGL, drawing a cube isn't hard. With Java OpenGL (JOGL), it's not as obvious, since you need the special import com.sun.opengl.util.GLUT to use the glutXXX()-methods. Another approach is to write your own version with the OpenGL primitives. If you don't want to think about all those needed coordinates, you can also try to rotate one face of the cube six times to create the cube.

Following Scala-code uses this principle:

Analog Clock

The first code I'd like to show is this analog clock. It is embedded into a HTML5 <canvas> element and brought to life by JavaScript. During initialization, the size and color of the clock can be set up. Usually, the background is transparent and CSS can set the background-color.

With the right browser (HTML5) and support for iframes, you should see the analog clock below this text. If iframes are causing trouble, you can also try the direct link at /stuff/clock/clock.html.

Subscribe to RSS - Code