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)
{
}
}
Andrew Thompson - 02 Jan 2008 14:56 GMT
>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.
'seem'? I'll bet if you check the Java Console you will
see a lot more specifically what is happening. Something
like a..
java.security.AccessControlException
..when you hit that 'start' button.
(BTW - I don't know what you are doing there, but it would
probably be a good idea to be loading the images in a
separate thread in the BG, or have the applet default to
'load and play at start-up'.)
An unstrusted applet cannot load File(s) off the local file system,
a trusted applet can load File(s) off the local file system, but not
the server. To get resources from the server, you need to use
URL(s).
There are some good examples of loading images via URL,
here*. In fact, take a look at Image Fader JApplet, it is
very similar to what you seem to be doing.
* <http://rabbitbrush.frazmtn.com/>

Signature
Andrew Thompson
http://www.physci.org/
Nigel Wade - 02 Jan 2008 15:26 GMT
> 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
[quoted text clipped - 5 lines]
> what my problem is.. any help would be greatly appreciated. Here is
> the code I am using.
> public void fillIconArray()
> {
[quoted text clipped - 17 lines]
>
> 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".

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
James - 02 Jan 2008 22:39 GMT
> 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
[quoted text clipped - 5 lines]
> 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.

Signature
James
*Note: Remove every other letter for correct email address