鼠标在java applet中按下并释放

时间:2014-05-01 20:43:43

标签: java applet

下面的代码是针对跳棋程序的。如果用户点击停止按钮或按住鼠标,它会停止,并且go按钮或鼠标释放会从头开始移动检查器。

我无法弄清楚如何将棋子从他们被拦截的位置移开。请帮忙!

import java.awt.Button;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Checkers extends java.applet.Applet implements Runnable,
        ActionListener {

Thread runner;
int xpos;
Image offscreenImg;
Graphics offscreenG;
Button  goButton, stopButton, exitButton;

private ExitButtonHandler ebHandler;

public void init() {
    // offscreenImg = createImage(getWidth(), getHeight());
    offscreenImg = createImage(this.size().width, this.size().height);
    offscreenG = offscreenImg.getGraphics();

    setSize(200, 250);

    addMouseListener(new Mouse());

    goButton = new Button("Go");
    goButton.setBounds(10, 200, 90, 20);
    setLayout(null);

    stopButton = new Button("Stop");
    stopButton.setBounds(100, 200, 90, 20);
    setLayout(null);

    exitButton = new Button("Exit");
    ebHandler = new ExitButtonHandler();
    exitButton.addActionListener(ebHandler);
    exitButton.setBounds(55, 220, 90, 20);
    setLayout(null);

    add(goButton);
    add(stopButton);
    add(exitButton);

    stopButton.addActionListener(this);
    goButton.addActionListener(this);
}

public void start() {
    if (runner == null) {
        runner = new Thread(this);
        runner.start();
    }
}

public void stop() {
    if (runner != null) {
        runner.stop();
        runner = null;
    }
}

public void run() {
    while (true) {
        for (xpos = 5; xpos <= 105; xpos += 4) {

            repaint();

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

        }
        xpos = 5;

        for (xpos = 105; xpos >= 5; xpos -= 4) {
            repaint();

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
        xpos = 5;
    }
}

public void update(Graphics g) {
    paint(g);
}

public void paint(Graphics g) {
    // Draw background onto the buffer area
    offscreenG.setColor(Color.blue);
    offscreenG.fillRect(0, 0, 100, 100);
    offscreenG.setColor(Color.red);
    offscreenG.fillRect(0, 100, 100, 100);
    offscreenG.setColor(Color.red);
    offscreenG.fillRect(100, 0, 100, 100);
    offscreenG.setColor(Color.blue);
    offscreenG.fillRect(100, 100, 100, 100);

    // Draw checker
    offscreenG.setColor(Color.green);
    offscreenG.fillOval(xpos, 5, 90, 90);
    offscreenG.setColor(Color.yellow);
    offscreenG.fillOval(-xpos + 110, 105, 90, 90);

    // Now, transfer the entire buffer onto the screen
    g.drawImage(offscreenImg, 0, 0, this);
}

public void destroy() {
    offscreenG.dispose();
}

// Mouse events
public class Mouse extends MouseAdapter {

    public void mousePressed(MouseEvent e) {
        stop();

    }

    public void mouseReleased(MouseEvent e) {
        start();

    }
}

// Button events
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == stopButton) {
        stop();

    } else if (e.getSource() == goButton) {

        start();
    }

}

// exit button
public class ExitButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
}

}

1 个答案:

答案 0 :(得分:1)

您正在重新创建一个新线程,每次调用start函数时,该线程从头开始。此外,在致电stop后,您将无法恢复&#34;你离开的地方。

您需要更改线程以使用某种类型的标记(boolean stopped;),然后在调用stopstart时更改该标记,这样线程就不会#39实际上结束了但是只是暂停了。&#34; (停止移动,但实际上仍在运行

if (stopped) {
    //dont do stuff
}
else {
    //do stuff
}