java线程游戏之背景图片的移动
2024-08-25 22:05:49
package com.plane; import java.awt.Graphics;
import java.awt.Image; import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel; public class PlaneMain extends JPanel{
private Image background;
private Plane pl;
public static void main(String[] args) {
new PlaneMain();
}
public PlaneMain(){ background = new ImageIcon(this.getClass().getResource("background.jpg")).getImage();
pl = new Plane(background,0,-100,this);
initUI(); } public void initUI(){
JFrame jf = new JFrame();
jf.setSize(1000, 900);
jf.setDefaultCloseOperation(3);
jf.setLocationRelativeTo(null); jf.add(this);
jf.setVisible(true);
pl.start(); }
//Plane pl = new Plane(background,0,0,this);
public void paint(Graphics g){
super.paint(g);
g.drawImage(pl.background, pl.x, pl.y, 1000, 1000, this); }
}
package com.plane; import java.awt.Image; import javax.swing.JPanel; public class Plane extends Thread{
public Image background;
public int x=0;
public int y=-60;
public JPanel jp;
public int movesp=2;
public Plane(Image background,int x,int y,JPanel jp){
this.background = background;
System.out.println(this.background);
this.x = x;
this.y = y;
this.jp = jp;
}
public void move(){
y+=movesp;
if (y==0){
y=-100;
}
}
public void run(){ while(true){
move();
jp.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
最新文章
- Android图片资源
- $(this)与this的区别
- php调用一个c语言写的接口问题
- ThinkPHP中initialize和construct的不同
- bzoj 1791: [Ioi2008]Island 岛屿
- VSTO Word2003 添加菜单栏, 添加工具栏
- 使用VNC远程连接Windows Azure Linux虚拟机
- WCF-IIS-PDA
- AssetManager中的路径参数不能包含";assets/";
- 在JS事件封装时,addEventListener()方法的this问题
- eclipes快捷键
- 洛谷 P4168 [Violet] 蒲公英
- angular2报错
- js中 switch 注意事项
- 【SqlServer】SqlServer的游标使用
- opencv -python
- Java happen-before
- 快速了解和使用Photon Server
- 操作系统:Android(Google公司开发的操作系统)
- php利用root权限执行shell脚本 (转)
热门文章
- ceilometer alarm 创建过程中的DB操作及优化
- JavaScript 对时间日期格式化
- PyCharm 的初始设置2 - 打开、新建项目
- 《Advanced Bash-scripting Guide》学习(十九):两个整数的最大公约数
- Get UTI (uniform type identifier) and ContentType
- Spring3.2.9 + JdbcTemplate 学习
- 制作导航菜单分隔线的总结:用css3
- 17 python 内置函数
- stencil in unity3d
- kibana 统计field所有值百分比