java图案模板拼图小程序_JAVA拼图小程序

importjava.awt.Graphics;importjava.awt.GridLayout;importjava.awt.Image;importjava.awt.MediaTracker;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;im...

import java.awt.Graphics;

import java.awt.GridLayout;

import java.awt.Image;

import java.awt.MediaTracker;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;

import java.util.HashSet;

import java.util.Iterator;

import java.util.Random;

import java.util.Set;

import javax.print.attribute.standard.Media;

import javax.swing.AbstractButton;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

public class Pintu extends JFrame implements ActionListener{

Image img;

Icon img1;

BufferedImage bimg;

BufferedImage[] subimg;

int width,height;

JButton[] jb;

public Pintu(){

this.setLayout(new GridLayout(3,3));

img=this.getToolkit().getImage("F:\\JAVA\\eclipse文件\\pintu\\8.jpg");

MediaTracker me=new MediaTracker(this);

me.addImage(img, 0);

try {

me.waitForAll(0);

} catch (InterruptedException e) {

// TODO 自动生成 catch 块

e.printStackTrace();

}

width=img.getWidth(this);

height=img.getHeight(this);

bimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);

Graphics bg=bimg.getGraphics();

bg.drawImage(img,0,0,this);

subimg=new BufferedImage[9];

int a[] = new int[9];

for (int i = 0; i < a.length; i++) {

a[i] = (int) (Math.random() * 9) ;

for (int k = 0; k < i; k++) {

if (a[i] == a[k]) {

i--;

continue;

}

}

}

int z=0;

for(int x=0;x<3;x++){

for(int y=0;y<3;y++){

subimg[a[z]]=bimg.getSubimage(width*x/3, height*y/3, width/3, height/3);

z++;

}

}

jb=new JButton[9];

for(int i=0;i

jb[i]=new JButton();

jb[i].addActionListener(this);

this.add(jb[i]);

jb[i].setIcon(new ImageIcon(subimg[i]));

}

setSize(img.getWidth(this),img.getHeight(this));

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

/*public void paint(Graphics g){

g.drawImage(subimg[0],0,0,this);

}*/

public static void main(String[] args) {

new Pintu();

}

public void actionPerformed(ActionEvent e) {

img1=jb[0].getIcon();

jb[0].setIcon(((AbstractButton) e.getSource()).getIcon());

((AbstractButton) e.getSource()).setIcon(img1);

}

}帮我翻译下这程序每句的意思,谢谢了

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import java.awt.*; import java.applet.*; import java.awt.event.*; public class PPuzzle extends Applet{ Image imgPuzzle,buff; Point fifteen=new Point(3,3); int[][] map={{0,4,8,12},{1,5,9,13},{2,6,10,14},{3,7,11,15}}; int sx,sy; Canvas screen; Graphics gs,gb; boolean running=false; Button bStart= new Button("新游戏"); Button bSee=new Button("显示正确图像"); public void init(){ prepareImage(); sx=imgPuzzle.getWidth(this)/4; sy=imgPuzzle.getHeight(this)/4; setBackground(Color.blue); initScreen(); initButtons(); add(screen); add(bStart); add(bSee); } void prepareImage(){ imgPuzzle=getImage(getCodeBase(),"images/3.jpg");// MediaTracker mt=new MediaTracker(this); mt.addImage(imgPuzzle, 0); try{ mt.waitForAll(); }catch(Exception e){} //创建buffer并获取graphics对象 buff=createImage(imgPuzzle.getWidth(this),imgPuzzle.getHeight(this)); gb=buff.getGraphics(); } void initMap(){ java.util.Random rnd=new java.util.Random(); int temp,x1,x2,y1,y2; for(int i=0;i<100;i++){ x1=rnd.nextInt(4); x2=rnd.nextInt(4); y1=rnd.nextInt(4); y2=rnd.nextInt(4); temp=map[x1][y1]; map[x1][y1]=map[x2][y2]; map[x2][y2]=temp; } outer:for(int j=0;j<4;j++) for(int i=0;i<4;i++) if(map[i][j]==15){ fifteen.setLocation(i,j); break outer; } } void initScreen(){ screen=new Canvas(){ public void paint(Graphics g){ if(gs==null) gs=getGraphics(); if(running) drawScreen(); else g.drawImage(imgPuzzle,0,0,this); } }; screen.setSize(imgPuzzle.getWidth(this), imgPuzzle.getHeight(this)); screen.addMouseListener(new MouseAdapter(){ public void mousePressed(MouseEvent me){ if(!running)return; int x=me.getX()/sx,y=me.getY()/sy; int fx=(int)fifteen.getX(),fy=(int)fifteen.getY(); if (Math.abs(fx-x)+Math.abs(fy-y)>=2)return; map[fx][fy]=map[x][y]; map[x][y]=15; fifteen.setLocation(x,y); drawScreen(); } }); } void initButtons(){ //新游戏 按钮事件的处理 bStart.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ initMap(); drawScreen(); running=true; bSee.setLabel("显示正确图像"); } }); //显示正确图像 按钮事件处理 bSee.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ //bsee 按钮标题为”继续游戏“ if(bSee.getLabel().equals("继续游戏")){ drawScreen(); bSee.setLabel("继续游戏"); } else{ //bsee的标题显示为”显示正确图像“ gs.drawImage(imgPuzzle,0,0,screen); bSee.setLabel("继续游戏"); } } }); } void drawScreen(){ gb.clearRect(0, 0, sx*4, sy*4); //将指定位置的图像块绘制到Buffer中 for(int j=0;j<4;j++) for(int i=0;i<4;i++) if(map[i][j]!=15) drawSegment(map[i][j],i,j); //向Screen绘制buffer中的图像 gs.drawImage(buff,0,0,screen); } void drawSegment(int seg,int x,int y){ int dx=seg%4*sx,dy=seg/4*sy; //可能有错误 gb.drawImage(imgPuzzle, x*sx,y*sy ,x*sx+sx-1 ,y*sy+sy-1 , dx , dy,dx+sx-1,dy+sy-1 ,screen ); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值