📄 huarongdao.txt
字号:
package lab3;
import java.awt.*;
import java.awt.event.*;
class MoveExample
{
public static void main(String args[])
{
new Hua_Rong_Road();
}
}
class Person extends Button implements FocusListener
{
int number;
Color c;
Person (int number, String s)
{
super(s);
this.number = number;
setFont(new Font("宋体",Font.CENTER_BASELINE,14));
setBackground(Color.pink);
addFocusListener(this);
}
public void focusGained(FocusEvent e)
{
setBackground(Color.cyan);
}
public void focusLost(FocusEvent e)
{
setBackground(Color.pink);
}
}
class Hua_Rong_Road extends Frame implements KeyListener, MouseListener, ActionListener
{
Person person[] = new Person[10];
Button left, right, above, below;
Button restart = new Button("重新开始");
public Hua_Rong_Road()
{
init();
setBounds(100,100,320,360);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{
public void windowsClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void init()
{
setLayout(null);
add(restart);
restart.setBounds(100,22,100,25);
restart.addActionListener(this);
String name[] = {"曹操","关羽","张","刘","马","许","兵","兵","兵","兵"};
for (int i = 0; i<name.length; i++)
{
person[i] = new Person(i, name[i]);
person[i].addKeyListener(this);
person[i].addMouseListener(this);
add(person[i]);
}
person[0].setBounds(104,54,100,100);
person[1].setBounds(104,154,100,50);
person[2].setBounds(54,154,50,100);
person[3].setBounds(204,154,50,100);
person[4].setBounds(54,54,50,100);
person[5].setBounds(204,54,50,100);
person[6].setBounds(54,254,50,50);
person[7].setBounds(204,254,50,50);
person[8].setBounds(104,204,50,50);
person[9].setBounds(154,204,50,50);
person[9].requestFocusInWindow();
left = new Button();
right = new Button();
above = new Button();
below = new Button();
add(left);
add(right);
add(above);
add(below);
left.setBounds(49,49,5,260);
right.setBounds(254,49,5,260);
above.setBounds(49,49,210,5);
below.setBounds(49,304,210,5);
validate();
}
public void keyTyped(KeyEvent e)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void keyPressed(KeyEvent e)
{
Person man = (Person)e.getSource();
if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
goDown(man);
}
if (e.getKeyCode() == KeyEvent.VK_UP)
{
goUp(man);
}
if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
goLeft(man);
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
{
goRight(man);
}
}
public void keyReleased(KeyEvent e)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseClicked(MouseEvent e)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void mousePressed(MouseEvent e)
{
Person man = (Person)e.getSource();
int x = -1, y = -1;
x = e.getX();
y = e.getY();
int w = man.getBounds().width;
int h = man.getBounds().height;
if (y > h/2)
{
goDown(man);
}
if (y < h/2)
{
goUp(man);
}
if (x < w/2)
{
goLeft(man);
}
if (x > w/2)
{
goRight(man);
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void actionPerformed(ActionEvent e)
{
removeAll();
init();
validate();
repaint();
}
public void goDown(Person man)
{
boolean move = true;
Rectangle manRect = man.getBounds();
int x = man.getBounds().x;
int y = man.getBounds().y;
y = y + 50;
manRect.setLocation(x, y);
Rectangle belowRect = below.getBounds();
for (int i = 0;i < 10; i++) {
Rectangle personRect = person[i].getBounds();
if ((manRect.intersects(personRect))&& (man.number != i)) {
move = false;
}
}
if (manRect.intersects(belowRect)) {
move = false;
}
if (move == true) {
man.setLocation(x, y);
}
}
public void goUp(Person man) {
boolean move = true;
Rectangle manRect = man.getBounds();
int x = man.getBounds().x;
int y = man.getBounds().y;
y = y - 50;
manRect.setLocation(x, y);
Rectangle aboveRect = above.getBounds();
for (int i = 0;i < 10; i++) {
Rectangle personRect = person[i].getBounds();
if ((manRect.intersects(personRect))&& (man.number != i)) {
move = false;
}
}
if (manRect.intersects(aboveRect)) {
move = false;
}
if (move == true) {
man.setLocation(x, y);
}
}
public void goLeft(Person man) {
boolean move = true;
Rectangle manRect = man.getBounds();
int x = man.getBounds().x;
int y = man.getBounds().y;
x = x - 50;
manRect.setLocation(x, y);
Rectangle leftRect = left.getBounds();
for (int i = 0;i < 10; i++) {
Rectangle personRect = person[i].getBounds();
if ((manRect.intersects(personRect))&& (man.number != i)) {
move = false;
}
}
if (manRect.intersects(leftRect)) {
move = false;
}
if (move == true) {
man.setLocation(x, y);
}
}
public void goRight(Person man) {
boolean move = true;
Rectangle manRect = man.getBounds();
int x = man.getBounds().x;
int y = man.getBounds().y;
x = x + 50;
manRect.setLocation(x, y);
Rectangle rightRect = right.getBounds();
for (int i = 0;i < 10; i++) {
Rectangle personRect = person[i].getBounds();
if ((manRect.intersects(personRect))&& (man.number != i)) {
move = false;
}
}
if (manRect.intersects(rightRect)) {
move = false;
}
if (move == true) {
man.setLocation(x, y);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -