the applet compiles but does not display text in the array. Can someone
point me in the right direction.
Andy
import java.util.Scanner;
import java.io.*;
import javax.swing.JApplet;
import java.awt.*;
public class URLDissector extends JApplet
{
String[] names;
String url;
Scanner fileScan, urlScan;
int i = 0;
//-----------------------------------------------------------------
// Reads urls from a file and prints their path components.
//-----------------------------------------------------------------
public void init()
{
try{
readFile();
}
catch (Exception IOexception){}
}
public void readFile() throws IOException
{
fileScan = new Scanner (new File("pictures.txt"));
// Read and process each line of the file
while (fileScan.hasNext())
{
// url = fileScan.nextLine();
names[i] = fileScan.nextLine();
System.out.println (names[i]);
i++;
}
}
}
Paul Hamaker - 02 Mar 2006 08:55 GMT
First of all, your array doesn't exist, because you forgot to create
it, as in new String[50], fe.
Furthermore you might want to consider using ArrayList, since you don't
know how many names you'll be reading and AL can grow automatically.
Then, if you have a try catch in your code, you should definitely show
the exception in the catch.
And last, if you want this to become an applet to be hosted somewhere,
you should use something like URL's openStream or Class'
getResourceAsStream to read the file from the host (or the archive).
--------------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com
Roland de Ruiter - 02 Mar 2006 12:16 GMT
> the applet compiles but does not display text in the array. Can someone
> point me in the right direction.
>
> Andy
> readFile();
> }
> catch (Exception IOexception){}
> }
Don't discard exceptions in this way. It will give you important hints
on what might be wrong. Use this instead:
readFile();
}
catch (IOException ioex)
{
ioex.printStackTrace();
}
Open the Java Console in your browser (in Internet Explorer use menu
Tools > Sun Java Console) and watch the (error)messages appearing in the
console while you run your applet.
Regards,
Roland
andrewtitus@alltel.net - 02 Mar 2006 15:12 GMT
Iam trying to read file into an array list and it reads file in but there is
a problem taking names from ArrayList and putting them in my Image array to
get pictures. The error I get is:
WebApplet.java:60: incompatible types
ÏϧÏfound : java.lang.String
ÏϧÏrequired: java.awt.Image
ÏÏ§Ï coolImage[j] =(String)names.get(j);
ÏÏ§Ï ^
ÏϧÏNote: G:\Java Programs I wrote\WebApplet.java uses unchecked or unsafe
operations.
ÏϧÏNote: Recompile with -Xlint:unchecked for details.
ÏϧÏ1 error
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;
public class WebApplet extends JApplet
{
private JButton previous,next;
private JPanel buttonPanel;
ArrayList names = new ArrayList();
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();
}
for(int j=0; j<names.size();j++) { //problem is here i think. I tried
casting to string but no luck
coolImage[j] =(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==4){
repaint();
}else
i++;
repaint();
}
if(event.getSource()==previous) {
if(i==0){
repaint();
}else
i--;
repaint();
}
}
}
} //end webApplet
Roedy Green - 02 Mar 2006 15:28 GMT
>coolImage[j] =(String)names.get(j);
You are hoping Java will magically convert a string "apple" in to a
picture an apple. It requires considerable hints to perform that feat.
See http://mindprod.com/jgloss/image.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
andrewtitus@alltel.net - 02 Mar 2006 17:20 GMT
I know what you are saying but not sure how to do it. I read in file as
strings into ArrayList now I have to use each name in ArrayList and get
image.
for(int j=0; j<names.size();j++) { // this gets array list till the end
coolImage[j] = getImage(getCodeBase(),names.get(j));
//The types are imcompatiable. I know why but not sure how to make them
compatiable
}
I looked at the site you sent to but not sure what to look for.
Andy