温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java制作拼图游戏

发布时间:2020-06-06 14:20:18 来源:亿速云 阅读:237 作者:Leah 栏目:编程语言

这篇文章主要为大家详细介绍了Java制作拼图游戏的方法,图文详解容易学习,配合代码阅读理解效果更佳,非常适合初学者入门,感兴趣的小伙伴们可以参考一下。

所用素材:Java制作拼图游戏

代码:Java制作拼图游戏
Java制作拼图游戏

package picture_mosical;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;

import javax.imageio.ImageIO;

public class PictureCut {
    private static int width;// 切割后图片的宽度
    private static int height;// 切割后图片的高度
    private static String dir_name = "/CutImage";// 存放图片的文件夹名称

    public static void cut(String paths, int rows, int cols, String dir)
            throws Exception {
        Image img;
        ImageFilter new_if;//fasf你的
        BufferedImage bi = ImageIO.read(new File(paths));// 读取图像源
        if (bi == null) {
            System.out.println("图像源为空");
            return;
        }
        int baseWidth = bi.getWidth();// 读取图像源的宽度
        int baseHeight = bi.getHeight();// 读取图像源的高度
        width = baseWidth / cols;// 切割后图片的宽
        height = baseHeight / rows;// 切割后图片的高
        System.out.println("width:" + width + "\t\theight:" + height);
        System.err.println("切割的行数=[" + rows + "]\n切割的列数=[" + cols + "]");
        Image image = bi.getScaledInstance(baseWidth, baseHeight,
                Image.SCALE_DEFAULT);
        BufferedImage bimg;
        File file;
        int x = 0, y = 0;
        int index = 0;
        // 开始对图像源进行切割
        long s = Math.round(Math.random() * 8);
        System.out.println(s + ">>>>>>>>>>>>>>>");
        boolean flag = true;
        for (int i = 0; i < rows; i++) {
            y = (int) (i * height);
            for (int j = 0; j < cols; j++) {
                if (index == 8) {
                    index=9;
                    new_if = new CropImageFilter(0, 0, baseWidth, baseHeight);
                    width=baseWidth;
                    height=baseHeight;

                }else{
                    x = (int) (j * width);
                    new_if = new CropImageFilter(x, y, width, height);
                }

                img = Toolkit.getDefaultToolkit().createImage(
                        new FilteredImageSource(image.getSource(), new_if));
                bimg = new BufferedImage(width, height,
                        BufferedImage.TYPE_INT_RGB);
                Graphics gi = bimg.getGraphics();
                gi.drawImage(img, 0, 0, null);
                gi.dispose();
                file = new File(dir + index + ".jpg");
                ImageIO.write(bimg, "JPEG", file);

                index++;

            }
        }

    }

    public static void main(String[] args) {
        try {
            PictureCut.cut("src/picture_mosical/123.jpg", 3, 3,
                    "WebRoot/images/");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

看完上述内容,你们掌握Java制作拼图游戏的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI