package program;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
* 裁减合适的图片作为游戏使用的图片
* 图片初始必须大于等于裁减区域
* 可以移动图片选择合适的区域以及调整图片的大小
* 图片最大不能超过它本身的150%
* 最小不能超过裁减区域的大小
*/
@SuppressWarnings("serial")
public class ChoicePicture extends JPanel implements MouseMotionListener, MouseListener, MouseWheelListener
{
public static final int WIDTH = 700;// 窗口宽度
public static final int HEIGHT = 700;// 窗口高度
public static final int BORDER = 50;// 透明边框大小
public static final int COMPASSION_SIZE = 10;// 图片每次伸缩的大小
public static File directory;//保存添加图片时的目录
// 保存图片原来的大小
private int sourceImgWidth;
private int sourceImgHeight;
private int imgMaxSize;// 图片的最大大小
private BufferedImage img;// 被裁减的图片
private int imgWidth;// 图片的宽度
private int imgHeight;// 图片的高度
private int picStartX;// 图片的左上角坐标
private int picStartY;
private int picEndX;// 图片的右下角坐标
private int picEndY;
// 鼠标按下时的坐标
private int pressedX;
private int pressedY;
// 记录图片停止移动后的左下角坐标
private int stopMoveX;
private int stopMoveY;
private boolean inTrimArea;// 鼠标是否在裁减区域
private boolean isPressed;// 鼠标是否被按下了
private MPGStatePanel statePanel;
private JFrame f;
private JButton define;
private JButton reselect;
private JButton cancel;
public ChoicePicture(BufferedImage img,MPGStatePanel statePanel)
{
this.statePanel = statePanel;
this.img = img;
init();// 初始化域
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.addMouseWheelListener(this);
f = new JFrame("添加图片");
Container cp = f.getContentPane();
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(WIDTH - 9, HEIGHT - 9));
p.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
define = new JButton("确定");
define.addActionListener(new ActionListener()
{
/**
* 从本地磁盘添加一张图片
*/
@Override
public void actionPerformed(ActionEvent e)
{
//计算图片的缩放比例
double scaleX = (double)ChoicePicture.this.imgWidth / (double)ChoicePicture.this.img.getWidth();
double scaleY = (double)ChoicePicture.this.imgHeight / (double)ChoicePicture.this.img.getHeight();
//缩放图片
AffineTransform at = new AffineTransform();
at.scale(scaleX, scaleY);
AffineTransformOp ato = new AffineTransformOp(at,AffineTransformOp.TYPE_BILINEAR);
BufferedImage image = ato.filter(ChoicePicture.this.img, null);
//计算裁减图片的大小
int border = ChoicePicture.BORDER;
int x = ChoicePicture.this.picStartX;
int y = ChoicePicture.this.picStartY;
int w = ChoicePicture.WIDTH;
int h = ChoicePicture.HEIGHT;
image = image.getSubimage(border - x, border - y, w - border * 2, h - border * 2);
//返回图片
ChoicePicture.this.statePanel.setThumbail(image);
ChoicePicture.this.f.dispose();
}
});
reselect = new JButton("重新选择");
reselect.addActionListener(new ActionListener()
{
/**
* 重新在本地磁盘选择图片
*/
@Override
public void actionPerformed(ActionEvent e)
{
ChoicePicture.this.img = ChoicePicture.addPicture(f,directory);
if (ChoicePicture.this.img != null)
{
ChoicePicture.this.init();//初始化数据,图片的坐标和大小
ChoicePicture.this.repaint();
}
}
});
cancel = new JButton("取消");
cancel.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
ChoicePicture.this.f.dispose();
}
});
buttonPanel.add(define);
buttonPanel.add(reselect);
buttonPanel.add(cancel);
p.add(this);
cp.add(buttonPanel, BorderLayout.SOUTH);
cp.add(p, BorderLayout.CENTER);
f.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation((screenSize.width - WIDTH) / 2, 0);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
}
public void init()
{
//保存图片的原始大小
this.sourceImgWidth = img.getWidth();
this.sourceImgHeight = img.getHeight();
this.imgMaxSize = (int) (img.getWidth() * 1.5);
// 图片大小
this.imgWidth = img.getWidth();
this.imgHeight = img.getHeight();
// 图片左上角坐标
this.picStartX = BORDER;
this.picStartY = BORDER;
// 图片右下角坐标
this.picEndX = picStartX + img.getWidth();
this.picEndY = picStartY + img.getHeight();
// 图片停止移动左上角坐标
this.stopMoveX = BORDER;
this.stopMoveY = BORDER;
this.inTrimArea = false;
this.isPressed = false;
}
/**
* 弹出添加图片对话框
* @param 相对显示的窗体
* @return
*/
public static BufferedImage addPicture(Container c,File dir)
{
JFileChooser chooser = new JFileChooser();
chooser.addChoosableFileFilter(new PictureFilter());//可以选择的文件类型
chooser.setCurrentDirectory(dir);//设置目录
chooser.setMultiSelectionEnabled(false);//不允许多选
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//只允许选择文件
chooser.setDialogTitle("请选择一张图片");
chooser.setAcceptAllFileFilterUsed(false);//全部文件选项取消
if(chooser.showOpenDialog(c) == JFileChooser.APPROVE_OPTION)
{
try
{
BufferedImage img = ImageIO.read(new File(chooser.getSelectedFile().getPath()));
ChoicePicture.directory = chooser.getCurrentDirectory();
if (ChoicePicture.pictureSize(img))//判断是否符合大小
{
return img;
}
else
{
JOptionPane.showMessageDialog(null, "图片大小必须大于 600 * 600像素!");
}
} catch (IOException e1)
{
JOptionPane.showMessageDialog(null, "找不到该图片!");
e1.printStackTrace();
}
}
return null;
}
/**
* 图片的大小是否符号要求
*
* @param img
* @return
*/
public static boolean pictureSize(BufferedImage img)
{
boolean w = img.getWidth() >= WIDTH - BORDER * 2 ? true : false;
boolean h = img.getHeight() >= HEIGHT - BORDER * 2 ? true : false;
return w & h;
}
/**
* 绘制图片的位置,根据左上角坐标和右下角坐标确定该图片的位置和大小
*/
public void paint(Graphics g)
{
super.paint(g);
picEndX = picStartX + imgWidth;
picEndY = picStartY + imgHeight;
g.drawImage(img, picStartX, picStartY, picEndX, picEndY, 0, 0, img.getWidth(), img.getHeight(), null);
// 裁减区域方框
g.setColor(new Color(128, 128, 128));
g.drawRect(BORDER, BORDER, WIDTH - BORDER * 2, HEIGHT - BORDER * 2);
g.drawRect(0, 0, WIDTH, HEIGHT);
// 透明边缘
g.setColor(new Color(255, 255, 255, 100));
g.fillRect(0, 0, WIDTH, BORDER);// 顶边框
g.fillRect(0, HEIGHT - BORDER, WIDTH, BORDER);// 底边框
g.fillRect(0, BORDER, BORDER, HEIGHT - BORDER * 2);// 左边框
g.fillRect(WIDTH - BORDER, BORDER, BORDER, HEIGHT - BORDER * 2);// 右边框
}
/**
* 缩小图片
*/
public void scale()
{
// 图片缩小后不小于裁减区域
if (imgWidth > WIDTH - BORDER * 2 && imgHeight > HEIGHT - BORDER * 2)
{
boolean startX = picStartX + COMPASSION_SIZE > BORDE
- 1
- 2
前往页