0

I was trying to make a circle and displaying that on applet window. But after running the code it neither creates any window nor displays the Circle. My code doesn't show any error. Where is the error?

package webgame;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;

public class StartingPoint extends Applet implements Runnable {

    int x = 0;
    int y = 0;
    int dx = 2;
    int dy = 2;
    int radius = 10;

    @Override
    public void init() {

    }

    @Override
    public void start() {
        Thread thread = new Thread(this);
        thread.start();

    }

    @Override
    public void run() {
        while (true) {
            repaint();
            try {
                Thread.sleep(17);
            } catch (InterruptedException e) {
                //Logger.getLogger(StartingPoint.class.getName()).log(Level.SEVERE, null, e);
                e.printStackTrace();
            }
        }

        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void stop() {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void paint(Graphics g) {

        g.setColor(Color.CYAN);
        g.fillOval(x, y, radius, radius);

    }

    public static void main(String[] args) {

        // TODO code application logic here
    }

}
  • 1) Why code an applet? If it is due to the teacher specifying it, 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 use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Sep 20 '14 at 02:34

1 Answers1

1

You don't need main method to execute applet and you have to Create following html file after compiling your class.

<HTML>
<HEAD></HEAD>
 <BODY>
   <div>
     <APPLET CODE="Main.class" WIDTH="500" HEIGHT="500">
     </APPLET>
   </div>
 </BODY>
</HTML>

And run like this

>appletviewer Main.java

Check Out this LINK

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
akash
  • 22,664
  • 11
  • 59
  • 87
  • Thanks a lot, it helps me, but thats not what i wanted. i dont want to create html file, rather I want to run the java file in netbeans with only java code. if I am wrong then how this guy is doing here: https://buckysroom.org/videos.php?cat=84&video=19761 without HTML code? – SomeOneSomewhere Sep 19 '14 at 10:52
  • *"i dont want to create html file, rather I want to run the java file in netbeans with only java code."* AFAIR Netbeans can run an applet without creating a specific HTML, but it is going to need HTML eventually so you might as well create some now, then it can be run using the Applet viewer. – Andrew Thompson Sep 20 '14 at 02:33