Domanda

I am new to programming and in the process of working my way through a Java book and I am trying to learn about Swing and how to create simple GUIs etc.

I am trying to create a puzzle game that holds 9 tiles which are pieces of a puzzle. I have created a buffered image from which i am using the getSubImage method but i cannot figure out how to place each tile in the correct position, I know it should be a simple task but i just keep on creating the sub image in the same position - I am not sure how to move each piece into the correct location. I understand this is simple logic and i should know this!

    for (int i = 0; i < n; i++) 
     {
     for (int j = 0; j < n; j++) 
       {
         BufferedImage b= bimg.getSubimage(j * w, i * h, w, h);     
         g.drawImage(b,0,0,w,h,this);   //0,0 are the problem                                                        
       }   
    }
È stato utile?

Soluzione

It looks like you already had it partially down when you set the image:

BufferedImage b= bimg.getSubimage(j * w, i * h, w, h);

I'm referring to j*w and i*h

So why not draw them in the correct spot as well?

Changing:

g.drawImage(b,0,0,w,h,this);

Into:

g.drawImage(b, j*w , i*h ,w,h,this);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top