⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 piece.java

📁 用java编写的拼图游戏
💻 JAVA
字号:
package mypackage;
import java.awt.*;
class piece {
	static Integer value;
	Integer x_pos,y_pos;//图块的位置
	int width=50,height=50;
	Graphics gc;
    int left,right,up,down;
	Image pic;
    int flags;
    puzzle obj;
    loadThread img_thread;
    piece(int index,int val,Graphics agc,int x,int y,Image pict,puzzle obj){
    	int temp;
    	if(obj!=null){
    		this.obj=obj;
    		pic=null;
    		//pic=obj.getImage(obj.getCodeBase(),"Christmas_"+val+".jpg");
    		img_thread=new loadThread("thread"+index,obj.image_name+val+".gif",this,index);
    		img_thread.start();
    	}
    	value=new Integer(val);
    	gc=agc;
    	x_pos=new Integer(x);
    	y_pos=new Integer(y);
       //右
     temp=index+1;
     if((temp==4)||(temp==8)||(temp==12)||(temp==16)){
    	 right=-1;}
     else
    	 right=temp;
     //左
     temp=index-1;
     if((temp==3)||(temp==7)||(temp==11))
    	 right=-1;
     else
    	 right=temp;
     //上
     temp=index-4;
     if(temp<0){
    	 up=-1;}
     else
    	 up=temp;
     //下
     temp=index+4;
     if(temp<0){
    		 down=-1;}
     else
    	 down=temp;
    }
    public void setImage(Image pic) {
    	if(pic!=null)
    	this.pic=pic;
    }
    //判断点是否在块内
    public boolean point_inside(int x,int y){
    	if((x>=x_pos.intValue())&&(y>=x_pos.intValue())&&(x<=x_pos.intValue()+width)&&(y<=x_pos.intValue()+height))
    		return true;
    	else
    		return false;
    }
    //方法same_col(int x)用来判断x是否在方块所在的那一列
    
	public boolean same_row(int x) {
		return((x>x_pos.intValue())&&(x<x_pos.intValue()+height));
   }

	//方法same_row(int y)用来判断y是否在方块所在的那一行
	public boolean same_col(int y) {
		return ((y>y_pos.intValue())&&(y<y_pos.intValue()+height));
	}
	
	public boolean toLeft(piece p) {
		
		return (x_pos.intValue()<p.x_pos.intValue());
	}
	public boolean toRight(piece p) {
		return (x_pos.intValue()>p.x_pos.intValue());
	}
	public boolean above(piece p) {
		return (y_pos.intValue()<p.y_pos.intValue());
	}
	public boolean below(piece p) {
		return (y_pos.intValue()>p.y_pos.intValue());
	}
	
	public void xchg(piece blank) {
		Integer temp;
		Image temp_pic;
		temp=value;
		value=blank.value;
		blank.value=temp;
		if(pic!=null){
			temp_pic=pic;
			pic=blank.pic;
			blank.pic=temp_pic;
	    }
		draw();
		blank.draw();
	}
	public void draw(){
	//System.out.println("In draw\n");
	if(piece.value.intValue()==16){
		gc.setColor(Color.black);
		gc.fillRect(x_pos.intValue(), y_pos.intValue(), width, height);
		gc.drawRect(x_pos.intValue(), y_pos.intValue(), width, height);
	}
	else if(obj.level_indicator.level()==0){
		gc.setColor(Color.yellow);
		gc.fillRect(x_pos.intValue(), y_pos.intValue(), width, height);
		gc.setColor(Color.black);
		gc.drawRect(x_pos.intValue(), y_pos.intValue(), width, height);
		gc.drawString(value.toString(), x_pos.intValue()+width/2, y_pos.intValue()+height/2);
		gc.setFont(new Font("Times",Font.BOLD,16));
		gc.drawString(value.toString(), x_pos.intValue()+(width-gc.getFontMetrics().stringWidth(value.toString()))/2,y_pos.intValue()+height/2);
	}
	else
	{
		gc.setColor(Color.black);
		gc.drawRect(x_pos.intValue(), y_pos.intValue(), width, height);
	    gc.drawImage(pic,x_pos.intValue(),y_pos.intValue(),null);
	    if(obj.level_indicator.level()==2){
	    	gc.setColor(Color.yellow);
	    	gc.setFont(new Font("Times",Font.BOLD,16));
	    	gc.drawString(value.toString(), x_pos.intValue()+(width-gc.getFontMetrics().stringWidth(value.toString()))/2,y_pos.intValue()+height/2);
	    }
	}
}
}
	    	
	
	
	
	
	

		
		
		

	
	




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -