java移动图形的问题
这是一个可以控制移动的圆!键盘按左就往左移动一下,右就右移。。~~~importjava.awt.*;importjava.awt.event.*;publicclass...
这是一个可以控制移动的圆!键盘按左就往左移动一下,右就右移。。~~~
import java.awt.*;
import java.awt.event.*;
public class work
{
public static void main(String[] args)
{
TFrame t = new TFrame("TestKeyEvent");
}
}
class TFrame extends Frame
{
static int x=50;
static int y=50;
TFrame(String s)
{
super(s);
setLayout(null);
setBounds(400,500,200,200);
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP)
{
{
y=y-5;
}
}
else if(keycode == KeyEvent.VK_DOWN)
{
y=y+5;
}
else if(keycode == KeyEvent.VK_LEFT)
{
x=x-5;
}
else if(keycode == KeyEvent.VK_RIGHT)
{
x=x+5;
}
repaint();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.fillOval(x,y,30,30);
}
}
现在想改进一下,要求改成按左就一直往左移动,而且是能看到在移动,其他方向也是,希望各位高手指点一下啊,我想了很久,结果做出来是一下就移开了~~~~
想问下那位给出程序的朋友,你那个程序要导入text包是吧? 展开
import java.awt.*;
import java.awt.event.*;
public class work
{
public static void main(String[] args)
{
TFrame t = new TFrame("TestKeyEvent");
}
}
class TFrame extends Frame
{
static int x=50;
static int y=50;
TFrame(String s)
{
super(s);
setLayout(null);
setBounds(400,500,200,200);
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP)
{
{
y=y-5;
}
}
else if(keycode == KeyEvent.VK_DOWN)
{
y=y+5;
}
else if(keycode == KeyEvent.VK_LEFT)
{
x=x-5;
}
else if(keycode == KeyEvent.VK_RIGHT)
{
x=x+5;
}
repaint();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.fillOval(x,y,30,30);
}
}
现在想改进一下,要求改成按左就一直往左移动,而且是能看到在移动,其他方向也是,希望各位高手指点一下啊,我想了很久,结果做出来是一下就移开了~~~~
想问下那位给出程序的朋友,你那个程序要导入text包是吧? 展开
5个回答
展开全部
package test;
import java.awt.*;
import java.awt.event.*;
public class Work {
public static void main(String[] args) {
TFrame t = new TFrame("TestKeyEvent");
}
}
class TFrame extends Frame {
static int x = 50;
static int y = 50;
enum Direction {
L, U, R, D, STOP
};
Direction dir = Direction.STOP;
TFrame(String s) {
super(s);
setLayout(null);
setBounds(400, 500, 200, 200);
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
if (keycode == KeyEvent.VK_UP) {
{
dir = Direction.U;
}
} else if (keycode == KeyEvent.VK_DOWN) {
dir = Direction.D;
} else if (keycode == KeyEvent.VK_LEFT) {
dir = Direction.L;
} else if (keycode == KeyEvent.VK_RIGHT) {
dir = Direction.R;
}
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
}
public void paint(Graphics g) {
repaint();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(dir){
case U:
if(y>30)
y-=5;
break;
case D:
if(y<200-35)
y+=5;
break;
case L:
if(x>5)
x-=5;
break;
case R:
if(x<200-35)
x+=5;
break;
}
}
}
import java.awt.*;
import java.awt.event.*;
public class Work {
public static void main(String[] args) {
TFrame t = new TFrame("TestKeyEvent");
}
}
class TFrame extends Frame {
static int x = 50;
static int y = 50;
enum Direction {
L, U, R, D, STOP
};
Direction dir = Direction.STOP;
TFrame(String s) {
super(s);
setLayout(null);
setBounds(400, 500, 200, 200);
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int keycode = e.getKeyCode();
if (keycode == KeyEvent.VK_UP) {
{
dir = Direction.U;
}
} else if (keycode == KeyEvent.VK_DOWN) {
dir = Direction.D;
} else if (keycode == KeyEvent.VK_LEFT) {
dir = Direction.L;
} else if (keycode == KeyEvent.VK_RIGHT) {
dir = Direction.R;
}
}
});
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
}
public void paint(Graphics g) {
repaint();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(dir){
case U:
if(y>30)
y-=5;
break;
case D:
if(y<200-35)
y+=5;
break;
case L:
if(x>5)
x-=5;
break;
case R:
if(x<200-35)
x+=5;
break;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
仰慕LS
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
吞吃蛇,是否可以帮上你。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
根据你的程序我加了一个线程
import java.awt.*;
import java.awt.event.*;
public class work
{
public static void main(String[] args)
{
TFrame t = new TFrame("TestKeyEvent");
}
}
class TFrame extends Frame
{
static int x=50;
static int y=50;
private Run run=new Run();
private Thread t=new Thread(run);
TFrame(String s)
{
super(s);
setLayout(null);
setBounds(400,500,200,200);
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP)
{
{
//y=y-5;
run.setLocation(KeyEvent.VK_UP);
t.start();
}
}
else if(keycode == KeyEvent.VK_DOWN)
{
//y=y+5;
run.setLocation(KeyEvent.VK_DOWN);
t.start();
}
else if(keycode == KeyEvent.VK_LEFT)
{
//x=x-5;
run.setLocation(KeyEvent.VK_LEFT);
t.start();
}
else if(keycode == KeyEvent.VK_RIGHT)
{
//x=x+5;
run.setLocation(KeyEvent.VK_RIGHT);
t.start();
}
repaint();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.fillOval(x,y,30,30);
}
private class Run implements Runnable
{
private int location;
public void setLocation(int location)
{
this.location=location;
}
public void run()
{
while(true)
{
switch(location)
{
case KeyEvent.VK_UP:
y=y-5;
TFrame.this.repaint();
break;
case KeyEvent.VK_DOWN:
y=y+5;
TFrame.this.repaint();
break;
case KeyEvent.VK_LEFT:
x=x-5;
TFrame.this.repaint();
break;
case KeyEvent.VK_RIGHT:
x=x+5;
TFrame.this.repaint();
break;
}
try{
Thread.sleep(100);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
import java.awt.*;
import java.awt.event.*;
public class work
{
public static void main(String[] args)
{
TFrame t = new TFrame("TestKeyEvent");
}
}
class TFrame extends Frame
{
static int x=50;
static int y=50;
private Run run=new Run();
private Thread t=new Thread(run);
TFrame(String s)
{
super(s);
setLayout(null);
setBounds(400,500,200,200);
addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int keycode = e.getKeyCode();
if(keycode == KeyEvent.VK_UP)
{
{
//y=y-5;
run.setLocation(KeyEvent.VK_UP);
t.start();
}
}
else if(keycode == KeyEvent.VK_DOWN)
{
//y=y+5;
run.setLocation(KeyEvent.VK_DOWN);
t.start();
}
else if(keycode == KeyEvent.VK_LEFT)
{
//x=x-5;
run.setLocation(KeyEvent.VK_LEFT);
t.start();
}
else if(keycode == KeyEvent.VK_RIGHT)
{
//x=x+5;
run.setLocation(KeyEvent.VK_RIGHT);
t.start();
}
repaint();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.fillOval(x,y,30,30);
}
private class Run implements Runnable
{
private int location;
public void setLocation(int location)
{
this.location=location;
}
public void run()
{
while(true)
{
switch(location)
{
case KeyEvent.VK_UP:
y=y-5;
TFrame.this.repaint();
break;
case KeyEvent.VK_DOWN:
y=y+5;
TFrame.this.repaint();
break;
case KeyEvent.VK_LEFT:
x=x-5;
TFrame.this.repaint();
break;
case KeyEvent.VK_RIGHT:
x=x+5;
TFrame.this.repaint();
break;
}
try{
Thread.sleep(100);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询