Вот мой кодМой MouseMotionListener останавливает мой KeyDown от работы
Image duelSGX;
Image selectionBG;
Image Ultor;
Image serin;
int picWidth = 500;
int picHeight = 500;
int[] duelSGXStats = {4424, 1067, 2000};
int[] UltorStats = {4950, 2415, 1605};
int[] serinStats = {5993, 1647, 1527};
int appletsize_x = 300;
int appletsize_y = 200;
int x_pos = appletsize_x/2; // x - Position of ball
int y_pos = appletsize_y/2; // y - Position of ball
int radius = 20; // Radius of ball
int x_coordinate = 500;
int y_coordinate;
private Image dbImage;
private Graphics dbg;
boolean isButtonPressed = false;
public void init()
{
resize (1900, 960);
selectionBG = getToolkit().getImage (PICTURE_PATH1);
duelSGX = getToolkit().getImage (PICTURE_PATH);
Ultor = getToolkit().getImage (PICTURE_PATH2);
serin = getToolkit().getImage (PICTURE_PATH3);
addMouseListener (this);
addMouseMotionListener (this);
}
public void stop()
{
}
public void destroy()
{
}
public void run()
{
}
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}
// clear screen in background
dbg.setColor (getBackground());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
public boolean keyDown (Event e, int key)
{
Thread.currentThread().setPriority (Thread.MIN_PRIORITY);
if (key == Event.LEFT)
{
if (x_coordinate >= (-877 - 877))
{
x_coordinate -= 100;
}
}
else if (key == Event.RIGHT)
{
if (x_coordinate <= 1900 - (677 + 200))
{
x_coordinate += 100;
}
}
// DON'T FORGET (although it has no meaning here)
repaint();
Thread.currentThread().setPriority (Thread.MAX_PRIORITY);
return true;
}
public void mouseEntered (MouseEvent e)
{
// called when the pointer enters the applet's rectangular area
}
public void mouseExited (MouseEvent e)
{
// called when the pointer leaves the applet's rectangular area
}
public void mouseClicked (MouseEvent e)
{
// called after a press and release of a mouse button
// with no motion in between
// (If the user presses, drags, and then releases, there will be
// no click event generated.)
}
public void mousePressed (MouseEvent e)
{
;
}
public void mouseReleased (MouseEvent e)
{
}
public void mouseMoved (MouseEvent e)
{
}
public void mouseDragged (MouseEvent e)
{ // called during motion with buttons down
}
public void paint (Graphics g)
{
g.drawImage (selectionBG, 0, 0, 1960, 960, null);
g.drawImage (duelSGX, x_coordinate, y_coordinate, 677, 960, null);
g.drawImage (Ultor, x_coordinate + 877, y_coordinate, 677, 960, null);
g.drawImage (serin, x_coordinate + 877 + 877, y_coordinate, 677, 960, null);
}
Проблема заключается в том, что KeyDown перестает работать, когда я добавить MouseMotionListener и MouseListener в методе инициализации. Когда я удаляю их, это работает ... Что здесь происходит? Помоги пожалуйста?
Когда/где вы добавляете 'KeyListener'? 'Applet' как 15 лет устарел, подумайте об использовании компонента JApplet и Swing (например,' JComponent' 'JPanel'), так как вам не нужно будет выполнять собственную двойную буферизацию – MadProgrammer
. Я добавил mouseListener в init(). Я не добавил keyListener, не знаю, где бы я добавил это, и я не знаю, как использовать эти новые апплеты ... –
@MadProgrammer OP не использует 'KeyListener' (хотя он/она должен be) - используется метод 'keyDown' класса' Component'. – TNT