Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java have a Bitmap class? [duplicate]

Tags:

java

bitmap

I have been searching google for stuff like "Java Bitmap", "Create Java Bitmap", etc and cannot seem to find much information. From all of the code examples it looks like all of the bitmap libraries are third party or for android.

What I want to do is very simple. I want to create a small bitmap maybe 10x80, and be able to color each pixel at (x,y). I want to make a small I guess color bar that will show that position of items in a queue by color.

Are there any build in libraries to do this?

like image 583
KDecker Avatar asked Sep 07 '14 19:09

KDecker


2 Answers

Here is an example of creating and writing to pixels with BufferedImage:

BufferedImage image = new BufferedImage(10, 80, BufferedImage.TYPE_4BYTE_ABGR);
image.setRGB(5, 20, Color.BLUE.getRGB());

The third parameter on the image is the type, which may vary based on your use, but for basic solid colors the one shown is good. The setRGB uses an int representation of the color so you can't just use a Color constant directly.

You probably don't want to use VolatileImage because the benefits are dubious in current Java implementations. See this stackoverflow question for why it may not help.

Look at the Oracle image turorial for help with this. It explains both your options and how to interact with them.

like image 51
Eben Avatar answered Nov 03 '22 22:11

Eben


There's the java.awt.image.BufferedImage class. This has pixel-specific get/set methods. http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html

like image 27
khelwood Avatar answered Nov 03 '22 21:11

khelwood