Java applet - MouseMotionListener在eclipse中工作,但在浏览器上不工作

时间:2014-07-18 21:10:41

标签: java applet awt mouseevent mouselistener

我的java applet有些问题...... MouseMotionListener在eclipse applet viewer中工作,但在浏览器上不工作(我试过Chrome和Firefox和IE)。在游戏中,我尝试使用鼠标移动对象。 MouseMotionListener位于initComponents方法中。

package tk.miloskostadinovski.game;

import java.applet.Applet;
import java.awt.Button;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;

import javax.swing.SwingUtilities;

public class Game extends Applet implements Runnable {

    private static final long serialVersionUID = 1L;

    // varibles
    Thread thread;
    boolean running = false;
    Random random = new Random();
    boolean pauseRunning = false;

    int width = 600, height = 700;
    int x = 350, y = 560;
    int fruitSpeed = 2;
    int points = 0;
    int lives = 5;
    int basketWH = 100;
    int fruitWH = 70;

    int aX = getRandNumbX(), aY = getRandNumbY();
    int oX = getRandNumbX(), oY = getRandNumbY();
    int sX = getRandNumbX(), sY = getRandNumbY();
    int bX = getRandNumbX(), bY = getRandNumbY();
    int hX = getRandNumbX(), hY = getRandNumbY();
    int basketX = 350;
    int basketY = 580;

    private Image i;
    private Graphics doubleG;

    Image apple, orange, banana, straw, hamburger, background, basket, heart,
            mellon;

    public void init() {
        initComponents();
        setSize(width, height);
        setLayout(null);

        apple = getImage(getDocumentBase(), "pictures/apple.png");
        orange = getImage(getDocumentBase(), "pictures/orange.png");
        banana = getImage(getDocumentBase(), "pictures/banana.png");
        straw = getImage(getDocumentBase(), "pictures/straw.gif");
        hamburger = getImage(getDocumentBase(), "pictures/hamburger.png");
        background = getImage(getDocumentBase(), "pictures/background.jpg");
        basket = getImage(getDocumentBase(), "pictures/basket.png");
        heart = getImage(getDocumentBase(), "pictures/heart.png");
        mellon = getImage(getDocumentBase(), "pictures/mellon.png");
    }

    public void start() {

    }

    public void stop() {
        running = false;
        if (thread != null) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void destroy() {

    }

    public void run() {
        pauseButton.setVisible(true);
        while (running) {

            while (pauseRunning) {
                repaint();
            }

            fruitMoving();
            repaint();

            if (aY >= height) {
                aY = getRandNumbY();
                aX = getRandNumbX();
                lives--;
            }
            if (oY >= height) {
                oY = getRandNumbY();
                oX = getRandNumbX();
                lives--;
            }
            if (bY >= height) {
                bY = getRandNumbY();
                bX = getRandNumbX();
                lives--;
            }
            if (sY >= height) {
                sY = getRandNumbY();
                sX = getRandNumbX();
                lives--;
            }
            if (hY >= height) {
                hY = getRandNumbY();
                hX = getRandNumbX();
            }

            if (hY <= basketY + basketWH && hY >= basketY && hX >= basketX - 40
                    && hX <= basketX + basketWH) {
                lives--;
                hY = getRandNumbY();
                hX = getRandNumbX();
            }

            if (sY <= basketY + basketWH && sY >= basketY && sX >= basketX - 40
                    && sX <= basketX + basketWH) {
                points++;
                sY = getRandNumbY();
                sX = getRandNumbX();
            }

            if (bY <= basketY + basketWH && bY >= basketY && bX >= basketX - 40
                    && bX <= basketX + basketWH) {
                points++;
                bY = getRandNumbY();
                bX = getRandNumbX();
            }

            if (oY <= basketY + basketWH && oY >= basketY && oX >= basketX - 40
                    && oX <= basketX + basketWH) {
                points++;
                oY = getRandNumbY();
                oX = getRandNumbX();
            }

            if (aY <= basketY + basketWH && aY >= basketY && aX >= basketX - 40
                    && aX <= basketX + basketWH) {
                points++;
                aY = getRandNumbY();
                aX = getRandNumbX();
            }

            if (basketX >= width - basketWH) {
                basketX = width - basketWH;
            }
            if (basketX <= 0) {
                basketX = 0;
            }
            if (lives <= 0) {

                tryAgain.setVisible(true);
                while (true) {
                    if (tryAgain.isVisible() == false) {
                        aY = getRandNumbY();
                        aX = getRandNumbX();
                        oY = getRandNumbY();
                        oX = getRandNumbX();
                        sY = getRandNumbY();
                        sX = getRandNumbX();
                        bY = getRandNumbY();
                        bX = getRandNumbX();
                        hY = getRandNumbY();
                        hX = getRandNumbX();

                        lives = 5;
                        points = 0;

                        break;
                    }
                }

            }

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paintComponents(g);
        g.drawImage(background, 0, 0, this);
        g.drawImage(apple, aX, aY, this);
        g.drawImage(orange, oX, oY, this);
        g.drawImage(banana, bX, bY, this);
        g.drawImage(straw, sX, sY, this);
        g.drawImage(hamburger, hX, hY, this);
        g.drawImage(basket, basketX, basketY, this);
        g.drawImage(heart, 100, 10, this);
        g.drawImage(mellon, 20, 10, this);
        g.setFont(new Font("Seris", Font.PLAIN, 20));
        g.drawString(lives + "", 125, 30);
        g.drawString(points + "", 50, 30);
        if (lives <= 0) {
            g.setFont(new Font("Seris", Font.PLAIN, 30));
            g.drawString("Game over", 220, 300);
        }
        if (pauseRunning) {
            g.drawString("Game Paused", 220, 300);
        }
    }

    @Override
    public void update(Graphics g) {
        if (i == null) {
            i = createImage(this.getSize().width, this.getSize().height);
            doubleG = i.getGraphics();
        }
        doubleG.setColor(getBackground());
        doubleG.fillRect(0, 0, width, height);

        doubleG.setColor(getForeground());
        paint(doubleG);

        g.drawImage(i, 0, 0, this);
    }

    public void initComponents() {
        // startGame button, pointsBox, livesBox, pause button, game over, play
        // again, resume game

        setFocusable(true);

        addMouseMotionListener(new MouseAdapter() {

            @Override
            public void mouseMoved(MouseEvent evt) {
                formMouseMoved(evt);

            }

        });

        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent evt) {
                formKeyPressed(evt);
            }

        });

        startGame = new Button("Start Game");
        startGame.setFocusable(false);
        startGame.setLocation(230, 300);
        startGame.setSize(130, 40);
        startGame.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startGameActionPerformed(e);
            }
        });

        tryAgain = new Button("Try Again?");
        tryAgain.setFocusable(false);
        tryAgain.setLocation(230, 350);
        tryAgain.setSize(130, 40);
        tryAgain.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tryAgainActionPerformed(e);
            }
        });
        tryAgain.setVisible(false);

        pauseButton = new Button("Pause");
        pauseButton.setFocusable(false);
        pauseButton.setLocation(550, 10);
        pauseButton.setSize(40, 25);
        pauseButton.setVisible(false);
        pauseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                pauseButtonActionPerformed(e);
            }
        });

        add(startGame);
        add(tryAgain);
        add(pauseButton);
    }

    public void startGameActionPerformed(ActionEvent e) {
        running = true;
        thread = new Thread(this);
        thread.start();
        startGame.setVisible(false);
    }

    public void tryAgainActionPerformed(ActionEvent e) {
        points = 0;
        lives = 5;
        tryAgain.setVisible(false);

    }

    public void pauseButtonActionPerformed(ActionEvent e) {
        if (pauseRunning == false) {
            pauseRunning = true;

        } else {
            pauseRunning = false;
        }

    }

    public int getRandNumbX() {
        return random.nextInt(520) + 10;
    }

    public int getRandNumbY() {
        return (random.nextInt(+600) + 100) * -1;
    }

    public void fruitMoving() {
        aY += fruitSpeed;
        oY += fruitSpeed;
        sY += fruitSpeed;
        bY += fruitSpeed;
        hY += fruitSpeed;
    }

    public void formMouseMoved(MouseEvent evt) {
        PointerInfo a = MouseInfo.getPointerInfo();
        Point point = new Point(a.getLocation());
        SwingUtilities.convertPointFromScreen(point, evt.getComponent());
        basketX = (int) point.getX() - 50;
        repaint();
    }

    public void formKeyPressed(KeyEvent evt) {
        if (pauseRunning == false) {
            if (evt.getKeyCode() == KeyEvent.VK_RIGHT)
                basketX += 20;
            if (evt.getKeyCode() == KeyEvent.VK_LEFT)
                basketX -= 20;
        }
    }

    // Component declaration
    Button startGame, tryAgain, pauseButton;
}

0 个答案:

没有答案