GamePiece image

Is it possible to access the image file of a GamePiece via custom code (and also manipulate that image)?

Something like:

p is a variable of type GamePiece

Image i = (Image) p.getProperty(“Image”); (this does not actually work)
BufferedImage bi = new BufferedImage(i.getWidth(), i.getHeight(), BufferedImage.TYPE_INT_ARGB);

Can I get the image and then can I use the BufferedImage bi to change color pixel by pixel?

Thus spake drimmer:

Is it possible to access the image file of a GamePiece via custom code
(and also manipulate that image)?

Something like:

p is a variable of type GamePiece

Image i = (Image) p.getProperty(“Image”); (this does not actually work)
BufferedImage bi = new BufferedImage(i.getWidth(), i.getHeight(),
BufferedImage.TYPE_INT_ARGB);

Can I get the image and then can I use the BufferedImage bi to change
color pixel by pixel?

You can get the piece image via BasicPiece. However, you MUST NOT
alter the image your get that way. You have to make a copy of the
image and modify that. Modifying the image directly will interefere
with image caching.


J.

J.

Thanks for your earlier answer on this. I am coming back to it now as I work on our draggable overlays in VASL. I cannot find a way to get from GamePiece p to BasicPiece. I have tried setting properties. I have scanned the properties of p when running in my IDE. I cannot see how access BasicPiece in code.

Apologies for having to ask for help so often!

Doug

If you have GamePiece p then it may either be a BasicPiece or equally likely a Decorator (Trait) that’s attached outside of one. It could also esoterically be a Stack or Deck, but those you’d probably ignore in your context.

So you’d need an algorithm like…

while (p instanceof Decorator) {
  p = ((Decorator)p).getInner(); // Traverse inwards toward BasicPiece
}
   
if (p instanceof BasicPiece) { // Make sure we didn't start with a Stack or Deck (or null)
  final BasicPiece bp = (BasicPiece)p;
  // Now do whatever you want with the basic piece
}

This is assuming that the image you’re looking for is actually IN the BasicPiece (as opposed to being in a Layer trait a.k.a. the Embellishment class, which would be one of the decorators along the way). If you have an unknown GamePiece and you need to get to the outermost Decorator to traverse inward, then it’s something like:

p = Decorator.getOutermost(p); // Start all the way at outside

while (p instanceof Decorator) {
  if (p instanceof Embellishment) {
    // It's a "Layer" trait, so it might contain the actual image of the piece
  } 

  p = ((Decorator)p).getInner(); // Traverse inwards toward BasicPiece
}

// Possibly check for BasicPiece image if didn't find one in an embellishment

I’m mentioning the piece image just because of your original question at the top of the thread. So if you’re looking for BasicPiece (or something along the way) for some other reason now then you can ignore the specifics about images but the general structure will be the same.

Hope this helps,

Brian

p.s. I replied to your message about the Chatter, in case you haven’t seen it.

You could also call Decorator.getInnermost(GamePiece p) to go straight to the bottom of the stack.

1 Like

Oh, right. That’s literally what that does, isn’t it.

public static GamePiece getInnermost(GamePiece p) {
  while (p instanceof Decorator) {
    p = ((Decorator) p).piece;
  }
  return p;
}
1 Like