日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 绘图球的移动_求助在JFrame上绘制移动的小球

發布時間:2023/12/9 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 绘图球的移动_求助在JFrame上绘制移动的小球 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我想在JFrame中或者Frame中添加一張背景圖片,然后在這圖片上畫出會移動的小球,怎么實現?我的代碼把添加背景圖片去掉,小球就正常運行了,

怎么修改啊?

希望各位大俠指教

不勝感激!!!!

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class TestItem extends JFrame {

JLabel jl;

Bullet b = new Bullet(80,80);

public static void main(String[] args) {

new TestItem().lauchFrame();

}

public void lauchFrame() {

jl = new JLabel();

ImageIcon image = new ImageIcon("Images\\mainBack.png");

jl.setIcon(image);

jl.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());

this.setTitle("坦克大戰");

this.setSize(246, 350);

this.setVisible(true);

this.getLayeredPane().add(jl, new Integer(Integer.MIN_VALUE));

((JPanel)getContentPane()).setOpaque(false);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

this.setBackground(Color.black);

new Thread(new paintThread()).start();

this.addKeyListener(new keyMonitor());

}

public void paint(Graphics g) {

b.draw(g);

}

private class paintThread implements Runnable {

public void run() {

while(true) {

repaint();

try {

Thread.sleep(80);

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

private class keyMonitor extends KeyAdapter {

public void keyPressed(KeyEvent e) {

b.keyPressed(e);

}

public void keyReleased(KeyEvent e) {

b.keyReleased(e);

}

}

}

import java.awt.Color;

import java.awt.Graphics;

import java.awt.event.KeyEvent;

public class Bullet {

private int x;

private int y;

private static final int width = 16;

private static final int height = 16;

private static final int speed = 8;

Direction dir = Direction.D;

private boolean bU = false,bR = false,bD = false,bL = false;

public Bullet(int x, int y) {

this.x = x;

this.y = y;

}

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 draw(Graphics g) {

Color c = g.getColor();

g.setColor(Color.yellow);

g.fillOval(x, y, width, height);

g.setColor(c);

move();

}

private void move() {

switch(dir) {

case U:

y -= speed;

break;

case R:

x += speed;

break;

case D:

y += speed;

break;

case L:

x -= speed;

break;

}

}

public void keyPressed(KeyEvent e) {

switch(e.getKeyCode()) {

case KeyEvent.VK_UP:

bU = true;

break;

case KeyEvent.VK_RIGHT:

bR = true;

break;

case KeyEvent.VK_DOWN:

bD = true;

break;

case KeyEvent.VK_LEFT:

bL = true;

break;

}

licalDirection();

}

private void licalDirection() {

if(bU && !bR && !bD && !bL) {

dir = Direction.U;

} else if(!bU && bR && !bD && !bL) {

dir = Direction.R;

} else if(!bU && !bR && bD && !bL) {

dir = Direction.D;

} else if(!bU && !bR && !bD && bL) {

dir = Direction.L;

}

}

public void keyReleased(KeyEvent e) {

switch(e.getKeyCode()) {

case KeyEvent.VK_UP:

bU = false;

break;

case KeyEvent.VK_RIGHT:

bR = false;

break;

case KeyEvent.VK_DOWN:

bD = false;

break;

case KeyEvent.VK_LEFT:

bL = false;

break;

}

licalDirection();

}

}

public enum Direction {

U,R,D,L;

}

總結

以上是生活随笔為你收集整理的java 绘图球的移动_求助在JFrame上绘制移动的小球的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。