This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Applets and the fly likes Scroller Applet & NS4 - getsize() method error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Scroller Applet & NS4 - getsize() method error" Watch "Scroller Applet & NS4 - getsize() method error" New topic
Author

Scroller Applet & NS4 - getsize() method error

Nicolas Tolksdorf
Greenhorn

Joined: Nov 07, 2001
Posts: 5
I have an applet that works in Internet Explorer (IE) but the getsize() method seems to throw Netscape4 (NS4).
WHAT DOES THE APPLET DO?
The applet loads a image as background and reads each line of a text file (results.txt) for both text to scroll across applet and set each line of text with the relevant hyperlink (the code uses the | character as a delimiter between the text and it's related hyperlink.
*********************************************************
NETSCAPE JAVA CONSOLE:
Thread started
# Applet exception: exception: java.lang.NullPointerException
java.lang.NullPointerException
Thread stopped
13
# Applet exception: error: java.lang.NoSuchMethodError: java.awt.Component: method getSize ()Ljava/awt/Dimension; not found java.lang.NoSuchMethodError: java.awt.Component: method getSize ()Ljava/awt/Dimension; not found
*********************************************************
APPLET SCROLLER SOURCE:
//reads a file and scrolls contents across screen with picture background
//written by peter guillebaud
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class Ticker extends Applet implements Runnable
{
private Font ft = new Font("Verdana",Font.BOLD,11);
private String text = "INSL" ;
private Thread myThread;
private Image imageBuffer = null;//double buff to stop flicker
private Image pic;
int speed = 200;
int xPos = 100;
private String filename = "results.txt";// file to read with alert info
Vector vecText = new Vector();
Vector vecUrls = new Vector();
int vecSize = 0;
int counter;
int curPos;
FontMetrics fm;
boolean mOver = false; //mouseover

public void init()
{
String param;
param = getParameter ( "bgImage" );
System.out.println ( "The parameter is " + param );
pic = getImage ( getDocumentBase(), param );
fm = getFontMetrics(ft);
readFile();
counter = 0;
}
public boolean mouseEnter(Event e, int x, int y)
{
stop();
mOver = true;
curPos = e.y;
System.out.println ( curPos );
repaint();
return true;
}
public boolean mouseExit(Event e, int x, int y)
{
start();
mOver = false;
repaint();
return true;
}
public boolean mouseDown ( Event e, int x, int y )
{
//try
//{
System.out.println ( vecUrls.elementAt ( counter ));
URL url = (URL)vecUrls.elementAt ( counter );
getAppletContext().showDocument ( url );
//}
//catch ( MalformedURLException mue)
//{
// System.out.println ( mue.toString());
//}
return true;
}
//called at init, populates vectors by reading file
public void readFile( )
{
try
{
URL fileUrl = new URL ( getDocumentBase(), filename );
try
{
BufferedReader br = new BufferedReader (new InputStreamReader ( fileUrl.openStream() ) );
String line = br.readLine();
while ( line != null )
{
StringTokenizer tokens = new StringTokenizer ( line, "|");
vecText.addElement ( tokens.nextElement());
while ( tokens.hasMoreElements())
{
vecUrls.addElement ( new URL( getDocumentBase(), tokens.nextToken()));
}
//text += line;
line = br.readLine();
}
//System.out.println ( text );
br.close();
}
catch ( FileNotFoundException fnfe )
{
System.out.println ( fnfe.toString());
}
catch ( IOException ioe )
{
System.out.println ( ioe.toString());
}
}
catch ( MalformedURLException mue )
{
System.out.println ( mue.toString());
}
//set the size of the vector
vecSize = vecText.size();
}

public void render ( Graphics g )
{
g = imageBuffer.getGraphics();
// g.setColor ( Color.black );
g.setColor ( new Color(Integer.parseInt ( "CCFFFF", 16 )));
g.fillRect(0, 0, 632, 15);
// g.fillRect(0, 0, getSize().width, getSize().height);
g.drawImage ( pic,0,0,632, 15, this );
// g.setColor ( Color.black );
g.setFont ( ft );

if ( mOver )
g.setColor ( Color.white );
//get the text line out of the vector
String lineToDisplay = (String)vecText.elementAt ( counter );
//find the length of this string
int length = fm.stringWidth ( lineToDisplay );
//if the string length is out of sight get the next string and reset the positioning
if ( length + xPos < 100 && counter < vecText.size() -1)
{
counter ++;
xPos = 632;
}
//if we've reached the end of the alerts go back to the beginning
if ( counter == vecText.size() -1)
counter = 0;

g.drawString ( lineToDisplay, xPos, 11 );
}
//called by repaint in the thread runner
public void update ( Graphics g )
{
//draws scrolling text offscreen
render( g);
//calls paint which draws offscreen stuff onscreen
paint ( g );
}

public void paint(Graphics g)
{
if (imageBuffer == null)
{ // double buffering
imageBuffer = createImage(632, 15);
// imageBuffer = createImage(getSize().width, getSize().height);
}
g.drawImage(imageBuffer, 0, 0, null);
}
public void start()
{
myThread = new Thread(this);
myThread.start();
System.out.println ( "Thread started" );
}
public void stop()
{
//myThread = null;
myThread.stop();
System.out.println ( "Thread stopped" );
}
public void run()
{
try
{
while(true)
{
repaint();
myThread.sleep(speed);
xPos--;
}
}
catch (InterruptedException e){}
}
}
*********************************************************
RESULT.TXT :
2001-11-06 text 1|http://www.netscape.com/
2001-11-06 text 2|http://www.microsoft.com/
2001-11-06 text 3|http://java.sun.com/
2001-11-06 text 4|http://applets.java.sun.com/
2001-11-06 text 5|http://www.sun.com/
2001-11-06 text 6|http://www.apache.org/
2001-11-06 text 7|http://jakarta.apache.org/
_________________________________________________
Any thoughts would be much appreciated.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Nicolas,
getSize() method for components only came into being with Java 1.1. I am not really sure, but if you use older than Netscape 4.5 it doesn't support 1.1 (only 1.0.x).
Regards,
Manfred.
 
 
subject: Scroller Applet & NS4 - getsize() method error
 
Similar Threads
VectorCanvas
Adding resizable components to JPanel
Gray box where my applet should be
The strangest thing with this program!
Applet working in appletviewer but no browser