Java 画圆 完成圆的8个方向的移动
这是4个方向的移动,请问怎么把它改成八个方向的移动呢?importjava.awt.*;importjava.awt.event.*;publicclassTestKey...
这是4个方向的移动,请问怎么把它改成八个方向的移动呢?
import java.awt.*;
import java.awt.event.*;
publicclassTestKeyEvent
{
publicstaticvoid main(String[] args)
{
TFrame t = new TFrame("TestKeyEvent");
}
}
classTFrameextends Frame
{
staticintx=50;
staticinty=50;
TFrame(String s)
{
super(s);
setLayout(null);
setBounds(400,500,200,200);
addKeyListener(new KeyAdapter()
{
publicvoid keyPressed(KeyEvent e)
{
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP)
{
y-=10;
}
elseif(keycode == KeyEvent.VK_DOWN)
{
y+=10;
}
elseif(keycode == KeyEvent.VK_LEFT)
{
x-=10;
}
elseif(keycode == KeyEvent.VK_RIGHT)
{
x+=10;
}
repaint();
}
});
addWindowListener(new WindowAdapter()
{
publicvoid windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
publicvoid paint(Graphics g)
{
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x,y,30,30);
g.setColor(c);
}
} 展开
import java.awt.*;
import java.awt.event.*;
publicclassTestKeyEvent
{
publicstaticvoid main(String[] args)
{
TFrame t = new TFrame("TestKeyEvent");
}
}
classTFrameextends Frame
{
staticintx=50;
staticinty=50;
TFrame(String s)
{
super(s);
setLayout(null);
setBounds(400,500,200,200);
addKeyListener(new KeyAdapter()
{
publicvoid keyPressed(KeyEvent e)
{
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP)
{
y-=10;
}
elseif(keycode == KeyEvent.VK_DOWN)
{
y+=10;
}
elseif(keycode == KeyEvent.VK_LEFT)
{
x-=10;
}
elseif(keycode == KeyEvent.VK_RIGHT)
{
x+=10;
}
repaint();
}
});
addWindowListener(new WindowAdapter()
{
publicvoid windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
publicvoid paint(Graphics g)
{
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x,y,30,30);
g.setColor(c);
}
} 展开
展开全部
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
if (keycode == KeyEvent.VK_UP || keycode == KeyEvent.VK_NUMPAD8) {//向上
y -= 10;
} else if (keycode == KeyEvent.VK_DOWN
|| keycode == KeyEvent.VK_NUMPAD2) {//向下
y += 10;
} else if (keycode == KeyEvent.VK_LEFT
|| keycode == KeyEvent.VK_NUMPAD4) {//向左
x -= 10;
} else if (keycode == KeyEvent.VK_RIGHT
|| keycode == KeyEvent.VK_NUMPAD6) {//向右
x += 10;
} else if (keycode == KeyEvent.VK_NUMPAD1) {//左下
x -= 10;
y += 10;
} else if (keycode == KeyEvent.VK_NUMPAD7) {//左上
x -= 10;
y -= 10;
} else if (keycode == KeyEvent.VK_NUMPAD9) {//右上
x += 10;
y -= 10;
} else if (keycode == KeyEvent.VK_NUMPAD3) {//右下
x += 10;
y += 10;
}
repaint();
}
});
改一下这个方法就OK了~~~
int keycode = e.getKeyCode();
if (keycode == KeyEvent.VK_UP || keycode == KeyEvent.VK_NUMPAD8) {//向上
y -= 10;
} else if (keycode == KeyEvent.VK_DOWN
|| keycode == KeyEvent.VK_NUMPAD2) {//向下
y += 10;
} else if (keycode == KeyEvent.VK_LEFT
|| keycode == KeyEvent.VK_NUMPAD4) {//向左
x -= 10;
} else if (keycode == KeyEvent.VK_RIGHT
|| keycode == KeyEvent.VK_NUMPAD6) {//向右
x += 10;
} else if (keycode == KeyEvent.VK_NUMPAD1) {//左下
x -= 10;
y += 10;
} else if (keycode == KeyEvent.VK_NUMPAD7) {//左上
x -= 10;
y -= 10;
} else if (keycode == KeyEvent.VK_NUMPAD9) {//右上
x += 10;
y -= 10;
} else if (keycode == KeyEvent.VK_NUMPAD3) {//右下
x += 10;
y += 10;
}
repaint();
}
});
改一下这个方法就OK了~~~
追问
我会了
感谢!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |