Java 只绘制一次小程序

Java 只绘制一次小程序,java,applet,awt,paint,mouselistener,Java,Applet,Awt,Paint,Mouselistener,我正在处理一个游戏的主菜单,每当小程序开始运行时,它会在启动程序之前绘制小程序4次,然后它会继续重新绘制,我在程序中添加了纹理,所以我不知道问题出在哪里 public class MainMenu extends Applet implements MouseListener, Runnable { int xpos; int ypos; private Image menu = null; private Image play = null; pri

我正在处理一个游戏的主菜单,每当小程序开始运行时,它会在启动程序之前绘制小程序4次,然后它会继续重新绘制,我在程序中添加了纹理,所以我不知道问题出在哪里

public class MainMenu extends Applet implements MouseListener, Runnable {

    int xpos;
    int ypos;

    private Image menu = null;
    private Image play = null;
    private Image exit = null;
    int rect1xco, rect1yco, rect1width, rect1height;
    int rect2xco, rect2yco, rect2width, rect2height;

    boolean mouseEntered;    
    boolean rect1Clicked;
    boolean rect2Clicked;

    public void init() {    
        rect1xco = 590;
        rect1yco = 300;
        rect1width = 150;
        rect1height = 70;    
        rect2xco = 590;
        rect2yco = 400;
        rect2width = 150;
        rect2height = 70;    
        addMouseListener(this);
    }

public void paint(Graphics g) {
        this.setSize(1350, 650);    
        if (menu == null)
            menu = getImage("menu.JPG");
        if (play == null)
            play = getImage("Button.png");
        if (exit == null)
            exit = getImage("Button.png");    
        g.drawImage(menu, 0, 0, getWidth(), getHeight(), this);
        g.drawImage(play, 590, 300, 150, 70, this);
        g.drawImage(exit, 590, 400, 150, 70, this);    
        Font Blockt = new Font("INFECTED", Font.ITALIC, 200);
        g.setFont(Blockt);
        g.setColor(Color.yellow);
        g.drawString("DX BALL", 360, 200);    
        g.getFontMetrics();
        Font DIGITAL = new Font("Blockt", Font.ITALIC, 30);
        g.setFont(DIGITAL);
        g.setColor(Color.black);
        g.drawString("Play", 635, 340);
        g.drawString("Exit", 635, 440);

        if (rect1Clicked) {

        }

        if (rect2Clicked) {
            System.exit(1);
        }    
    }
    public void mousePressed(MouseEvent me) {
        if (rect1Clicked == true) {
            repaint();
            play = getImage("ButtonClicked.png");    
        }
        if (rect2Clicked == true) {
            repaint();
            exit = getImage("ButtonClicked.png");
        }
    }

    public void mouseClicked(MouseEvent me) {    
        xpos = me.getX();
        ypos = me.getY();
        rect1Clicked = false;
        if (xpos > rect1xco && xpos < rect1xco + rect1width && ypos > rect1yco
                && ypos < rect1yco + rect1height) {
            rect1Clicked = true;    
            System.out.println("Start button pressed"); // start the game    
        } else
            rect1Clicked = false;
        if (xpos > rect2xco && xpos < rect2xco + rect1width && ypos > rect2yco
                && ypos < rect2yco + rect2height)
            rect2Clicked = true;    
        else
            rect2Clicked = false;    
    }

    public void mouseReleased(MouseEvent me) {    
        play = getImage("Button.png");
        exit = getImage("Button.png");
        repaint();    
    }

    public void mouseEntered(MouseEvent me) {    
        mouseEntered = true;    
    }

    public void mouseExited(MouseEvent me) {    
        mouseEntered = false;    
    }
public类主菜单扩展小程序实现鼠标侦听器,可运行{
int XPO;
int ypos;
私有图像菜单=空;
私有图像播放=空;
私有映像退出=null;
int rect1xco、rect1yco、rect1width、rect1height;
int rect2xco、rect2yco、rect2width、rect2height;
布尔鼠标;
布尔矩形1单击;
布尔rect2Clicked;
public void init(){
rect1xco=590;
rect1yco=300;
矩形宽度=150;
rect1高度=70;
rect2xco=590;
rect2yco=400;
矩形宽度=150;
rect2height=70;
addMouseListener(这个);
}
公共空间涂料(图g){
这个.设置大小(1350650);
如果(菜单==null)
menu=getImage(“menu.JPG”);
如果(播放==null)
play=getImage(“Button.png”);
if(exit==null)
exit=getImage(“Button.png”);
g、 drawImage(菜单,0,0,getWidth(),getHeight(),this);
g、 drawImage(play,590、300、150、70,本);
g、 drawImage(出口,590、400、150、70,本);
Font Blockt=新字体(“已感染”,Font.ITALIC,200);
g、 setFont(Blockt);
g、 setColor(颜色为黄色);
g、 抽绳(“DX球”,360200);
g、 getFontMetrics();
数字字体=新字体(“Blockt”,Font.ITALIC,30);
g、 setFont(数字);
g、 设置颜色(颜色为黑色);
g、 抽绳(“演奏”,635340);
g、 抽绳(“出口”,635440);
如果(单击rect1){
}
if(rect2Clicked){
系统出口(1);
}    
}
public void mousePressed(MouseEvent me){
if(rect1Clicked==true){
重新油漆();
play=getImage(“ButtonClicked.png”);
}
if(rect2Clicked==true){
重新油漆();
exit=getImage(“ButtonClicked.png”);
}
}
公共无效mouseClicked(MouseEvent me){
xpos=me.getX();
ypos=me.getY();
rect1Clicked=false;
如果(xpos>rect1xco&&xposrect1yco
&&yposrect2xco&&xposrect2yco
&&ypos
永远不要在
paint()
方法中设置大小、加载图像或创建字体!这些都应该在构建类并随后缓存时完成。(实际上,设置小程序大小的调用不应该完成*,因为这是一个从加载它的HTML获取大小的小程序。1)为什么要编写小程序?如果是因为老师指定了它,请参考他们。2)为什么要使用AWT?有关放弃使用组件的AWT以支持Swing的许多好理由,请参阅。哦,,,没有小程序可以这样做。
If(rect2Clicked){System.exit(1);
。但是,由于鼠标事件退出的应用程序(?)几乎不会意外结束,因此提供给exit方法的值似乎是错误的。