?
Solved

Java applet disappear on screen?

Posted on 1997-04-06
4
Medium Priority
?
795 Views
Last Modified: 2013-11-23
I have a Java applet that takes <params> from an HTML document (which represent announcements with date, title, short description) and print them in a window that simple scrolls through them every ten seconds.  My problem is that if you go to another window that covers the applet, when you switch back, it's blank until it rotates to the next announcement.  And sometimes, it doesn't display anything (for seemingly no reason) and I have to shut down Communicator and restart it to see the applet in working mode.  I have a feeling it's based on my paint() function... I'm drawing the text, then drawing over it in the background color before drawing the new text.

Following is the code -- any ideas on how I could fix this?  Thanks...

import java.awt.*;
import java.applet.Applet;

public class stuff4 extends Applet
   {
      String Message[],DateTime[],Details[];
      int totalimages;
      boolean DrawOrg;
      Font drawit=new Font("TimesRoman", Font.ITALIC, 20);
      Font smallit=new Font("TimesRoman", Font.BOLD, 15);
      Font smallerit=new Font("TimesRoman", Font.PLAIN, 13);

      public void init()
      {
         setBackground (Color.white);
         processHTMLParms();
      }

      public void processHTMLParms()
      {
         String parm1;
         parm1=getParameter("totalimages");
         totalimages=(parm1==null?50: Integer.parseInt(parm1));
         Message = new String[totalimages];
         DateTime = new String[totalimages];
         Details = new String[totalimages];
         for (int i=0; i<totalimages; i++)
         {
            Message[i]=getParameter("m"+i);
            DateTime[i]=getParameter("dt"+i);
            Details[i]=getParameter("de"+i);
         }
      }

      public void paint (Graphics g)
      {
         for (int i=0; i<totalimages; i++)
         {
            g.setColor(Color.black);
            g.setFont(drawit);
            g.drawString(DateTime[i],1,20);
            g.setColor(new Color(50,75,75));
            g.setFont(smallit);
            g.drawString(Message[i],1,35);
            g.setColor(new Color(75,100,100));
            g.setFont(smallerit);
            g.drawString(Details[i],1,50);
            try
               {
                  Thread.sleep(10000);
               }
            catch (InterruptedException e)
               {
                  showStatus(e.toString());
               }
            g.setColor(Color.white);
            g.setFont(drawit);
            g.drawString(DateTime[i],1,20);
            g.setFont(smallit);
            g.drawString(Message[i],1,35);
            g.setFont(smallerit);
            g.drawString(Details[i],1,50);

         if (i==(totalimages-1)) {i=-1;};
         }
      }
   }
0
Comment
Question by:laze
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 

Author Comment

by:laze
ID: 1219436
Edited text of question
0
 
LVL 2

Accepted Solution

by:
majkl earned 150 total points
ID: 1219437
If you take a look in documentation there is method start()
which can you override. It is called every time you visit page containing applet and it is also called when user for example press back button .
Try to put code thah draws in this start() method and read more about in :
http://java.sun.com/nav/read/Tutorial/index.html
0
 

Author Comment

by:laze
ID: 1219438
How would I move the paint code to the start code?  I tried just replacing the paint (Graphics g) with start (Graphics g)... not quite sure (in case you couldn't tell, I'm starting with doing Java and have a little trouble with the GUI methods)...

0
 

Author Comment

by:laze
ID: 1219439
Haven't heard back from you -- and putting it in the start() method didn't work... here's a slightly altered version of the code:

import java.awt.*;
import java.applet.Applet;

public class stuff6 extends Applet
   {
      String Message[],DateTime[],Details[];
      int totalimages;
      int i;
      boolean DrawOrg;
      Font drawit=new Font("TimesRoman", Font.ITALIC, 20);
      Font smallit=new Font("TimesRoman", Font.BOLD, 15);
      Font smallerit=new Font("TimesRoman", Font.PLAIN, 13);

      public void init()
      {
         setBackground (Color.white);
         processHTMLParms();
         i=-1;
      }

      public void processHTMLParms()
      {
         String parm1;
         parm1=getParameter("totalimages");
         totalimages=(parm1==null?50: Integer.parseInt(parm1));
         Message = new String[totalimages];
         DateTime = new String[totalimages];
         Details = new String[totalimages];
         for (int i=0; i<totalimages; i++)
         {
            Message[i]=getParameter("m"+i);
            DateTime[i]=getParameter("dt"+i);
            Details[i]=getParameter("de"+i);
         }
      }

      public void paint (Graphics g)
      {
            i++;
            g.setColor(Color.black);
            g.setFont(drawit);
            g.drawString(DateTime[i],1,20);
            g.setColor(new Color(50,75,75));
            g.setFont(smallit);
            g.drawString(Message[i],1,35);
            g.setColor(new Color(75,100,100));
            g.setFont(smallerit);
            g.drawString(Details[i],1,50);
            try
               {
                  Thread.sleep(5000);
               }
            catch (InterruptedException e)
               {
                  showStatus(e.toString());
               }
         if (i==(totalimages-1)) {i=-1;};
         repaint();
      }
   }


This works a little better, but when the user scrolls down or comes back to the page or switches from antoher window, it takes times for the thread.sleep to finish before it redraws the next section...

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question