Using Awt with Android

Porting AWT graphics code to Android

The android platform supports a small subset of awt. By small, I mean it supports awt fonts. Going from java swing (are you really just using awt as a standalone UI?) to Android is going to be a shock to the system. One defines Android's UI in XML resource files, and those resources are loaded into Activity classes which represents a logical unit of the application. Canvas' replace Graphics2D objects, and they have somewhat different functionality.The Android UI system seeks to avoid absolute positioning, which is common in java UI's. And there is so much more to consider for Android, like varying screen sizes and differences in resolution. Neither of which were much of a issue for Java SE. To answer your question: you have your work cut out for you and you will have to start much of your coding from scratch.

I'm not sure what 'drawing code' means, but in my case, I have a large amount of animation code that is displayed via awt shapes. This is reproducable on android as there are graphics.drawable.shapes objects, and one can display primitive shapes by doing something like canvas.drawCircle(x,y,z,h) (remind you of something?). But when I ported it, it was difficulty and felt like I was cortorting the android to do something it really didn't want to, and wasn't intended to do. It's hard to answer your question exactly given the vagueness of it.

How to add java.awt.image package in Android

The Java AWT classes contain native code, so unless someone ports that native code to Android, you are out of luck. And, they won't port it, because as it was pointed out above, Android has its own graphics libraries (android.graphics).

it's possible to use Java.awt.Image android application

For instance, to convert a grayscale image (byte array, imageSrc) to drawable:

        byte[] imageSrc= [...];
// That's where the RGBA array goes.
byte[] imageRGBA = new byte[imageSrc.length * 4];
int i;
for (i = 0; i < imageSrc.length; i++) {
imageRGBA[i * 4] = imageRGBA[i * 4 + 1] = imageRGBA[i * 4 + 2] = ((byte) ~imageSrc[i]);

// Invert the source bits
imageRGBA[i * 4 + 3] = -1;// 0xff, that's the alpha.
}

// Now put these nice RGBA pixels into a Bitmap object
Bitmap bm = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bm.copyPixelsFromBuffer(ByteBuffer.wrap(imageRGBA));

Code may differ depending of input format.

How can I import java.awt.image.BufferedImage in Android Studio

You can't.

The AWT package is not supported in Android, you need to change your implementation to use the Android classes.

See these similar questions:

Porting AWT graphics code to Android

How to add java AWT image package in Android

Using AWT with Android

java.awt.Point class in Android

Android does not support the AWT. Neither does it support Swing.

It has it's own graphics stuff.

The class you can use instead is android.graphics.Point, when the coordinates are integers, or android.graphics.PointF, when the coordinates are of type float.

Having looked at libGDX after your commment on your question, it doesn't seem to have a 2D point class of it's own, instead using float x, float y where it needs 2D points. As these are of type float, the PointF class is what you'll want to use.

java.awt.Desktop class

I solved it by myself now. Instead of trying to get the Desktop class from java.awt.Desktop, I just overwrote the on Authorization method:

AuthorizationCodeInstalledApp ab = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()){
protected void onAuthorization(AuthorizationCodeRequestUrl authorizationUrl) throws IOException {
String url = (authorizationUrl.build());
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
main_Activity.startActivity(browserIntent);
}
};

The reason I did this is because authorize() will call onAuthorization() which will call browse(), which checks if Desktop is supported or not.
So by rewriting the onAuthorization() method, I won't need that class anymore.
My rewritten class will just start a new Browserwindow with that authorization-URL in your android device.

I hope, I was able to help anyone, who encounteres this problem.



Related Topics



Leave a reply



Submit