Re: displaying image
- From: andrewtitus@xxxxxxxxxx
- Date: Fri, 3 Mar 2006 13:18:53 GMT
I added media tracker to the applet thinking that might help with the issue
Iam having but still same problem. Iam not sure what Iam doing wrong. Can
someone please point me in the right direction.
Thank You,
Andy
ERROR:
java.lang.NullPointerException
ÏÏ§Ï at WebApplet.readFile(WebApplet.java:99)
ÏÏ§Ï at WebApplet.init(WebApplet.java:54)
ÏÏ§Ï at sun.applet.AppletPanel.run(AppletPanel.java:373)
ÏÏ§Ï at java.lang.Thread.run(Thread.java:595)
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;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.net.URL.*;
public class WebApplet extends JApplet implements Runnable
{
private JButton previous,next;
private JPanel buttonPanel;
String[] names;
//ArrayList names = new ArrayList();
String[] temp;
Image[] coolImage;
String url,pictureName;
Scanner fileScan, urlScan;
Font title,documentText;
int i = 0;
Thread thread;
MediaTracker tracker;
public void init()
{
tracker = new MediaTracker(this);
previous = new JButton("Previous");
next = new JButton("Next");
ButtonListener listener = new ButtonListener();
previous.addActionListener(listener);
next.addActionListener(listener);
buttonPanel = new JPanel();
add(buttonPanel);
buttonPanel.add(previous);
buttonPanel.add(next);
try{
readFile();
}
catch (IOException ioex)
{
ioex.printStackTrace();
}
Image[] coolImage = new Image[names.length];
for(int j=0; j<names.length;j++) {
coolImage[j] = getImage(getCodeBase(),names[j]);
tracker.addImage(coolImage[j], 0);
}
} // end init()
public void start() {
thread = new Thread(this);
thread.start();
}
public void stop() {
thread.stop();
thread = null;
}
public void run() {
try {
tracker.waitForID(0);
}
catch (InterruptedException e) {
return;
}
repaint();
}
public void readFile() throws IOException
{
fileScan = new Scanner (new File("pictures.txt"));
// Read and process each line of the file
while (fileScan.hasNext())
{
int k = 0;
names[k]=fileScan.nextLine();
k++;
}
for(int h=0;h<names.length;h++) //trace to see if it made it here
System.out.println(names[h]);
}
public void paint(Graphics page){
super.paint(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);
if ((tracker.statusID(0, true) & MediaTracker.ERRORED) != 0) {
page.setColor(Color.red);
page.fillRect(0, 0, size().width, size().height);
return;
}
if ((tracker.statusID(0, true) & MediaTracker.COMPLETE) != 0) {
for (int i = 0; i < 10; i++)
page.drawImage(coolImage[i],0,30, this);
}
} //end paint()
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if(event.getSource()==next) {
if(i==names.length){
repaint();
}else
i++;
repaint();
}
if(event.getSource()==previous) {
if(i==0){
repaint();
}else
i--;
repaint();
}
}
}
} //end webApplet
.
- Follow-Ups:
- Re: displaying image
- From: James Westby
- Re: displaying image
- From: andrewtitus
- Re: displaying image
- References:
- displaying image
- From: andrewtitus
- displaying image
- Prev by Date: [beginner] Help - deprecated code (mouse events)
- Next by Date: Re: displaying image
- Previous by thread: displaying image
- Next by thread: Re: displaying image
- Index(es):
Relevant Pages
|