我正在尝试将图像从一个目录(仅包含图像)逐个加载到JPanel。我想使用鼠标事件逐个裁剪图像。我编写了一个程序,用于加载一个图像,通过它给JPanael的路径,然后使用鼠标裁剪它。但我不知道如何逐个扩展多个图像的程序并将裁剪的图像保存在不同的目录中。我也试过摆动计时器,但我无法想象如何使用它。可以请任何帮助吗?
这是我的单张图片代码。
public class MainActivity extends JFrame implements MouseListener,MouseMotionListener{
int drag_status=0,c1,c2,c3,c4;
static String[] pathArray = new String[1000];
public static void main(String args[]) {
new MainActivity().start();
String s = "G:\\my pic";
File f = new File(s);
String str;
System.out.println(true);
//just to put all the pictures in list
File[] fa = f.listFiles();
for (int i=0; i< fa.length; i++) {
String path = fa[i].toString();
str = path.replace("\\", "\\\\");
pathArray [i] = str;
System.out.println(pathArray[2]); //gives all the paths in ImagePanel path format
}
}
public void start() {
ImageJPanel imageJPanel=new ImageJPanel(pathArray[1]);
add(imageJPanel);
setSize(200,200);
setVisible(true);
addMouseListener(this);
addMouseMotionListener( this );
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
repaint();
c1=arg0.getX();
c2=arg0.getY();
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
repaint();
if(drag_status==1) {
c3=arg0.getX();
c4=arg0.getY();
try {
draggedScreen();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
public void draggedScreen()throws Exception {
int width = c1 - c3;
int height = c2 - c4;
width = width * -1;
height = height * -1;
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(c1, c2,width,height));
File save_path=new File("C:\\Users\\apurvgandhwani\\Desktop\\cropped");
ImageIO.write(img, "JPG", save_path);
System.out.println("Saved to folder");
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
repaint();
drag_status=1;
c3=arg0.getX();
c4=arg0.getY();
}
public void paint(Graphics g) {
super.paint(g);
int width = c1 - c3;
int height = c2 - c4;
width = width * -1;
height = height * -1;
if(width<0) width = width * -1;
g.drawRect(c1, c2, width, height); }
}
和ImageJPanel类
public class ImageJPanel extends JPanel{
private static final long serialVersionUID = 1L;
private Image image;
int x = 0;
public ImageJPanel(String image){
this(new ImageIcon(image).getImage());
Timer timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
x += 110;
if (x >= 1000) {
x = 1000;
((Timer)e.getSource()).stop();
}
repaint();
}
});
timer.start();
}
public ImageJPanel(Image image){
this.image = image;
Dimension size = new Dimension(image.getWidth(null), image.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g){
g.drawImage(image, 0, 0, null);
}
}