本人想做一个拼图游戏 如何将一张图片分割成9份 简洁明了 没时间看太多东西 学习中

解决方案 »

  1.   

    http://www.javaeye.com/problems/43676
      

  2.   


    package com.image;import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.Date;import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;@SuppressWarnings("serial")
    public class TestImage extends JFrame { private static String imagepath="src\\image\\aa.jpg";

    private BufferedImage image; private JButton[] jbuttons=new JButton[9];

    private BufferedImage[] bis=new BufferedImage[9];
    /**
     * @param args
     */
    public static void main(String[] args) {
     System.out.println("支持写的图片格式:" +Arrays.toString(ImageIO.getWriterFormatNames()));
     new TestImage();
    } public TestImage(){
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    try {
    image=ImageIO.read(new File(imagepath));
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    int image_width=image.getWidth();
    int image_height=image.getHeight();
    //预览窗口
    //得到当前图片1/3大小的图片,并画到buffered_image当中
    Image small_image=image.getScaledInstance(image_width/3, image_height/3, Image.SCALE_DEFAULT);
    BufferedImage buffered_image=new BufferedImage(image_width/3,image_height/3,BufferedImage.TYPE_INT_BGR);
    Graphics g = buffered_image.getGraphics();      
            g.drawImage(small_image, 0, 0, null); 
            try {
    ImageIO.write(buffered_image, "jpeg", new FileOutputStream("bb.jpg"));
    } catch (IOException e1) {
    e1.printStackTrace();
    }
            JFrame small_frame=new JFrame("图片预览");
            small_frame.add(new JButton(new ImageIcon(buffered_image)));
            small_frame.setSize(image_width/3, image_height/3);
            small_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            small_frame.setVisible(true);
            
    int block_x=image_width/3;
    int block_y=image_height/3;
    bis[0]=image.getSubimage(0, 0,block_x,block_y);
    bis[1]=image.getSubimage(block_x,0,block_x,block_y);
    bis[2]=image.getSubimage(2*block_x,0,block_x,block_y);
    bis[3]=image.getSubimage(0, block_y,block_x,block_y);
    bis[4]=image.getSubimage(block_x, block_y,block_x,block_y);
    bis[5]=image.getSubimage(2*block_x, block_y,block_x,block_y);
    bis[6]=image.getSubimage(0, 2*block_y,block_x,block_y);
    bis[7]=image.getSubimage(block_x, 2*block_y,block_x,block_y);
    bis[8]=image.getSubimage(2*block_x, 2*block_y,block_x,block_y);
    for(int i=0;i<jbuttons.length;i++){
    jbuttons[i]=new JButton(new ImageIcon(bis[i]));
    jbuttons[i].addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    Date date=new Date(e.getWhen());
    System.out.println(date);
    }
    });
    }
    this.setLayout(new GridLayout(3,3));
    for(int i=jbuttons.length-1;i>=0;i--){
    this.add(jbuttons[i]);
    }
    this.setSize(image_width,image_height);
    this.setVisible(true);
    }

    }改下图片地址,能满足楼主要求了