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.

ArrayList

Thread view: 
andrewtitus@alltel.net - 02 Mar 2006 21:40 GMT
Iam writting a program that read a external file into an applet which uses
the names in the ArrayList to get images. The error I get is:

ÏWebApplet.java:62: cannot find symbol
ÏϧÏsymbol  : method getImage(java.lang.Object)
ÏϧÏlocation: class WebApplet
ÏÏ§Ï         coolImage[j] =  getImage(names.get(j))

Someone pointed out that the types are incompatible but not sure how to get
names of pictures from ArrayList to my coolImage[]. Is there anyway to
convert this to be compatible or is my logic wrong.

for(int j=0; j<names.size();j++) {

        coolImage[j] =  getImage(names.get(j));
        //  coolImage[j] = getImage(getCodeBase(),names.get(j));
    }   

Thank You,
Andy
Hendrik Maryns - 02 Mar 2006 22:06 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

andrewtitus@alltel.net uitte de volgende tekst op 03/02/2006 10:40 PM:
> Iam writting a program that read a external file into an applet which uses
> the names in the ArrayList to get images. The error I get is:
[quoted text clipped - 13 lines]
>         //  coolImage[j] = getImage(getCodeBase(),names.get(j));
>     }   

We need more code.  You probably need a cast to String, as it seems you
are using old style List: getImage((String)names.get(j)) or use generic
List<String>.

H.
Signature

Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org

andrewtitus@alltel.net - 02 Mar 2006 22:12 GMT
here is my code.

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();
    }

  for(int j=0; j<names.size();j++) {

      coolImage[j] = getImage(getCodeBase(),names.get(j)); // problem is
      here I tried casting to string
    }   

//System.out.println(names);

} // 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

*****************
ÏWebApplet.java:62: cannot find symbol
ÏϧÏsymbol  : method getImage(java.net.URL,java.lang.Object)
ÏϧÏlocation: class WebApplet
ÏÏ§Ï       coolImage[j] = getImage(getCodeBase(),names.get(j));
Hendrik Maryns - 02 Mar 2006 22:21 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
NotDashEscaped: You need GnuPG to verify this message

andrewtitus@alltel.net uitte de volgende tekst op 03/02/2006 11:12 PM:
> here is my code.

>   String[] temp;
>   Image coolImage[];

Please don?t mix idioms.  I prefer Image[] coolImage, the intention is
clearer.

> *****************
> ÏWebApplet.java:62: cannot find symbol
> ÏϧÏsymbol  : method getImage(java.net.URL,java.lang.Object)
> ÏϧÏlocation: class WebApplet
> ÏÏ§Ï       coolImage[j] = getImage(getCodeBase(),names.get(j));

You don?t have a method getImage, and you don?t inherit one either.
Define it, or invoke it against the appropriate object.  Learn Java.

H.
Signature

Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org

klynn47@comcast.net - 02 Mar 2006 22:29 GMT
The method getImage is inherited. The problem is that the poster is not
sending the correct parameters to the method.
andrewtitus@alltel.net - 02 Mar 2006 22:37 GMT
Isnt the getImage() set up in the import java.awt.Image; import header. i
used it this way before in a program and it worked. I have this program
working with pictures hard coded into string array and the call with
getImage() worked. If all I did was switch to an ArrayList I should be able
to access indexs of array the same way butfor some reason not letting me.

Thank You,
Andy
andrewtitus@alltel.net - 02 Mar 2006 22:14 GMT
If I cast to string

coolImage[j] = getImage(getCodeBase(),(String)names.get(j));

I get:

java.lang.NullPointerException
ÏϧϠ   at WebApplet.init(WebApplet.java:62)
ÏϧϠ   at sun.applet.AppletPanel.run(AppletPanel.java:373)
ÏϧϠ   at java.lang.Thread.run(Thread.java:595)
ÏϧÏ
klynn47@comcast.net - 02 Mar 2006 22:28 GMT
The problem is that you never initialize the array coolImage.
andrewtitus@alltel.net - 02 Mar 2006 22:39 GMT
I did not initalize it because I do not know length of array. Should I just
give a default size?

Thank You,
Andy
klynn47@comcast.net - 02 Mar 2006 23:20 GMT
I would recommend that you use an ArrayList instead of an array to hold
the images.
andrewtitus@alltel.net - 02 Mar 2006 23:27 GMT
Can I make an ArrayList of Images? I did not see a constructor for it in
that class.

Andy
klynn47@comcast.net - 02 Mar 2006 23:56 GMT
Well you're not exactly making an ArrayList of Images. An ArrayList is
a structure which holds object references and grows dynamically. If you
create the ArrayList like this

ArrayList<Image> images = new ArrayList<Image>();

then you can iterate through it like this

for (Image image : images)
  ...
andrewtitus@alltel.net - 03 Mar 2006 00:05 GMT
If I iterate through with the for loop to draw image to screen wont it keep
over writing the previous image till end of array. Instead of waiting for
user to press button to iterate?

Andy
andrewtitus@alltel.net - 03 Mar 2006 00:21 GMT
The first picture seems to load then disappears and background image turns
gray. I have program working on my site but picture names are hardcoded in
applet. This one reads in the names of pictures from text file and then does
same as other. Not sure why so much problem getting it to display properly.

Andy
klynn47@comcast.net - 03 Mar 2006 00:30 GMT
Well the iteration was just to show how you can access the Images. You
can include some logic in your code to display them.

You might also take a look at MediaTracker. It will block the applet
until a set of images have loaded.
andrewtitus@alltel.net - 02 Mar 2006 23:23 GMT
I set up the Image[] coolImage array and it compiles. Now I got a
nullpointer error when I run applet. I ran a stack trace on it and the
values from the file are being read in and all is good till that point.I
think it has something to do with the
page.drawImage(coolImage[i],0,30,this);

Iam sorry for all the questions but Iam really trying to learn java. I want
to thank you again for your time and patience.

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
{
 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()];
//    System.out.println(names.size());
  for(int j=0; j<names.size();j++) {

      coolImage[j] = getImage(getCodeBase(),(String)names.get(j));
    }   

//System.out.println(names);

} // 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()){
        i = names.size();
         repaint();
        }else
         i++;
         repaint();
    }
    if(event.getSource()==previous) {
      if(i==names.indexOf(0)){
       
         repaint();
        }else
         i--;
         repaint();
    }

    }
   
}   

} //end webApplet


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.