在ai里面,用键盘方向键移动东西时,每按一次移动我想要的数值,应该怎么设置?说详细点,初学ai。

如题所述

第1个回答  2011-07-10
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;

public class DrawRectTest extends JFrame {
int oldx =40,x=40;
int y=30, oldy =40;
public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}

public int getY() {
return y;
}

public void setY(int y) {
this.y = y;
}

public void moveOval(){

Graphics g = this.getGraphics();
Color c = g.getColor();
g.setColor(this.getBackground());
g.fillOval(oldx, oldy, 40, 40);
g.setColor(c);
g.fillOval(x, y, 40, 40);
this.oldx = x;
this.oldy = y;
}
public DrawRectTest() {

this.setSize(500, 500);
this.setVisible(true);
addKeyListener(new A(this));
}
public void paint(Graphics g) {
super.paint(g);
int w = 0;
int h = 30;
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 10; j++) {
g.drawRect(w = w + 40, h, 40, 40);
}
w = 0;
h += 40;
}
g.fillOval(x, y, 40, 40);

}
public static void main(String[] args) {
new DrawRectTest();

}
}

class A implements KeyListener {
DrawRectTest j;
public A(DrawRectTest jp){
this.j=jp;
}

@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_DOWN:
j.setX(j.getX());
j.setY(j.getY()+40);
//j.repaint();
j.moveOval();
break;
case KeyEvent.VK_UP:
break;
case KeyEvent.VK_LEFT:
break;
case KeyEvent.VK_RIGHT:
break;

}
}

@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub

}

}
第2个回答  2011-07-09
在变换面板里,在X轴或Y轴,加上或减去你想要的数值,回车就可以了。
第3个回答  2011-07-09
右键单击你想移动的图形,选择变换〉移动,或直接按快捷键ctrl+shift+m。
第4个回答  推荐于2017-11-24
编辑 -》 首选项 -》 常规 -》 键盘增量本回答被网友采纳
相似回答