2011-03-13 77 views
0

我正在構建一個簡單的滑塊遊戲。我使用repaint()方法繪製拼圖圖像的塊,然後在BufferedImage數組(help [empty_row] [empty_col] = null;)的幫助下繪製一個空塊。空塊是您通常在拼圖中使用的空白框。一個簡單的滑塊遊戲(遊戲邏輯和繪畫組件方法)

事情是,我「調用」繪製方法來繪製我的拼圖的所有塊,我有一個空塊。每當用戶點擊它時,我將empty_row,empty_col更改爲鼠標事件的getX(),getY(),以便將空白框移動到所需的位置。

什麼...困惑我在這個難題:)是先前的空白框仍然出現後,我已經畫出了新的。這對你有意義嗎?

With repaint()我希望塊將被重繪,只留下一個空白框。

對此有何看法?這是我在這裏的第二篇文章,我看到人們非常樂意提供幫助。對此,我真的非常感激。預先感謝您的回答。

ZOI

我的代碼從我SlidingBlockPanel類位:

public SlidingBlockPanel(int nc, int nr) 
{ 
    numCols = nc; 
    numRows = nr; 
    addMouseListener(this); 
    SBModel= new SlidingBlockModel(numCols, numRows, "puzzle.jpg"); 
} 

public void mouseClicked(MouseEvent event) 
{ 
    int thisCol = getCol(event.getX()); 
    int thisRow = getRow(event.getY()); 
    System.out.println 
    ("you clicked in row " + thisRow); 
    System.out.println 
    ("you clicked in column " + thisCol); 

    if (SBModel.slideBlock(thisRow,thisCol)==true) 
    repaint(); 

} 


Rectangle getRect(int thisCol, int thisRow) 
{ 
    // if input is out of range, return "null" 
    if(thisCol <0 || thisRow < 0) 
     return null; 
    if(thisCol>=numCols || thisRow>=numRows) 
     return null; 

    // otherwise, make and return the Rectangle 
    int w = getWidth()/numCols; 
    int h = getHeight()/numRows; 

    int x = thisCol*w; 
    int y = thisRow*h; 

    Rectangle myRect = new Rectangle(x,y,w,h); 
    return myRect; 
} 

public void paint(Graphics g) 
{ 
    //g.setColor(Color.gray); 
    g.fillRect(0,0,getWidth(), getHeight()); 
    g.setColor(Color.black); 

    Graphics2D g2 = (Graphics2D)g; 
    // we'll use Graphics2D for it's "drawImage" method this time 

    for (int i = 0;i<numCols;i++) 
    { 
     for(int j = 0;j<numRows;j++) 
     { 
      Rectangle r = getRect(i, j); 
      g2.drawImage(SBModel.getSubimage(i, j),r.x,r.y,r.width,r.height,null); 

     } 
    } 



} 

和SlidingBlockModel類:

進口java.awt.image中。 ; import java.io.; import javax.imageio。 ; import java.awt.Image。; import java.awt.Graphics。*;

類SlidingBlockModel {

BufferedImage original_image; // the image to be cut up into blocks 
int numCols; // number of columns in the grid of blocks 
int numRows; // number of rows in the grid of blocks 
int empty_col=0; // holds the column index of the one empty grid element 
int empty_row=3; // holds the row index of the one empty grid element 
BufferedImage[][] puzzle; // the two '[][]' allows a 2-D array 
          // to be created - each element is an image 



public SlidingBlockModel (int input_numCols, int input_numRows, String filename) { 


String image_filename=filename; 

numCols=input_numCols; 
numRows=input_numRows; 


original_image = null; 

try 
{ 
original_image = ImageIO.read(new File(image_filename)); 
System.out.println("image " + image_filename + " loaded in ok."); 
System.out.println("Width: " + original_image.getWidth() + ", height: " + original_image.getHeight()); 
    } 


catch (Exception e) 
{ 
System.out.println("Sorry - couldn't load in file " + image_filename); 
    } 


//cut up the original image into 'blocks' and 
//assign each of these to the elements of the puzzle 2D array 

puzzle = new BufferedImage[numCols][numRows]; 


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

    for (int j=0;j<numRows;j++){ 

    puzzle[i][j]=getImageRect(i,j); 

    } 

} 

//Initialise the empty block 

puzzle[empty_row][empty_col] = null; 


} 

    //slide the block indicated by the two parameters, if possible 
    boolean slideBlock(int thisCol, int thisRow) { 

     if(thisCol<0 || thisCol>numCols) 
     return false; 

     if (thisRow<0 || thisRow>numRows) 
     return false; 

     if (thisRow==empty_row) { 

      if ((thisCol==empty_col-1) || (thisCol==empty_col+1)) { 
       empty_col=thisCol; 
       puzzle[empty_row][empty_col]=null; 
       return true; 
       } 
     } 

     else 


      if (thisCol==empty_col) { 

      if ((thisRow==empty_row-1) || (thisRow==empty_row+1)) { 
       empty_row=thisRow; 
       puzzle[empty_row][empty_col]=null; 
       return true; 
       } 
     } 



     return false; 

    } 




    //return the BufferedImage for any given grid element 

    BufferedImage getSubimage(int thisCol, int thisRow) { 

    if(thisCol<0 || thisCol>numCols) 
    return null; 
    //if(thisRow<0 || thisRow>=max_num_counters) 

    if (thisRow<0 || thisRow>numRows) 
    return null; 
    else 
    return puzzle[thisCol][thisRow]; 

    } 



    private BufferedImage getImageRect(int thisCol, int thisRow){ 

    // if input is out of range, return "null" 
    if(thisCol <0 || thisRow < 0) 
     return null; 
    if(thisCol>=numCols || thisRow>=numRows) 
     return null; 
    else { 

    // otherwise, make and return the Rectangle 
    int w = original_image.getWidth()/numCols; 
    int h = original_image.getHeight()/numRows; 

    int x = thisCol*w; 
    int y = thisRow*h; 

    BufferedImage myRect; 

    myRect=original_image.getSubimage(x,y,w,h); 

    return myRect; 
    } 
} 





public static void main (String[] args) { 

} 

}

+0

你能告訴我們一些你的代碼嗎?順便說一句,我假設這是Java代碼? – 2011-03-13 14:00:24

+0

哦,對不起查克!你應該假設這是Java代碼。 :) – 2011-03-13 14:14:19

+0

但丁,那是重繪()做的。還是我誤會了?看看我的paint()方法。 – 2011-03-13 14:21:37

回答

1

我覺得你的問題是下一個:

if (SBModel.slideBlock(thisRow,thisCol)==true) 
repaint(); 

你打電話給你的幻燈片功能,然後重新繪製(至極是不是所有的錯),但在滑動功能中,您只需移動空白部分,在任何時候您都可以用圖像滑動該部分。在滑動功能中,您需要先將圖像移動到圖像上,然後移動空白圖像,然後重新繪製。

+0

True Chuck,謝謝。這就是我現在正在做的事情。 :) – 2011-03-13 15:49:28

+0

@Zoi很高興我能幫忙 – 2011-03-13 16:14:55