-2

this program is supposted to write an String in the Applet than String is supposted to go up in the applet untill it reaches top of it. Im getting error at

try {
    Thread.currentThread().sleep(20);
} 

The static method Sleep void schould be run as acessed the static way.

Any ideas?

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Zad2b extends Applet implements Runnable {

   napis n;
   Thread t;

   public void init() {
      Dimension d = getSize();
      Color c = new Color((float) Math.random(), (float) Math.random(),
            (float) Math.random());
      n = new napis(getParameter("napis"), (d.getWidth() / 2) - 30,
            d.getHeight(), c);
      t = new Thread(this);
      t.start();
      n.start();
   }

   public void paint(Graphics g) {
      n.rysuj(g);
   }

   public void run() {
      while (true) {
         repaint();
         try {
            Thread.currentThread().sleep(20);
         } catch (InterruptedException e) {
         }
      }
   }

   class napis extends Thread {
      double x, y;
      String s;
      Color c;

      public napis(String s, double x, double y, Color c) {
         this.x = x;
         this.y = y;
         this.s = s;
         this.c = c;
      }

      public void rysuj(Graphics g) {
         g.setColor(c);
         g.drawString(s, (int) x, (int) y);
      }

      public void run() {
         while (y > 0) {
            y -= 1.0;
            try {
               Thread.currentThread().sleep(5);
            } catch (InterruptedException e) {
            }
         }

      }
   }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Please improve the question. It's really hard to understand. – elbuild Jan 10 '14 at 13:57
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Jan 10 '14 at 18:15
  • BTW - Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Jan 10 '14 at 18:15

2 Answers2

1

You must access it through the type not the instance. Use Thread.sleep(20) instead.

Ceiling Gecko
  • 3,104
  • 2
  • 24
  • 33
1

The static method Sleep void should be run as accessed the static way.

This is not an error, only a warning. It means that you should invoke the sleep() method statically, since sleep is a static method of Thread. Invoke it like this:

Thread.sleep(20);

The reason is that static methods can not be overridden in a sub class (they are tied to the class, not to the instance), but methods with the same name can still be defined in a sub class:

class C1 {
   public static void a() {}
}

class C2 extends C1 {
   public static void a() {}
}

So, when using instance invocation, it does not directly become clear which method is invoked:

...
C1 obj = new C2();
obj.a();            // which a() is invoked?? (it is C1.a(), since static methods can not be overridden)
...

When using the static way of invoking the method, it becomes clear that it is C1.a():

C1.a();
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123