Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Embed a small image in code

Thread view: 
aaronfude@gmail.com - 23 Feb 2007 18:07 GMT
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
Andrew Thompson - 23 Feb 2007 18:18 GMT
On Feb 24, 5:07 am, aaronf...@gmail.com 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.
aaronfude@gmail.com - 23 Feb 2007 18:31 GMT
> What more could be added?

If you wanted to spoil me, a short summary of the bottom line!

> Andrew T.
Andrew Thompson - 23 Feb 2007 18:39 GMT
On Feb 24, 5:31 am, aaronf...@gmail.com wrote:
> > What more could be added?
>
> If you wanted to spoil me, a short summary of the bottom line!

"Don't"

Andrew T.
Joe Attardi - 23 Feb 2007 21:39 GMT
> 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>

Ah, sweet memories... :-)
Oliver Wong - 23 Feb 2007 18:36 GMT
> 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
aaronfude@gmail.com - 23 Feb 2007 18:47 GMT
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.
Christian - 23 Feb 2007 19:03 GMT
aaronfude@gmail.com schrieb:
> In C++ I could do this:
>
[quoted text clipped - 8 lines]
>
> 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?
Oliver Wong - 23 Feb 2007 19:06 GMT
> In C++ I could do this:
>
[quoted text clipped - 6 lines]
>
> 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
aaronfude@gmail.com - 23 Feb 2007 19:16 GMT
> <aaronf...@gmail.com> wrote in message
>
[quoted text clipped - 18 lines]
>
> }

Then what would be the command for displaying this image in a JPanel
(assuming I fix the dimensions if they are wrong)?
Oliver Wong - 23 Feb 2007 19:29 GMT
>> <aaronf...@gmail.com> wrote in message
>>
[quoted text clipped - 16 lines]
>>   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,

>> 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)?

   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[i];
 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
angrybaldguy@gmail.com - 26 Feb 2007 05:32 GMT
> <aaronf...@gmail.com> wrote in message
>
[quoted text clipped - 24 lines]
>
>     - 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.
Tor Iver Wilhelmsen - 25 Feb 2007 22:21 GMT
På Fri, 23 Feb 2007 19:47:59 +0100, skrev <aaronfude@gmail.com>:

> In C++ I could do this:
>
[quoted text clipped - 6 lines]
>
> 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?
Knute Johnson - 23 Feb 2007 23:24 GMT
> Hi,
>
[quoted text clipped - 5 lines]
>
> 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.

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.