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
/*
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
//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("image" + i));
CardDataArray.website = getParameter("website" + 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;
if (CardDataArray.x > maxx)
showStatus("The Cards are going of the screen!!!"
//good example of being a lazy programmer
if (CardDataArray.y > maxy)
showStatus("The Cards are going of the screen!!!"
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
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("Times New Roman", Font.BOLD, maxx18);
screen.setFont(font);
}
public void mouseEntered(MouseEvent e)
{
/*
try
{
AppletContext context = getAppletContext();
URL u = new URL(getCodeBase(),website0);
context.showDocument(u,"MainArea"
}
catch(Exception error)
{
showStatus("Error " + 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 && (CardAreaArray.x+CardAreaArray.width) >= mousex)
{
if (CardAreaArray.y <= mousey && (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,
}
//----------------------------------------------------
// 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("asdf"
validate();
}
else if (event.getSource() == websites[2])
{
websites[2].setText("asdf"
validate();
}
else if (event.getSource() == websites[3])
{
websites[3].setText("asdf"
validate();
}
else if (event.getSource() == websites[4])
{
websites[4].setText("asdf"
validate();
}
}
/
/
for (int i =0;i<websiteslength;i++)
{
websites = new JButton("Website" + i);
add(websites);
websites.addActionListener(this);
}
for (int i =0;i<websiteslength;i++)
{
WebParams = new String();
String test = getParameter("website" + i);
if (test == null)
System.out.println("This was no parameter entered for param: website" + i);
else
WebParams = test;
}
/
/
if (test == null)
System.out.println("This was no parameter entered for param: frame"
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)));
}
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("bgcolor"
//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("There has been an error in the initCardsmethod()"
break;
}
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:
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.
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?
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 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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.