简体   繁体   中英

Java Applet isn't displaying circle

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
    }

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM