速度恢复 本人想做一个拼图游戏 如何将一张图片分割成9份 简洁明了 没时间看太多东西 学习中 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.javaeye.com/problems/43676 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); } }改下图片地址,能满足楼主要求了 求助,关于File的构造函数简单思考? 求Java swt教程 Voctor错误,请帮忙修改 语法问题 关于getParameterValues方法和动态数组与集合的问题求教 看看这个小程序错误在哪里,多谢了!! 一个很孤独,依赖性很强却又很喜欢JAVA的人会学好JAVA吗? 请问各位大侠帮忙!怎样安装java.comm.properties! Java的编程环境有哪些 zilong:JAVA有专门存放字符串等资源的资源文件吗? 谁有<JAVA编程思想>最新版的电子书? nio 接收信息的时候说java.io.IOException: 您的主机中的软件放弃了一个已建立的连接
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);
}
}改下图片地址,能满足楼主要求了