Tagjava

Java Enums in C#?

I really love C#! Almost all features like delegates, extensions methods, constraints, lambdas, refs, “magic methods”, etc. are really super convinient to use. It’s totally my favorite language so far and trust me I’ve been around with C/C++, Php, Javascript, Java. I pretty much code all the time in C# nowedays.

Probably one feature that Java did way better than C# is the way to extend enums. As stated on this website especially made for java to c# converting people.

Enums are used to create and group together a list of user defined named constants. Although on the surface the enumerated types in C# and Java seem quite similar there are some significant differences in the implementation of enumerated types in both languages. In Java, enumerated types are a full fledged class which means they are typesafe and can be extended by adding methods, fields or even implementing interfaces. Whereas in C#, an enumerated type is simply syntactic sugar around an integral type (typically an int) meaning they cannot be extended and are not typesafe.

You can use enums in Java as collection of concrecte behaviour implementations:

Or have a concrecte amount of certain Objects of a kind like a represenation of different resolutions.

Sure you can do it with normal classes, readonly attributes in c# too. But i was wondering how much of the functionality you could “port” towards c# with the help of generics, extension methods and injected attributes. So this is a small test i came up with the help of some respective stackoverflow articles.

Conclusion

Well it’s far from elegant/readable or even practically usable. However it’s possible to a certain extend. Feel free to correct me.

Android: OpenGL 1.0 and 2.0 infos

So here I was again, trying to figure out a way to get opengl infos like max texture size, max uniform vectors, max vertex attributes, etc. for several android devices. Technically you could just use a small getter:

The problem was that on many devices the the returned number would always be 0. That’s because there hasn’t been created an opengl context yet. As suggested on Stackoverflow you could create a GLSurfaceView, add a renderer which could read the required infos by adding the glsurfaceview to the actual main active view and simply remove itself when done. In order to get opengles 1.0 and opengles 2.0 infos you would need to create two surfacesviews with different opengl versions. The following example looks a bit complicated, because I use fragments which i need to update on the main thread after I got all the infos I wanted. A full example is on github.

How to get the MAC address on Android

(A test project can be found on github)

Since you can easily get confused with the many permissions provided by google, let me show you two ways to get the mac address on android. The first approach would be to use the WifiManager.getMacAdress().

However there are Disadvantages:

  • Device should have Wi-Fi (where not all devices have Wi-Fi)
  • If Wi-Fi present in device should be turned on otherwise does not report the MAC address
  • And the suspicious permission ACCESS_WIFI_STATE

Another approach is the use the NetworkInterface.getHardwareAddress()

Disadvantage: uses permissions too, although they are required for your app to access the internet anyway and more likely commonly known to the end-user.

Conclusion:

Carring about permissions can be a pain in the butt there are even other ways if you just want identifying app installations, too.

Android libgdx ShaderAssetLoader & StillModelLoader

I totally love how libgdx brings a lot of interfaces for your application to connect to the graphics library. For instance the asset manager is really thought through well in particular. It has some really useful methods like Assets.manager.getProgress(). Especially since you can easily write your own asset loader and be able to utilize the loading progress in your app.

1) Simply add your loader to the asset manager at some point.

2) load your assets at required places, e.g. loading screen
(Note: SHADER_PHONG = “data/Graphics/Shader/Phong”; and MODEL_TEAPOT = “data/Graphics/Shader/teapot.obj”)

3) get your shader or model wherever you need them

4) free the resource by unloading when you’re done with them (e.g. disposing your app; changing screens)

That’s the shader asset loader for loading custom shaders in libgdx. (Note: adapt your extensions)

And here is the StillModel loader for loading 3D meshes:

Conclusion: assets are handled well in libgdx :D

Android: Saving Screenshots in OpenGLES

1) Get your helper methods:

2) get a queue ready

3) add new filepath the the queue via e.g. events:

4) IMPORTANT! save the screen at the end of the opengl render process. (Note: If try to capture somewhere in the middle you might end up with a black screen or any other funny effects.)

How to swap two variables without a temp variable

In C

In Java

In Assembler

Fun fact: fastest is still with temp variable. (:

Extra python:

How to iterate over a java map

Since I’ve been asked this a lot lately …

Extracted from Reference link (original Sergiy Kovalchuk): How to Iterate Over a Map in Java

There are several ways of iterating over a Map in Java. Lets go over the most common methods and review their advantages and disadvantages. Since all maps in Java implement Map interface, following techniques will work for any map implementation (HashMap, TreeMap, LinkedHashMap, Hashtable, etc.)

Method #1: Iterating over entries using For-Each loop.

This is the most common method and is preferable in most cases. Should be used if you need both map keys and values in the loop.

Note that For-Each loop was introduced in Java 5 so this method is working only in newer versions of the language. Also For-Each loop will throw NullPointerException if you try to iterate over a map that is null, so before iterating you should always check for null references.

Method #2: Iterating over keys or values using For-Each loop.

If you need only keys or values from the map, you can iterate over keySet or values instead of entrySet.

This method gives slight performance advantage over entrySet iteration (about 10% faster) and is more clean.

Method #3: Iterating using Iterator.

Using Generics:

Without Generics:

You can also use same technique to iterate over keySet or values.

This method might look redundant but it has its own advantages. First of all it is the only way to iterate over a map in older versions of Java. The other important feature is that it is the only method that allows you to remove entries from the map during iteration by calling iterator.remove(). If you try to do this during For-Each iteration you will get “unpredictable results” according to javadoc.

From performance point of view this method is equal to For-Each iteration.

Method #4: Iterating over keys and searching for values (inefficient).

This might look like a cleaner alternative for method #1 but in practice it is pretty slow and inefficient as getting values by a key might be time consuming (this method in different Map implementations is 20%-200% slower than method #1). If you have FindBugs installed, it will detect this and warn you about inefficient iteration. This method should be avoided.
Conclusion

If you need only keys or values from the map use method #2. If you are stuck with older version of Java (less than 5) or planning to remove entries during iteration you have to use method #3. Otherwise use method #1.

How to boost android canvas performance with a buffered Image

When it comes to drawing to canvas on android one very big performance boost can be gained by using a buffered bitmap image. The idea is to draw lines to a bitmap. And then draw the bitmap to the screen.
This approach avoids unnecessarily redrawing elements that has been drawn already multiple times.

1) Create a bitmap in your view.

2) Remember all touch events.

3) Process all touch events. (Draw lines or something)

4) Draw to buffered bitmap image.

lineManager.draw():

5) And finally draw the buffered bitmap image.

Bonus: invalidate canvas only around finger area and only every 90 milliseconds.

See also

Performance in 2D Canvas App / Game on Android and Traceview War Story

Doodle Drawer – Brushes

At my Doodle Drawer app, I’ve played with a couple of “brushes”. The idea is to treat the connection lines differently.

shadedshaded
sketchedsketched
furfur

Basically the difference is that the line connections are drawn closer or further away from each line based on the formula of Harmony.

The ‘shaded brush’ looks like this:

The sketched brush looks like this:

And the ‘fur brush’ looks like this:

Java Tutorium: Aufgabe 11

Aufgabe 11

Download (PDF?RAW=TRUE, 28KB)

Continue reading