只绘制一次applet

时间:2014-12-26 12:42:01

标签: java applet awt paint mouselistener

我正在为一个游戏的主菜单工作,每当applet开始运行时,它会在启动程序之前绘制applet 4次,它会不断重新绘制它,我在程序中添加纹理,所以我不知道问题是什么< / p>

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;    
    }

0 个答案:

没有答案