Solved

Java applet disappear on screen?

Posted on 1997-04-06
4
789 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
  • 3
4 Comments
 

Author Comment

by:laze
Comment Utility
Edited text of question
0
 
LVL 2

Accepted Solution

by:
majkl earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now