Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Discussion: Images in applets

Status
Not open for further replies.

krip

Technical User
Dec 11, 2002
2
CA
Is anyone out there?

Say that I have the a byte array representation of a gif file. How would I create an Image object out of it?

(it's either that or you give me a better way to extract .gif files from jars

Regards Ira.

 
I struggled with this for a bit, when I wrote a simple applet for my web page.... Here is how you can do it...

I am just dumping the html code and the java code since i really don't have the time to explain, just basically jar files work like zip files. In the java program you simply have to make sure the main program knows where to find the .gif file or whatever - here you go

<html>
<body TEXT=&quot;#c0c0c0&quot; LINK=&quot;#c0c0c0&quot; VLINK=&quot;#c0c0c0&quot; BGCOLOR=&quot;#000000&quot; alink=&quot;#FF8000&quot;>

<applet code=&quot;CardFile.class&quot; archive=&quot;CardFile.jar&quot; width=140 height=300 align=left>
<PARAM NAME=&quot;website0&quot; VALUE=&quot;Mainpage.html&quot;>
<PARAM NAME=&quot;website1&quot; VALUE=&quot;starcraft.html&quot;>
<PARAM NAME=&quot;website2&quot; VALUE=&quot;programming.html&quot;>
<PARAM NAME=&quot;website3&quot; VALUE=&quot;spidey.html&quot;>
<PARAM NAME=&quot;website4&quot; VALUE=&quot;links.html&quot;>
<PARAM NAME=&quot;website5&quot; VALUE=&quot;badstuff.html&quot;>
<PARAM NAME=&quot;image0&quot; VALUE=&quot;images/duke_wave.gif&quot;>
<PARAM NAME=&quot;image1&quot; VALUE=&quot;images/star1.jpg&quot;>
<PARAM NAME=&quot;image2&quot; VALUE=&quot;images/101.jpg&quot;>
<PARAM NAME=&quot;image3&quot; VALUE=&quot;images/spider1.jpg&quot;>
<PARAM NAME=&quot;image4&quot; VALUE=&quot;images/sp1.jpg&quot;>
<PARAM NAME=&quot;image5&quot; VALUE=&quot;images/cdc.gif&quot;>

<PARAM NAME=&quot;frame&quot; VALUE=&quot;MainPage&quot;>
<PARAM NAME=&quot;bgcolor&quot; VALUE=&quot;000000&quot;>
</applet>




</body>
</html>

/*
Programmer Tom Moses, do not care if you use this just give me credit and do not use
this for a school projects your just a lamer if you do so. ackka

Very simple applet, the repeating rectangle give the illusion of 3d
The z index in the CardData array controls which will be displayed
first, a z index of the highest number will mean it is displayed on top since this
was the easeist way to program and it is logical so you can possibly add as
many possbile cards as you want


*/
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
class CardData
{
int x = 0;
int y = 0;
int z = 0;
int width = 0;
int height = 0;
Color CardColor = new Color(0xff, 0xff, 0x0f);//use static for now
Image CardImage;
//stores the image that will be displayed on the card
String website;
//stores the website that this card will go to
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
public class CardFile extends Applet implements MouseListener, Runnable,MouseMotionListener
{
int maxx, maxy;//the maxx and maxy from the applet widht and height
int maxx12,maxx14, maxx18,maxx34,maxx78,maxy12,maxy14,maxy18,maxy34,maxy78;
int mousex,mousey;

Cursor hand = new Cursor(Cursor.HAND_CURSOR);

int maxcards = 6, currentcard = 0;
//max cards is the total number of careds that will be displayed on the screen and the
//current card is the card that is being looked at in the compare area method

CardData[] CardDataArray= new CardData[maxcards];
CardData[] CardAreaArray= new CardData[maxcards];//use for the compare area method

Image offscreenImg;
Graphics offscreen;

String frame;
String website0;
String background;

int r = 100, b =255,g =100;
Color blink = new Color(r,g,b); int blinkctr = 0;//these variables control the blinking text

Thread runner;

public void init()
{
//grabs the width and height of the applet
maxx = getSize().width;
maxy = getSize().height;
maxx12 = (maxx / 2); //.5
maxx14 =(maxx/2)/2; //.25
maxx18 = (((maxx/2)/2)/2); // 0.125
maxx34 = maxx12 + maxx14; //.75
maxx78 = maxx12 + maxx14 + maxx18; //.875
//these lengths will be added to the x position to acheive the desired pos

//these variables con
maxy12 = (maxy / 2); //.5
maxy14 =(maxy/2)/2; //.25
maxy18 = (((maxy/2)/2)/2); // 0.125
maxy34 = maxy12 + maxy14; //.75
maxy78 = maxy12 + maxy14 + maxy18; //.875



//double buffering stuff
offscreenImg = createImage(maxx, maxy);
offscreen = offscreenImg.getGraphics();

addMouseListener(this);
addMouseMotionListener(this);

setBackground(Color.black);

frame = getParameter(&quot;frame&quot;);
website0 = getParameter(&quot;website0&quot;);

//this grabs each of the paramter names form the hmtl file for the images
//and pushes them into a image variable which will then be displayed on the fronts
//of the cards

for (int i = 0; i < maxcards; i++)
{
CardDataArray = new CardData();
CardAreaArray = new CardData();
CardDataArray.CardImage= getImage(getCodeBase(),getParameter(&quot;image&quot; + i));
CardDataArray.website = getParameter(&quot;website&quot; + i);
}

initCards();

}
//////////////////////////////////////////////////////////////////////////////////////////////////////
public void initCards()
{
//this function is a very simple initialization function which sets all the values of the cards
//the values will be modifies later by the listener functions


for(int i = 0; i < maxcards; i++)
{
CardDataArray.x = maxx18/2/2/2;
for(int x = 0; x < i; x++)
CardDataArray.x += maxx18;
//this simple for loop allows the vertical positioning of the cards in a decreasing
//order
CardDataArray.y = maxy18/2/2/2;
for(int x = 0; x < i; x++)
CardDataArray.y += maxy18;


//the z postion is set
CardDataArray.z = i;

CardDataArray.width = maxx14;
CardDataArray.height = maxy14;

if (CardDataArray.x > maxx)
showStatus(&quot;The Cards are going of the screen!!!&quot;);
//good example of being a lazy programmer :)
if (CardDataArray.y > maxy)
showStatus(&quot;The Cards are going of the screen!!!&quot;);
}


}
//////////////////////////////////////////////////////////////////////////////////////////////////////
public void run()
{


for(;;)
{
pause(1500);
repaint();
}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////
public void paint(Graphics screen)
{
offscreen.setColor(Color.black);
offscreen.fillRect(0,0,maxx,maxy);


//this for loop prints out the cards based upon the order of the z postion, if the z position is
//zero it will be the first to be printed and will be behind every other object
//also need to make sure if set a z position to reset all other positions
for(int i = 0; i < maxcards; i++)
for(int x = 0; x < maxcards; x++)
if (CardDataArray[x].z == i)
paintFile(offscreen,x);

paintBlinking(offscreen);
//calls the function that paints the name of the current webpage to go to
//i didn;t exactly get this to blink like i wanted it to :(

//this is how to do double buffering i can't explain all the details just get a book :)
screen.drawImage(offscreenImg,0,0,this);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////

public void paintFile(Graphics screen, int cardnum)
{

for (int i=0; i < 10; i++)
{
//this next line produces the random line color effect scene in the applet
CardDataArray[cardnum].CardColor = new Color(150,randomInt(255),150);
screen.setColor(CardDataArray[cardnum].CardColor);
screen.fillRect(CardDataArray[cardnum].x+i,CardDataArray[cardnum].y+i,CardDataArray[cardnum].width+i+i,CardDataArray[cardnum].height);
if (i == 9)
{

//this displays the image as the last thing
screen.drawImage(CardDataArray[cardnum].CardImage, CardDataArray[cardnum].x+i,CardDataArray[cardnum].y+i, CardDataArray[cardnum].width+i+i,CardDataArray[cardnum].height, this);

//this copies the x,y,widthand height of the image displayed to be used in compare area()
CardAreaArray[cardnum].x = CardDataArray[cardnum].x+i;
CardAreaArray[cardnum].y = CardDataArray[cardnum].y+i;
CardAreaArray[cardnum].width = CardDataArray[cardnum].width+i+i;
CardAreaArray[cardnum].height = CardDataArray[cardnum].height;
}
}


}
public void paintBlinking(Graphics screen)
{
Font font = new Font(&quot;Times New Roman&quot;, Font.BOLD, maxx18);
screen.setFont(font);

screen.setColor(blink);
offscreen.drawString(CardDataArray[currentcard].website,maxx18+maxx18/2/2, maxy-(maxy18/2/2));

}
//////////////////////////////////////////////////////////////////////////////////////////////////////
public void mouseClicked(MouseEvent e)
{

mousex=e.getX();
mousey=e.getY();
compareArea();


try
{
AppletContext context = getAppletContext();
URL u = new URL(getCodeBase(),CardDataArray[currentcard].website);
context.showDocument(u,&quot;MainArea&quot;);
}
catch(Exception error)
{
showStatus(&quot;Error &quot; + error);
}



}
public void mouseEntered(MouseEvent e)
{
/*
try
{
AppletContext context = getAppletContext();
URL u = new URL(getCodeBase(),website0);
context.showDocument(u,&quot;MainArea&quot;);
}
catch(Exception error)
{
showStatus(&quot;Error &quot; + error);
}
*/
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// need to satisfy listener interfaces -- two methods
public void mouseMoved(MouseEvent e)
{

}

///////////////////////////////////////////////////////////////////////////////////////////////
public void mouseDragged(MouseEvent e) {

}
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
public void compareArea()
{
//this works perfectly except that it is not supported by #$%%% microsoft
//offscreen.setClip(CardAreaArray[0].x,CardAreaArray[0].y,CardAreaArray[0].width,CardAreaArray[0].height);
//if (offscreen.hitClip(mousex,mousey,1,1) == true)
boolean hitcards = false;

for (int i = 0; i < maxcards; i++)
{
if (mousex >= CardAreaArray.x &amp;&amp; (CardAreaArray.x+CardAreaArray.width) >= mousex)
{
if (CardAreaArray.y <= mousey &amp;&amp; (CardAreaArray.y+CardAreaArray.height) >= mousey)
{
hitcards = true;
setCursor(hand);
currentcard = i;

int newzvalue = 0;
for(int x = 0; x < maxcards; x++)
{

if (i == x)
CardDataArray[x].z = maxcards-1;
else
{
CardDataArray[x].z = newzvalue;//this is so the other cards will be reorganzined into 0-4
newzvalue++;
}
}
repaint();
break;
}
}
else
hitcards = false;

}

if (hitcards == false)
{
setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
initCards();
repaint();
}

}
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
public int randomInt(int max) {
double x =
Math.floor((double)(max + 1) * Math.random());
return((int)(Math.round(x)));
}
////////////////////////////////////////////////////////////////////////////////////////////

public void update(Graphics screen)
{
paint(screen);
}
////////////////////////////////////////////////////////////////////////////////////////////
public void destroy()
{
offscreen.dispose();
}
////////////////////////////////////////////////////////////////////////////////////////////
//grabbed these six functions from Core Web examples at
//1998 Marty Hall,
public void pause(int milliseconds)
{
try {
Thread.sleep((long)milliseconds);
} catch(InterruptedException ie) {}

}
//----------------------------------------------------
// Returns an int from 0 to max (inclusive),
// yielding max + 1 possible values.
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
public void start()
{
if (runner == null)
{
runner = new Thread(this);
runner.start();
}
}
////////////////////////////////////////////////////////////////////////////////////////////
public void stop()
{
if (runner != null)
{
runner = null;
}
}

////////////////////////////////////////////////////////////////////////////////////////////



}
//////////////////////////////////////////////////////////////////////////////////////////////////////


/*
/
int websiteslength = 5;
String[] WebParams = new String[websiteslength];
JButton[] websites = new JButton[websiteslength];
/
extra code
/
public void actionPerformed(ActionEvent event)
{

if (event.getSource() == websites[0])
{
try
{
URL u = new URL(WebParams[0]);
getAppletContext().showDocument(u, frame);
}
catch(MalformedURLException e) {}


}
else if (event.getSource() == websites[1])
{
websites[1].setText(&quot;asdf&quot;);
validate();
}
else if (event.getSource() == websites[2])
{
websites[2].setText(&quot;asdf&quot;);
validate();
}
else if (event.getSource() == websites[3])
{
websites[3].setText(&quot;asdf&quot;);
validate();
}
else if (event.getSource() == websites[4])
{
websites[4].setText(&quot;asdf&quot;);
validate();
}

}
/
/
for (int i =0;i<websiteslength;i++)
{
websites = new JButton(&quot;Website&quot; + i);
add(websites);
websites.addActionListener(this);
}
for (int i =0;i<websiteslength;i++)
{
WebParams = new String();
String test = getParameter(&quot;website&quot; + i);
if (test == null)
System.out.println(&quot;This was no parameter entered for param: website&quot; + i);
else
WebParams = test;
}
/

/
if (test == null)
System.out.println(&quot;This was no parameter entered for param: frame&quot;);
else
frame = test;
/
/
try
{
URL u = new URL(website0);
getAppletContext().showDocument(u, frame);
}
catch(MalformedURLException e) {}
/
//MediaTracker tracker = new MediaTracker();
//tracker.addImage(frontImage, 0);
//try { tracker.waitForID(0); }
//catch(InterruptedException e) {}
//this class will control all the graphical movements of each individual folder
//an image of the html file and the name of the file will appear on the folder
//also the folder will appear 3d this will be done by slowling decrementing
//the color and painting rectangles after the file rectangle
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
class File
{
int x,y,z,width,height; //these variable control the movements of a File
Color MainColor = new Color(0xff, 0xff, 0x0f);//use static for now

//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
File(int x, int y, int z, int width,int height)
{
//the width height and zindex that this graphics context will be show in
this.width = width;
this.height = height;
this.z = z;
this.x = x;
this.y = y;

}
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
public void paintFile(Graphics screen, Image frontImage)
{
int width12 = (width / 2); //.5
int width14 =(width/2)/2; //.25
int width18 = (((width/2)/2)/2); // 0.125
int width34 = width12 + width14; //.75
int width78 = width12 + width14 + width18; //.875
//these lengths will be added to the x position to acheive the desired pos

//these variables con
int height12 = (height / 2); //.5
int height14 =(height/2)/2; //.25
int height18 = (((height/2)/2)/2); // 0.125
int height34 = height12 + height14; //.75
int height78 = height12 + height14 + height18; //.875

for (int i=0; i < 10; i++)
{
MainColor = new Color(150,randomInt(255),150);
screen.setColor(MainColor);
screen.fillRect(x+i,y+i, width+i+i,height);
if (i == 9)
{
//this displays the image as the last thing
screen.drawImage(frontImage, x+i,y+i, width+i+i,height, new ImageObserver());

}
}




}
// Returns an int from 0 to max (inclusive),
// yielding max + 1 possible values.
////////////////////////////////////////////////////////////////////////////////////////////
public int randomInt(int max) {
double x =
Math.floor((double)(max + 1) * Math.random());
return((int)(Math.round(x)));
}

}


if(blinkctr < 2 &amp;&amp; blinkctr >= 0)
{
blink = blink.brighter();
screen.setColor(blink);
offscreen.drawString(CardDataArray[currentcard].website,maxx18, maxy-(maxy18/2/2));
blinkctr++;
}
if (blinkctr > 1)
{
blink = blink.darker();
screen.setColor(blink);
offscreen.drawString(CardDataArray[currentcard].website,maxx18, maxy-(maxy18/2/2));
blinkctr--;
}





if (blinkctr == 0)
{
screen.setColor(blink);
offscreen.drawString(CardDataArray[currentcard].website,maxx18, maxy-(maxy18/2/2));
blinkctr ++;
}
else if (blinkctr == 1)
{
blink = blink.brighter().brighter().brighter().brighter().brighter();
screen.setColor(blink);
offscreen.drawString(CardDataArray[currentcard].website,maxx18, maxy-(maxy18/2/2));
blinkctr ++;
}

else if (blinkctr == 2)
{
blink = blink.brighter();
screen.setColor(blink);
offscreen.drawString(CardDataArray[currentcard].website,maxx18, maxy-(maxy18/2/2));
blinkctr ++;
}
else if (blinkctr == 3)
{
blink = blink.brighter();
screen.setColor(blink);
offscreen.drawString(CardDataArray[currentcard].website,maxx18, maxy-(maxy18/2/2));
blinkctr ++;
}
//this is to set the background to a specific color
//the values in the bgcolor are the same as in the body tag in a html document
//they are in hexadecimal where 00 stands for 8bits or one interger
//that means when i convert it, it will be seperated into three ints for
//red blue and green
//bgcolor = getParameter(&quot;bgcolor&quot;);
//int r = (int) bgcolor.charAt(0)




for (int i = 0; i < maxcards; i++)
{
if (temp1 == true)
{
CardDataArray.x = maxx18/2;
CardDataArray.y = maxy18/2;
CardDataArray.width = maxx14;
CardDataArray.height = maxy14;
temp1 = false;
}
else if(temp1 == false)
{
CardDataArray.x = maxx12;
temp1 = true;
}
else
{
System.out.println(&quot;There has been an error in the initCardsmethod()&quot;);
break;
}

CardDataArray.z = i;
}


*/
 
You either misunderstood me, or I misunderstood your code..

I need to be able to build an Image out of it's raw byte data, or alternately be able to create Image objects out of gifs in a jar file.
The only place you got images in the code (that I could see) was in this line:

CardDataArray.CardImage= getImage(getCodeBase(),getParameter(&quot;image&quot; + i));

and well I have a seperate Jar file for images that hold some hundreds of Gifs with similira names (under different paths). I can't see how this would help me, but I might be mistaken as I work to be compatible with SDK 1.1.7 and you might be working on a newer version.

again thanks... Ira



 
Ira,

sorry to do this to you. I have a question about your question.
Are you working with .gifs in this way for reasons of compression? I have a project on the horizon where I'll be dealing with hundreds possible thousands of .gifs.

I was imagining a way to store them in binary?
we have similar idea, no?



Lupine
RobertCorrina@patownsend.com

 
ira ...

i think what acka is trying to say is that if you have the image file in the jar file under the specific directory then just tell the program where it is and it will find it.

i think that,

import java.awt.*

Image myImage = ToolKit.getImage(&quot;images/image1.gif&quot;);

i think that that will work if you have image1.gif in the jar file under a subdirectory images ... try it :)

and lupine .... ummm i dont know, i think that if you want to save space with the images try maybe saving them as jpegs, they tend to be smaller, what kinda images are they and what are you using them for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top