关于java paint的一点问题,实在没找出答案
题目大意是:在JAVA小程序窗口中显示“java程序设计”,按下键盘上的箭头建,字符串相应移动
书翻了,也GOOGLE了,我实在不知道该如何使用paint重画实现移动效果,请指点,如果我实在写得太遭,请编出一个新程序来帮助解惑,感谢各位
因为是新手,请大家说详细些,再次感谢
我的程序:
//第一个类,绘图类
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class Tutu{
public int x,y;
Tutu(int a,int b){
x=a;
y=b;
}
public void paint(Graphics g){
g.setColor(Color.green);
g.drawString( "java程序设计 ",x,y);
}
public void repaint(Graphics r){
r.setColor(Color.green);
g.drawString( "java程序设计 ",x,y);
}
}
//第二个类,实现APPLET
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class T312 extends Applet{
Tutu tu=new Tutu(300,200);
public void init(){
addKeyListener(new type());
}
public class type extends KeyAdapter{
public void keyTyped(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_UP)
{
tu.y+=20;
}
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
tu.y-=20;
}
else if (e.getKeyCode()==KeyEvent.VK_LEFT)
{
tu.x-=20;
}
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
tu.x+=20;
}
}
}
}
------解决方案--------------------import java.applet.Applet;
import java.awt.*;
public class TempApplet extends Applet {
int x, y;
String str;
int v = 10;
public TempApplet() {
x = 100;
str = "测试 ";
y = 100;
}
public void init() {
this.resize(300, 300);
}
public void paint(Graphics g) {
g.drawString(str, x, y);
}
public boolean keyDown(Event e, int keycode) {
if (keycode == 1004)
y -= v;
else if (keycode == 1005)
y += v;
else if (keycode == 1006)
x -= v;
else if (keycode == 1007)
x += v;
repaint();
return true;
}
}
------解决方案--------------------可以在frame里面设置焦点为真