Question regarding Japplets inside a webpage?

S

Shraff

I have a java applet that scrolls through some pictures that are found
in the same directory as the java code. When I run the applet from
inside of my compiler everything seems to work fine. However, when I
insert the applet into html code using the applet tag or the object
tag it dosent seem to work. The applet will appear and the buttons
will be there but when the start button is pressed it will not scroll
the pictures. I am new to adding applets to the web and am not sure
what my problem is.. any help would be greatly appreciated. Here is
the code I am using.

Html Tag:
<applet code=gifAnimation.class name=gifAnimation
archive=imageScroll.jar
width=300 height=400>
<param name="bgcolor" value="ffffff">
<param name="fontcolor" value="000000">
Your browser is not Java enabled.
</applet>

Java Applet:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.Locale;

class FillPictureBox extends Thread
{

private Graphics g;
private boolean keepscrolling = true;
private int scrollSpeed, numOfPics;
ImageIcon[] pictures;
private Container co;


public FillPictureBox(Graphics graphics, Container c, ImageIcon
pics[], int numberofPics)
{
g = graphics;
pictures = pics;
co = c;
numOfPics = numberofPics;

keepscrolling = true;
}

public void run()
{


while(keepscrolling == true)
{
for(int x =0; x < numOfPics; x++)
{
pictures[x].paintIcon(co,g, 0, 0);
for(int y=0; y < 10000; y++) { } // Fine tunning the
scroll.

if(keepscrolling == false) { x = numOfPics - 1; }

try {Thread.sleep(300);}

catch(InterruptedException e){}
}

}
}

public void stopnow()
{

keepscrolling = false;
}

public void fillPictureBox(Graphics g)
{
}

public void setScrollSpeed(int speed)
{
}


}


public class gifAnimation extends JApplet implements ActionListener,
MouseListener
{



Graphics g = getGraphics();

FillPictureBox movingPicture;

Container c = getContentPane();
JLabel titleLabel, blankLabel;
JButton startGamebt, stopbt;
JPanel northP, southP, eastP, westP;

Font titleFont = new Font("serif",Font.BOLD,34);
Font headFont = new Font("serif",Font.BOLD, 24);
Font textFont = new Font("serif",Font.ITALIC, 18);

private ImageIcon[] pics;
private ImageIcon[] thumbs;
private int numberofPics;
private boolean filled = false;


public void init()
{
gifAnimation window = new gifAnimation();
window.setSize(400,500);
window.setVisible(true);
}

public gifAnimation()
{
//Set JPanels
southP = new JPanel();

eastP = new JPanel();
northP = new JPanel();
westP = new JPanel(new FlowLayout());
southP.setLayout(new GridLayout(2,2));



c.setLayout(new BorderLayout());

//Set Labels
titleLabel = new JLabel("Esys Shop Cam 2");
blankLabel = new JLabel("");
//titleLabel.setFont(titleFont);


//Set Buttons
startGamebt = new JButton("Start");
//startGamebt.setFont(headFont);
stopbt = new JButton("Stop");
//stopbt.setFont(headFont);


//Add Widgets to panels
southP.add(titleLabel);
southP.add(blankLabel);
southP.add(startGamebt);
southP.add(stopbt);


startGamebt.addActionListener(this);
stopbt.addActionListener(this);


// Add JPanels to contentpane
c.add(northP, BorderLayout.NORTH);
c.add(southP, BorderLayout.SOUTH);
c.add(westP, BorderLayout.WEST);

this.addMouseListener(this); // register frame with MouseListener


}

public void actionPerformed(ActionEvent e)
{
Graphics g = getGraphics();

if(e.getSource() == startGamebt)
{
if(filled == false)
{
filled = true;
fillIconArray();
}
movingPicture = new FillPictureBox(g,c, thumbs, numberofPics);
movingPicture.start();
}

if(e.getSource() == stopbt)
{
movingPicture.stopnow();
}

/*

if(e.getSource() == slideB)
{

int newRate = slideB.getValue();
movingPicture.setScrollSpeed(newRate);

}
*/

}

public void fillIconArray()
{
numberofPics = 9;
pics = new ImageIcon[9];
thumbs = new ImageIcon[9];
String fileNames, extension;

extension = ".jpg";

for(int x=0; x< numberofPics; x++)
{

fileNames = "";
fileNames = Integer.toString(x);
fileNames = fileNames + extension;
pics[x] = new ImageIcon(fileNames);
thumbs[x] = new
ImageIcon(pics[x].getImage().getScaledInstance(427, 341,
Image.SCALE_FAST));

System.out.println(fileNames);
}
}

// Add MouseListeners

public void mousePressed(MouseEvent e)
{
}

public void mouseReleased(MouseEvent me)
{
}

public void mouseEntered(MouseEvent e)
{
}

public void mouseExited(MouseEvent e)
{
}

public void mouseClicked(MouseEvent e)
{

}



}
 
N

Nigel Wade

Shraff said:
I have a java applet that scrolls through some pictures that are found
in the same directory as the java code. When I run the applet from
inside of my compiler everything seems to work fine. However, when I
insert the applet into html code using the applet tag or the object
tag it dosent seem to work. The applet will appear and the buttons
will be there but when the start button is pressed it will not scroll
the pictures. I am new to adding applets to the web and am not sure
what my problem is.. any help would be greatly appreciated. Here is
the code I am using.


public void fillIconArray()
{
numberofPics = 9;
pics = new ImageIcon[9];
thumbs = new ImageIcon[9];
String fileNames, extension;

extension = ".jpg";

for(int x=0; x< numberofPics; x++)
{

fileNames = "";
fileNames = Integer.toString(x);
fileNames = fileNames + extension;
pics[x] = new ImageIcon(fileNames);
thumbs[x] = new
ImageIcon(pics[x].getImage().getScaledInstance(427, 341,
Image.SCALE_FAST));

System.out.println(fileNames);

What do you mean, in the context of an applet, by "in the same directory as the
java code"? Do you mean as separate files in the web server filesystem, or as
files within the applet's jar? Unfortunately in neither case can you use a
simple filename as you have in the above code. Also, a simple applet is unable
to load files from your local filesystem.

In the former case you need to construct a proper URL to the image file so it
can be requested from the web server. In the latter case you should use
ImageIcon(getClass().getResource(fileNames)) to locate the image file in the
jar. For more info. look at
http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html and also
Google "applet load image".
 
J

James

I have a java applet that scrolls through some pictures that are found
in the same directory as the java code. When I run the applet from
inside of my compiler everything seems to work fine. However, when I
insert the applet into html code using the applet tag or the object tag
it dosent seem to work. The applet will appear and the buttons will be
there but when the start button is pressed it will not scroll the
pictures. I am new to adding applets to the web and am not sure what my
problem is.. any help would be greatly appreciated. Here is the code I
am using.
[snip]

I did not critique your code because you said it works fine except when
running from the applet so....

Sign your jar file with jarsigner to read from your local filesystem.
Create your signature with keytool first.

When you load the applet, you will get prompted to trust the applet or
not.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top