j2se图片拖拽练习

package graphicsDrag;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 图片拖拽练习
 * @author tiger
 *
 *	0,点中图片拖动可移动图片位置
 *	1,拖动鼠标选中区域内图片
 *  2,按住Ctrl键可连续选择图片,并拖动所有已选图片。
 *  3,按下shift键,在拖动同时复制图片。
 */

@SuppressWarnings({ "unchecked", "serial" })
public class MyPanel extends JPanel implements MouseListener, MouseMotionListener{

	private int width = 600;
	private int height = 450;
	
	/**
	 * 所有图片集合
	 */
	private List list = new LinkedList();
	/**
	 * 当前所选图片集合
	 */
	private List select_list = new LinkedList();
	/**
	 * 当前所选图片的复制品的集合 。。。用于按下Ctrl键时的拖动
	 */
	private List copy_list = new LinkedList();
	
	public MyPanel()
	{
		this.setPreferredSize(new Dimension(width,height));
		this.setFocusable(true);
		
        ImageObject object = new ImageObject("g_2_1.png", 100, 200);
        list.add(object);
        object = new ImageObject("g_434.png", 50, 200);
        list.add(object);
        object = new ImageObject("g_461.png", 150, 300);
        list.add(object);
        object = new ImageObject("g_462.png", 400, 100);
        list.add(object);
        
        
        this.setFocusable(true);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
	}
	
	
	
	/**
	 * 封装了图片的对象
	 * @author tiger
	 *
	 */
	class ImageObject{
		Image image ;
		int x, y;
		Rectangle rect;
		boolean isSelect;
		
		public ImageObject() {
		}
		
		public ImageObject(String imageName, int x, int y) {
			ImageIcon icon = new ImageIcon(getClass().getResource("/image/" + imageName));
	        image = icon.getImage();
	        this.x = x;
	        this.y = y;
	        rect = new Rectangle(x, y, image.getWidth(null), image.getHeight(null));
		}
		public void paint(Graphics g) {
			g.drawImage(image, x, y, null);
			if(isSelect)
			{
				g.setColor(Color.BLACK);
				g.drawRect(x - 1, y - 1, image.getWidth(null) + 1, image.getHeight(null) + 1);
			}
		}
		
		public boolean contains (int x, int y) {
			return rect.contains(x, y);
		}
		
		public boolean contains (Point p) {
			return rect.contains(p.x, p.y);
		}
		
		public void move(int moveX, int moveY)
		{
			this.x += moveX;
			this.y += moveY;
			this.rect.x = this.x;
			this.rect.y = this.y;
		}
		
		public ImageObject clone()
		{
			ImageObject object = new ImageObject();
			object.x = this.x;
			object.y = this.y;
			object.image = this.image;
			object.isSelect = this.isSelect;
			object.rect = new Rectangle(x, y, image.getWidth(null), image.getHeight(null));
			return object;
		}
	}
	
	
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		ImageObject object = null;
		for (int i = 0; i < list.size(); i++) {
			object = (ImageObject) list.get(i);
			object.paint(g2);
		}
		
		//半透明画复制图
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.75f));
		for (int i = 0; i < copy_list.size(); i++) {
			object = (ImageObject) copy_list.get(i);
			object.paint(g2);
		}
		
		//画选择框
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1));
		if(selectRect != null)
		{
			g2.setColor(Color.green);
			g2.draw(selectRect);
		}
		
		g2.dispose();
	}
	
	
	


	@Override
	public void mouseClicked(MouseEvent e) {
	}
	@Override
	public void mouseEntered(MouseEvent e) {
	}
	@Override
	public void mouseExited(MouseEvent e) {
	}

	/**
	 * 上次点击的点
	 */
	private Point prePoint ;

	
	
	/**
	 * ctrl按下后可以连续选择图片
	 * shift按下后会复制所选择的图片
	 */
	@Override
	public void mousePressed(MouseEvent e) {
		Point p = e.getPoint();
		ImageObject object;
		
		if (e.isControlDown())
		{
			System.out.println("ctrl is press down !!!");
		}else{
			for (int i = 0; i < select_list.size(); i++) {
				object = (ImageObject) select_list.get(i);
				object.isSelect = false;
			}
			select_list.clear();
		}
		
		for (int i = 0; i < list.size(); i++) {
			object = (ImageObject) list.get(i);
			if(object.contains(p) && select_list.contains(object) == false)
			{
				select_list.add(object);
				object.isSelect = true;
				
				if(e.isShiftDown())
				{					
					copy_list.add(object.clone());
				}
				break;
			}
		}
		
		prePoint = p;
		this.repaint();
	}

	private Rectangle selectRect;

	@Override
	public void mouseDragged(MouseEvent e) {
		Point p = e.getPoint();
		
		if(select_list.isEmpty())
		{
			selectRect = new Rectangle(Math.min(p.x, prePoint.x), Math.min(p.y, prePoint.y), 
					Math.abs(p.x - prePoint.x), Math.abs(p.y - prePoint.y));
			this.repaint();
			return;
		}
		
		//针对用选择框选择的图片,shift按下时copy_list还为空的情况
		if(e.isShiftDown() && copy_list.isEmpty())
		{
			for (int i = 0; i < select_list.size(); i++) {
				copy_list.add(((ImageObject) select_list.get(i)).clone());
			}
		}
		
		
		int moveX = p.x - prePoint.x;
		int moveY = p.y - prePoint.y;
		ImageObject object;
		
		List list ;
		if(e.isShiftDown())
		{
			list = copy_list;
		}else{
			list = select_list;
		}
		
		for (int i = 0; i < list.size(); i++) {
			object = (ImageObject) list.get(i);
			object.move(moveX, moveY);
		}
		prePoint = p;
		this.repaint();
	}

	

	@Override
	public void mouseReleased(MouseEvent e) {
		prePoint = null;
		
		if(selectRect != null)
		{
			ImageObject object;
			for (int i = 0; i < list.size(); i++) {
				object = (ImageObject) list.get(i);
				if(object.rect.intersects(selectRect))
				{
					select_list.add(object);
					object.isSelect = true;
				}
			}
			selectRect = null;
			this.repaint();
			return;
		}
		
		
		if(e.isShiftDown() || e.isControlDown())
		{
			return;
		}
		
		ImageObject object;
		for (int i = 0; i < copy_list.size(); i++) {
			object = (ImageObject) copy_list.get(i);
			object.isSelect = false;
			list.add(object);
		}
		copy_list.clear();
		this.repaint();
	}

	@Override
	public void mouseMoved(MouseEvent e) {
	}
	
	
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("图片拖动测试");
		JPanel panel = new MyPanel();
		frame.getContentPane().add(panel);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
}

posted on 2011-05-31 11:09 千山万水_ 阅读(...) 评论(...) 编辑 收藏

导航

公告