去掉TextField后的程序,這個(gè)程序是要用到repaint()的,具體請(qǐng)參考程序中的注釋位置:
為延吉等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及延吉網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、延吉網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
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();
}
}
改變規(guī)制時(shí)候的X Y就行了.偽代碼如下.
int x =0,y=0,;
x++; y++;
g.drawImage( "圖片信息" , x, y,錨點(diǎn));
大概就這樣圖片就動(dòng)了.你想移動(dòng)到哪加個(gè)判斷就行了.
你的JPanel如樓上所說(shuō)并沒(méi)有一直獲取焦點(diǎn)啊,當(dāng)你面板一直獲取焦點(diǎn)時(shí),你要還是快速的放大縮小窗體速度會(huì)變的更快,這樣就改變了移動(dòng)速度的,原理也就是改動(dòng)了焦點(diǎn)的獲得頻率,你可以固定窗體setResizable(false);,不要讓窗體進(jìn)行放大操作會(huì)更好!
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();
}
}
}
}
}
以上
網(wǎng)站欄目:java圖片上下移動(dòng)代碼 java怎么實(shí)現(xiàn)上下移動(dòng)效果
瀏覽路徑:http://jinyejixie.com/article6/dopsdog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、動(dòng)態(tài)網(wǎng)站、企業(yè)建站、ChatGPT、電子商務(wù)、小程序開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)