Embed a small image in code

A

aaronfude

Hi,

I want to display a 16x16 image in my application/applet and I don't
want to deal with jars/urls/resources/etc. Is there a way to embed an
image in code in some ascii format and then display it?

Thanks!

Aaron Fude
 
A

Andrew Thompson

On Feb 24, 5:07 am, (e-mail address removed) wrote:
....
I want to display a 16x16 image in my application/applet and I don't
want to deal with jars/urls/resources/etc. Is there a way to embed an
image in code in some ascii format and then display it?

Wow! You apparently missed last year's
'sensible' discussion on this.
<http://groups.google.com/group/comp.lang.java.programmer/browse_frm/
thread/2e03cd2160451bb0/beec9e133ed935b4?#beec9e133ed935b4>

What more could be added?

Andrew T.
 
O

Oliver Wong

Hi,

I want to display a 16x16 image in my application/applet and I don't
want to deal with jars/urls/resources/etc. Is there a way to embed an
image in code in some ascii format and then display it?

How about storing each pixel colour in an 16x16 array of int (or a
16x16x3 array of byte)?

- Oliver
 
A

aaronfude

In C++ I could do this:

#define test_width 16
#define test_height 7
static char test_bits[] = {
0x13, 0x00, 0x15, 0x00, 0x93, 0xcd, 0x55, 0xa5, 0x93, 0xc5, 0x00,
0x80,
0x00, 0x60, };

Is there an equivalent in Java?

Thanks.
 
C

Christian

In C++ I could do this:

#define test_width 16
#define test_height 7
static char test_bits[] = {
0x13, 0x00, 0x15, 0x00, 0x93, 0xcd, 0x55, 0xa5, 0x93, 0xc5, 0x00,
0x80,
0x00, 0x60, };

Is there an equivalent in Java?

Thanks.

private static final byte[] test_bits = new byte[]{ 0x13, 0x00, 0x15,
0x00, 0x93, 0xcd, 0x55, 0xa5, 0x93, 0xc5, 0x00,0x80,0x00, 0x60 };

? thats what you want?
 
O

Oliver Wong

In C++ I could do this:

#define test_width 16
#define test_height 7
static char test_bits[] = {
0x13, 0x00, 0x15, 0x00, 0x93, 0xcd, 0x55, 0xa5, 0x93, 0xc5, 0x00,
0x80,
0x00, 0x60, };

Is there an equivalent in Java?

public final class MyEmbeddedImage {
final public static int WIDTH = 16;
final public static int HEIGHT = 7;
final public static byte[] = {0x13, 0x00, 0x15, 0x00, 0x93, 0xcd, 0x55,
0xa5, 0x93, 0xc5, 0x00, 0x80, 0x00, 0x60, };
}

Though it's not clear to me the relationship between your chars/bytes
and your height and width. The number of elements in the array (14)
certainly does not seem to equate with 16x7.

- Oliver
 
A

aaronfude

In C++ I could do this:
#define test_width 16
#define test_height 7
static char test_bits[] = {
0x13, 0x00, 0x15, 0x00, 0x93, 0xcd, 0x55, 0xa5, 0x93, 0xc5, 0x00,
0x80,
0x00, 0x60, };
Is there an equivalent in Java?

public final class MyEmbeddedImage {
final public static int WIDTH = 16;
final public static int HEIGHT = 7;
final public static byte[] = {0x13, 0x00, 0x15, 0x00, 0x93, 0xcd, 0x55,
0xa5, 0x93, 0xc5, 0x00, 0x80, 0x00, 0x60, };

}

Then what would be the command for displaying this image in a JPanel
(assuming I fix the dimensions if they are wrong)?
 
O

Oliver Wong

In C++ I could do this:
#define test_width 16
#define test_height 7
static char test_bits[] = {
0x13, 0x00, 0x15, 0x00, 0x93, 0xcd, 0x55, 0xa5, 0x93, 0xc5, 0x00,
0x80,
0x00, 0x60, };
Is there an equivalent in Java?

public final class MyEmbeddedImage {
final public static int WIDTH = 16;
final public static int HEIGHT = 7;
final public static byte[] = {0x13, 0x00, 0x15, 0x00, 0x93, 0xcd,
0x55,

Note I forgot to put the name of the variable here, so this line
should read:

final public static byte[] test_bits = {0x13, 0x00, 0x15, 0x00, 0x93,
0xcd, 0x55,
Then what would be the command for displaying this image in a JPanel
(assuming I fix the dimensions if they are wrong)?

Well, I have no idea what format you're using for encoding the image
as a sequence of bytes. Is this a greyscale image with 256 intensities,
and each byte represents such an intensity? Assuming that's the case, it'd
probably look something like this:

int xOffSet, yOffset; /*initialize these somehow, depending on where you
want the image drawn*/
Graphics g = /*get this somehow, probably passed into your paint method by
the Swing API*/;
for (int i = 0; i < test_bits.length; i++) {
int x = i % MyEmbeddedImage.WIDTH;
int y = i / MyEmbeddedImage.WIDTH;
int intensity = MyEmbeddedImage.test_bits;
g.setColor(new Color(intensity, intensity, intensity));
g.fillRect(x + xOffSet, y + yOffset, 1, 1);
}

You might want to draw this image once, store it in some sort of
buffer, and then use the Graphics.copyImage() method to copy from it onto
the actual image associated with the JFrame.

- Oliver
 
K

Knute Johnson

Hi,

I want to display a 16x16 image in my application/applet and I don't
want to deal with jars/urls/resources/etc. Is there a way to embed an
image in code in some ascii format and then display it?

Thanks!

Aaron Fude

There are two very simple ways, look at BufferedImage.setRGB() and the
MemoryImageSource class. The other way depending on how complicated
your image is is to just draw it. Create a 16x16 BufferedImage and get
a Graphics for it and draw on it.

I think it is actually easier to embed it into a jar or get it from your
website or something but have at it.
 
T

Tor Iver Wilhelmsen

In C++ I could do this:

#define test_width 16
#define test_height 7
static char test_bits[] = {
0x13, 0x00, 0x15, 0x00, 0x93, 0xcd, 0x55, 0xa5, 0x93, 0xc5, 0x00,
0x80,
0x00, 0x60, };

Is there an equivalent in Java?

Not really, because Java does not have a data segment. So you *could*
create such an array in Java, but when compiled it would consist of a
sequence of statements that built up an array by adding the elements one
by one. And you would still need code (using BufferedImage and
WritableRaster) to turn that bitmap data into an image.

Using file/classpath resources is the way to go - why do you want to avoid
it?
 
A

angrybaldguy

In C++ I could do this:
#define test_width 16
#define test_height 7
static char test_bits[] = {
0x13, 0x00, 0x15, 0x00, 0x93, 0xcd, 0x55, 0xa5, 0x93, 0xc5, 0x00,
0x80,
0x00, 0x60, };
Is there an equivalent in Java?

public final class MyEmbeddedImage {
final public static int WIDTH = 16;
final public static int HEIGHT = 7;
final public static byte[] = {0x13, 0x00, 0x15, 0x00, 0x93, 0xcd, 0x55,
0xa5, 0x93, 0xc5, 0x00, 0x80, 0x00, 0x60, };

}

Though it's not clear to me the relationship between your chars/bytes
and your height and width. The number of elements in the array (14)
certainly does not seem to equate with 16x7.

- Oliver

It does if you count bits, rather than bytes. That said, there aren't
many facilities to load such a bitmap directly into an Image that I
know of; you'd have to write at least a small amount of loader code to
translate it into raster data or Graphics draw operations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top