简体   繁体   English

Java Applet未显示圆圈

[英]Java Applet isn't displaying circle

I was trying to make a circle and displaying that on applet window. 我试图做一个圆圈,并将其显示在applet窗口上。 But after running the code it neither creates any window nor displays the Circle. 但是在运行代码后,它既不会创建任何窗口,也不会显示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. 您不需要main方法来执行applet,并且必须在编译类后创建以下html文件。

<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 查看此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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