大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
去掉TextField后的程序,这个程序是要用到repaint()的,具体请参考程序中的注释位置:
为延吉等地区用户提供了全套网页设计制作服务,及延吉网站建设行业解决方案。主营业务为成都网站设计、成都网站建设、延吉网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
import java.awt.*;
import java.awt.event.*;
class mCar extends Frame{
Color redColor;
int xl=80,yl=80,speed=10,step=5;/*********注意这里***********/
public mCar(){
addKeyListener(new KeyAdapter(){ /*********注意这里***********/
public void keyPressed(KeyEvent e){
if(e.getKeyCode()== KeyEvent.VK_UP){
System.out.println("\n Go Up");
yl-=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_DOWN){
System.out.println("\n Go Down");
yl+=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_LEFT){
System.out.println("\n Go Left");
xl-=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_RIGHT){
System.out.println("\n Go Right");
xl+=speed;/*********注意这里***********/
}
else if(e.getKeyCode()== KeyEvent.VK_F1){
speed+=step;/*********注意这里***********/
System.out.println("\n Speed Up");
}
else if(e.getKeyCode()== KeyEvent.VK_F2){
System.out.println("\n Speed Down");
speed-=step;/*********注意这里***********/
}
else
System.out.println(e.getKeyChar());
repaint();/*********注意这里***********/
}
}
);
setSize(400,300);
setVisible(true);
setLocation(400,200);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
}
);
}
public void paint(Graphics g){
g.setColor(Color.GREEN);
g.fillRect(xl, yl, 40, 40);/*********注意这里***********/
}
}
public class miniCar {
public static void main(String[] args){
new mCar();
}
}
改变规制时候的X Y就行了.伪代码如下.
int x =0,y=0,;
x++; y++;
g.drawImage( "图片信息" , x, y,锚点);
大概就这样图片就动了.你想移动到哪加个判断就行了.
你的JPanel如楼上所说并没有一直获取焦点啊,当你面板一直获取焦点时,你要还是快速的放大缩小窗体速度会变的更快,这样就改变了移动速度的,原理也就是改动了焦点的获得频率,你可以固定窗体setResizable(false);,不要让窗体进行放大操作会更好!
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
public class DrawTest extends JFrame {
private int x = 50;
private int y = 50;
private Image offScreenImage = null;
@Override
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, 30, 30);
g.setColor(c);
}
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(500, 500);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, 500, 500);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
public static void main(String[] args) {
DrawTest d = new DrawTest();
}
public DrawTest() {
init();
addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent e) {
int code = e.getKeyCode();
switch (code) {
case KeyEvent.VK_UP:
y -= 5;
break;
case KeyEvent.VK_RIGHT:
x += 5;
break;
case KeyEvent.VK_DOWN:
y += 5;
break;
case KeyEvent.VK_LEFT:
x -= 5;
break;
}
}
});
}
public void init() {
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBackground(Color.GREEN);
this.setResizable(false);
this.setBounds(140, 140, 500, 500);
this.setVisible(true);
MyThread mt = new MyThread();
new Thread(mt).start();
}
class MyThread implements Runnable {
public void run() {
while (true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
以上