首页 > 代码库 > 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之控制多幅图片