Solved

Java applet disappear on screen?

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 120
Arrays.asList  VS  ArrayList 4 71
Windows 10 IE Certificate Issue 10 51
how to install java on RHEL image on EC2 4 41
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

837 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