Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

didplay problem

Thread view: 
andrewtitus@alltel.net - 02 Mar 2006 03:06 GMT
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


Free Magazines

Get 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 ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.