Solved

Java applet disappear on screen?

Posted on 1997-04-06
4
793 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 50 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 91
SequenceInputStream example 3 38
Html Table Looping (part 2) 5 41
Coding for the first time 9 101
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 …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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

738 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