首页 > 代码库 > java之控制多幅图片
java之控制多幅图片
package xxj.thread0904;import java.awt.Image;import javax.swing.ImageIcon;import javax.swing.JFrame;public class Plane { // extends Thread { public Image image; public int y = 300, x = 100, width = 50, height = 50; //public JFrame frame; public Plane(int x,int y){//,JFrame frame) { this.x = x; this.y = y; //this.frame = frame; image = new ImageIcon(this.getClass().getResource("plane.jpg")) .getImage(); } public void move(){ y-=3; if(y<=40) y = 300; } //// /**// * 重写Runnable接口的run方法// */// public void run() {// while (true) {// y -= 3;// if (y <= 50)// y = 300;// frame.repaint();// try {// Thread.sleep(100);// } catch (InterruptedException e) {// e.printStackTrace();// }// }// }}
package xxj.thread0904;import java.awt.Graphics;import java.util.ArrayList;import javax.swing.JFrame;public class PlaneDemo extends JFrame { public static void main(String[] args) { new PlaneDemo(); } public PlaneDemo() { list = new ArrayList<Plane>(); initUI(); } private ArrayList<Plane> list; private void initUI() { this.setTitle("线程控制图片移动"); this.setSize(600, 400); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(3); this.setResizable(false); this.setVisible(true); PlaneListener pl = new PlaneListener(list,this); this.addMouseListener(pl); Thread t = new Thread(pl); t.start(); } /** * 重写窗体的paint方法 */ public void paint(Graphics g) { super.paint(g); for (int i = 0; i < list.size(); i++) { Plane plane = list.get(i); g.drawImage(plane.image, plane.x, plane.y, plane.width, plane.height, this); } }}
package xxj.thread0904;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.util.ArrayList;import javax.swing.JFrame;public class PlaneListener extends MouseAdapter implements Runnable { private ArrayList<Plane> list; private JFrame frame; public PlaneListener(ArrayList<Plane> list,JFrame frame) { this.list = list; this.frame = frame; } public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); Plane plane = new Plane(x,y);//,frame); list.add(plane);// plane.start(); } public void run(){ while(true){ for(int i=0;i<list.size();i++){ Plane plane = list.get(i); plane.move(); } frame.repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }
这些代码,实现了简单的控制
java之控制多幅图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。