Java Forum / First Aid / March 2006
displaying image
andrewtitus@alltel.net - 03 Mar 2006 02:24 GMT The applet reads in a file and uses the file to get picture names for the images for the applet. The program does all and compiles but I get: when I run the program. Why is this doing this if read in file and just has to iterate through array and get the string for the picture names.
Andy
[StBasials1.jpg, museum9.jpg, Red_Square9.jpg, StBasials8.jpg, Russian_Soilders_in_Formation.jpg , 11-07-05_0557.jpg, 11-07-05_0727.jpg, 11-07-05_0728.jpg, 11-07-05_1010.jpg, 11-07-05_1011.jpg, 11-07-05_10111.jpg, 11-08-05_0059.jpg, 11-08-05_0100.jpg, 11-08-05_0101.jpg, 11-08-05_0412.jpg] ÏϧÏException in thread "AWT-EventQueue-1" java.lang.NullPointerException ÏÏ§Ï at WebApplet.paint(WebApplet.java:101) ÏÏ§Ï at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248) ÏÏ§Ï at sun.awt.RepaintArea.paint(RepaintArea.java:224) ÏÏ§Ï 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:234) ÏÏ§Ï 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)
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 { private JButton previous,next; private JPanel buttonPanel; ArrayList names = new ArrayList(); String[] temp; Image[] coolImage; String url,pictureName; Scanner fileScan, urlScan; Font title,documentText; int i = 0;
public void init() {
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.size()];
for(int j=0; j<names.size();j++) {
coolImage[j] = getImage(getCodeBase(),(String)names.get(j)); }
} // end init() public void readFile() throws IOException {
fileScan = new Scanner (new File("pictures.txt"));
// Read and process each line of the file while (fileScan.hasNext()) {
names.add(fileScan.nextLine()); } System.out.println(names); }
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);
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.size()){
repaint(); }else i++; repaint(); } if(event.getSource()==previous) { if(i==names.indexOf(0)){
repaint(); }else i--; repaint(); }
}
}
} //end webApplet
andrewtitus@alltel.net - 03 Mar 2006 13:18 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
andrewtitus@alltel.net - 03 Mar 2006 13:30 GMT Wont display picture but makes red background where image should be. Not sure why not getting picture.
Andy
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.size()];
for(int j=0; j<names.size();j++) { coolImage[j] = getImage(getCodeBase(),(String)names.get(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()) {
names.add(fileScan.nextLine()); } //trace to see if it made it here System.out.println(names);
}
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.size()){ repaint(); }else i++; repaint(); } if(event.getSource()==previous) { if(i==0){ repaint(); }else i--; repaint(); }
} }
} //end webApplet
Thomas Weidenfeller - 03 Mar 2006 14:18 GMT > Wont display picture but makes red background where image should be. Not > sure why not getting picture. Use a debugger and check which data you get at which point.
> try{ > readFile(); [quoted text clipped - 3 lines] > ioex.printStackTrace(); > } Did you ever looked at the console? Is there maybe a stack trace? Is you applet signed?
> public void start() { > thread = new Thread(this); [quoted text clipped - 15 lines] > repaint(); > } Consider using a Runnable instead. And Thread.stop() is deprecated for good reasons. You probably have to remove the images from the media tracker to ensure it will stop ASAP.
> public void readFile() throws IOException > { [quoted text clipped - 12 lines] > > } Again: Is your applet signed?
> if ((tracker.statusID(0, true) & MediaTracker.ERRORED) != 0) { > page.setColor(Color.red); [quoted text clipped - 3 lines] > if ((tracker.statusID(0, true) & MediaTracker.COMPLETE) != 0) { > for (int i = 0; i < 10; i++) Hard-coded number of images, while in fact you try to read them dynamically from a file. How do you know you really got 10 images?
> private class ButtonListener implements ActionListener > { > public void actionPerformed (ActionEvent event) > { > > if(event.getSource()==next) { Use separate listeners. The events are initially separate. You lump them together into one handler, just so the first thing you have to do in this handler is to separate them again ...
> if(i==names.size()){ > repaint(); Why repaint() if apparently nothing has happened?
BTW: The usage of a global variable named 'i' is rather uncool.
> if(event.getSource()==previous) { > if(i==0){ > repaint(); See above.
/Thomas
 Signature The comp.lang.java.gui FAQ: ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
andrewtitus@alltel.net - 03 Mar 2006 14:36 GMT I tried running a stack trace in different locations and at all points it reads the file. I wrote this program with names of pictures hardcoded into array names and it works not sure why this is not. This is really confusing me. When I run the applet it flashes picture then turns red in display area.
Andy
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 counter = 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.size()];
for(int j=0; j<names.size();j++) { coolImage[j] = getImage(getCodeBase(),(String)names.get(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()) {
names.add(fileScan.nextLine()); // System.out.println(names); } }
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 < names.size(); 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(counter ==names.size()){ // repaint(); }else counter++; repaint(); } if(event.getSource()==previous) { if(counter ==names.indexOf(0)){ repaint(); }else counter--; repaint(); }
} }
} //end webApplet
James Westby - 03 Mar 2006 14:40 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. [snip]
> public void readFile() throws IOException > { [quoted text clipped - 8 lines] > k++; > } This loop sets k to 0 each iteration, meaning that only the first position is ever filled. Move int k = 0 outside the loop.
[snip]
James
andrewtitus@alltel.net - 03 Mar 2006 16:48 GMT I switched it back to an ArrayList. It reads in the names from the file and it gets the images not sure why no display. Is there something I am missing or is my way of thinking about the problem need adjusted.
Thank You, Andy
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 counter = 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.size()];
for(int j=0; j<names.size();j++) { coolImage[j] = getImage(getCodeBase(),(String)names.get(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()) {
names.add(fileScan.nextLine());
} System.out.println(names); }
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 < names.size(); 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(counter ==names.size()){ // repaint(); }else counter++; repaint(); } if(event.getSource()==previous) { if(counter ==names.indexOf(0)){ repaint(); }else counter--; repaint(); }
} }
} //end webApplet
Erich Blume - 03 Mar 2006 19:24 GMT On 3/3/06 8:48 AM, in article hZSdnc3B_5ba7pXZRVn-iQ@giganews.com,
> I switched it back to an ArrayList. It reads in the names from the file and > it gets the images not sure why no display. Is there something I am missing [quoted text clipped - 25 lines] > // String[] names; > ArrayList names = new ArrayList(); I've rarely used ArrayList, but I do know that I once ran in to a lot of trouble with them that was solved by calling names.trim(); right after instantiating a new ArrayList. Also, you'll want to make it an ArrayList of String, I believe. So this is your new code, if I am right:
ArrayList<String> names = new ArrayList<String>(); names.trim();
This was because there are null occupied spaces in the ArrayList at first. The exact reasons for that I don't know, I don't grok the ArrayList ADT. Probably, it's because of performance issues, and initializing it to ten spaces improves overall performance. You might want to read up on that.
There's another thing "wrong" about your code - you are way, way over-importing things. This won't change your code at all, but it makes for some strange reading. Change all your important statements to this:
import java.util.*; import java.awt.*; import javax.swing.*; import java.io.*; import java.net.URL.*;
The wildcard (*) will pull in all sub-elements.
Hope that helps some.
 Signature Erich Blume e b l u m e @ u c s c dawt e d u
Libros virumque cano.
andrewtitus@alltel.net - 03 Mar 2006 19:43 GMT I did what you said with the arry list and it cannot find name.trim().
Andy
> import java.util.*; > import java.awt.*; [quoted text clipped - 145 lines] > > } //end webApplet Erich Blume - 03 Mar 2006 19:39 GMT On 3/3/06 8:48 AM, in article hZSdnc3B_5ba7pXZRVn-iQ@giganews.com,
> I switched it back to an ArrayList. It reads in the names from the file and > it gets the images not sure why no display. Is there something I am missing > or is my way of thinking about the problem need adjusted. > > Thank You, > Andy ...
> fileScan = new Scanner (new File("pictures.txt")); > [quoted text clipped - 7 lines] > System.out.println(names); > } Wow, sorry for the double post, but that's very wrong code. I mean, it'll compile and work, but it's not a good idea. Calling println() on anything other than a primitive or a String calls a method on the object called "toString()" which returns a string. ArrayList.toString(), which is what you're calling in this line of code,
> System.out.println(names); Will cause very very ugly output. At least, not what you want. This is what you want. Replace the System.out line with this:
For (String line : names) System.out.println(line);
This will print out all the lines stored in names, and not the toString() value of names.
These two posts come from the two small chunks of code I've read of your entire post. There are likely to be additional bugs: I recommend you buy a book on java data structures, or even just data structures in general. There might be a reason you're using ArrayList (and it might be a good one, too), but for now might I recommend LinkedList? A LinkedList will have more overhead and a slower searching speed than any of array-based structures, but it is completely growable (with no costly array-copy operations) and rather easy to understand.
Also, the code I suggested above requires you declare your ArrayList with generics as I suggested in my previous post - in other words, replace the word ArrayList with ArrayList<String>. Otherwise, you'll need to modify my code to cast the objects to Strings.
Once more, hope I've helped some.
 Signature Erich Blume e b l u m e @ u c s c dawt e d u
Libros virumque cano.
andrewtitus@alltel.net - 03 Mar 2006 19:49 GMT The reason I am using array list is because I did not know how many images names would be stored in text file. I have program working when I hard code names of pictures into arrray. but now using a text file and it seems to read in but no disaplay. very confusing and frustrating. :)
Andy
andrewtitus@alltel.net - 03 Mar 2006 19:51 GMT I did the trace with the for loop and it printed out valuse in array. it is getting them to that point.
Andy
andrewtitus@alltel.net - 03 Mar 2006 21:52 GMT I got it working. I set my images to an array list also and it worked.
Andy
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|