Thank You,
Andy
//Web Page
//By: Andrew Titus
//Date 2-26
import javax.swing.JApplet;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.util.*;
import java.awt.*;
import javax.swing.ImageIcon;
import java.awt.event.*;
import javax.swing.*;
public class WebApplet extends JApplet
{
private JButton previous,next;
private JPanel buttonPanel;
Image coolImage[] = new Image[5];
Font title,documentText;
int i = 0;
String[] names = {"StBasials1.jpg", "museum9.jpg", "Red_Square9.jpg",
"StBasials8.jpg",
"Russian_Soilders_in_Formation.jpg" };
public void init()
{
previous = new JButton("Previous");
next = new JButton("Next");
for(int j=0; j<names.length;j++) {
coolImage[j] = getImage(getCodeBase(),names[j] );
}
ButtonListener listener = new ButtonListener();
previous.addActionListener(listener);
next.addActionListener(listener);
buttonPanel = new JPanel();
buttonPanel.add(previous);
buttonPanel.add(next);
add(buttonPanel);
} // end init()
public void paint(Graphics page)
{
setBackground(Color.black);
title = new Font("Jokerman",Font.BOLD,(int)(getHeight()*0.02) );
documentText = new Font("Courier New",Font.BOLD,(int)(getHeight()*0.02) );
//set color for text
page.setColor(Color.white);
page.setFont(title);
page.setFont(documentText);
page.drawImage(coolImage[i],30,60,this);
} //end paint()
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if(event.getSource() == next){
i++;
repaint();
}
if(event.getSource() == previous) {
i--;
repaint();
}
}
}
} //end webApplet
public class WebApplet extends JApplet{
private JButton previous,next;
private JPanel buttonPanel;
Font title, documentText;
int i = 0;
Container con;
ImagePanel ip;
String[] names
= {"Pic1.jpg", "Pic2.jpg", "Pic3.jpg", "Pic4.jpg", "Pic5.jpg" };
Image[] coolImage;
ImageIcon[] iia;
public void init(){
con = getContentPane();
// you should use MediaTracker, or, use ImageIcon that uses
// MediaTracker internally
coolImage = new Image[names.length];
iia = new ImageIcon[names.length];
for (int i = 0; i < names.length; ++i){
iia[i] = new ImageIcon(getImage(getCodeBase(),names[i]));
coolImage[i] = iia[i].getImage();
}
previous = new JButton("Previous");
next = new JButton("Next");
ButtonListener listener = new ButtonListener();
previous.addActionListener(listener);
next.addActionListener(listener);
ip = new ImagePanel();
buttonPanel = new JPanel();
buttonPanel.add(previous);
buttonPanel.add(next);
add(buttonPanel);
con.add(ip, BorderLayout.CENTER);
con.add(buttonPanel, BorderLayout.SOUTH);
}
class ImagePanel extends JPanel{
public ImagePanel(){
setBackground(Color.black);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
title = new Font("Jokerman",Font.BOLD,(int)(getHeight()*0.02) );
documentText
= new Font("Courier New",Font.BOLD,(int)(getHeight()*0.02) );
g.setColor(Color.white);
g.setFont(title);
g.setFont(documentText);
g.drawImage(coolImage[i],30,60,this);
}
} // class ImagePanel
private class ButtonListener implements ActionListener{
public void actionPerformed (ActionEvent event){
if(event.getSource() == next){
i++;
if (i >= names.length){
i = 0;
}
}
else if(event.getSource() == previous){
i--;
if (i < 0){
i = names.length - 1;
}
}
ip.repaint();
}
}
} // class webApplet
------------------------------------------------------------------
Andy
What's the exeception, and what line does the exception refer to?
- Oliver
exception: java.lang.nullPointerException error and the applet will not
load. Not sure why this is happening.
Thank You,
Andy
//Web Page
//By: Andrew Titus
//Date 2-26
//import java.applet.Applet;
import javax.swing.JApplet;
import java.awt.Event;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.util.*;
import java.awt.*;
import javax.swing.ImageIcon;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton;
Usually exceptions are accompanied with a stack trace which have line
numbers. If you could post the line numbers, it'd be easier to determine
where the null pointer exception is occurring. If you know how to use the
applet viewer, you might want to use that instead of a full fledge web
browser.
Otherwise, look in your browser for a feature to display the "Java
Console"; that should show a text box with the stacktrace present.
- Oliver
ÏException in thread "AWT-EventQueue-1"
java.lang.ArrayIndexOutOfBoundsException: 5
ÏÏ§Ï at WebApplet.paint(WebApplet.java:86)
ÏÏ§Ï at javax.swing.JApplet.update(JApplet.java:148)
ÏÏ§Ï at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
ÏÏ§Ï at sun.awt.RepaintArea.paint(RepaintArea.java:216)
ÏÏ§Ï at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:254)
ÏÏ§Ï at java.awt.Component.dispatchEventImpl(Component.java:4031)
ÏÏ§Ï at java.awt.Container.dispatchEventImpl(Container.java:2024)
ÏÏ§Ï at java.awt.Component.dispatchEvent(Component.java:3803)
ÏÏ§Ï at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
ÏÏ§Ï at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
ÏÏ§Ï at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
ÏÏ§Ï at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
ÏÏ§Ï at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
ÏÏ§Ï at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
ÏϧÏ
When I upload applet to internet to view in browser at the bottom of browser
it gives
exception: java.lang.nullPointerException error
THis is all it gives me down at bottom of browser
when I run in the applet viewer it lets me scroll through pictures but not
sure why out of bounds error and the other exception.
Thank You,
Andy
Andy
load: class WebApp.class not found.
java.lang.ClassNotFoundException: WebApp.class
at sun.applet.AppletClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadCode(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.plugin.AppletViewer.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: open HTTP connection failed.
at sun.applet.AppletClassLoader.getBytes(Unknown Source)
at sun.applet.AppletClassLoader.access$100(Unknown Source)
at sun.applet.AppletClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
... 10 more
Exception in thread "Thread-4" java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletStatus(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "thread applet-WebApp.class"
java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
java.lang.NoClassDefFoundError: WebApplet$ButtonListener
at WebApplet.init(WebApplet.java:43)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "thread applet-WebApplet.class"
java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Andy
What did you do to fix the error? You didn't simply rename the file to
"WebApp.class", did you? The name of the class file must match the name of
the public class declared within that file. Your file has a line which reads
"public class WebApplet extends JApplet", so the public class is called
"WebApplet", and the class file must be named "WebApplet.class".
- Oliver
Hey I want to thank you for your time and help. If you did not point that I
can view error with browser in IE 6 I would have never found the mistakes.
Andy
Andy
Might have something to do with threading; not sure. See this article:
http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html
It says you shouldn't directly edit the GUI (e.g. creating buttons and
adding them to panels, etc.) in the init() method, but that you should use
Swing's invokeAndWait() to create the buttons in the EDT.
>
> Hey I want to thank you for your time and help. If you did not point that
> I
> can view error with browser in IE 6 I would have never found the mistakes.
Your welcome.
- Oliver
The most important one is:
public void paintComponent(Graphics g){ // not overriding paint() for
Swing component
The paint() method of Swing component should
call atleast three paintXxxx() methods, one of
which is paintComponent(). So you should never
override the paint() method in your custom class.
And make a custom panel and override its
paintComponent() method. Don't do it
for you JApplet class. Because JApplet
is, like JFrame, a top level composite
container. You should add components
on its content pane.
And, this is the array index part of my code: