Java3D: changing appearance of a Box

A

Angus Parvis

Hi,

I've written some really ugly code for a game. Basically the game-field
consists of a couple of com.sun.j3d.utils.geometry.Box-objects that are
clicked by the user. Now I figured out how to pick a box, but fail to
change the box's color after it was created.

Here's the code:

public class GameField extends Box {

public final static Color3f EMPTY = new Color3f(0.1f, 0.2f, 0.3f);

public final static Color3f TRIED = new Color3f(0.4f, 0.2f, 0.7f);

public final static Color3f SHIP = new Color3f();

public final static Color3f SHIP_HIT = new Color3f();

Game _game;

Position _position;

private Player _owner;

public GameField(Game game, Player owner, Position position) {
super(1f, 1f, 0.5f, new Appearance());
setCapability(ENABLE_APPEARANCE_MODIFY);
setCapability(GEOMETRY_NOT_SHARED);
Appearance appearance = new Appearance();
ColoringAttributes coloringAttributes = new ColoringAttributes(EMPTY,
ColoringAttributes.NICEST);
appearance.setColoringAttributes(coloringAttributes);
setAppearance(appearance);

_game = game;
_position = position;
_owner = owner;
}

public int hit() throws PositionOutOfBoundsException,
PositionAlreadyTriedException {
int state = Battlefield.EMPTY;
if (_game.getCurrentDefender().equals(_owner)) {
state = _game.attackPosition(_position);
}

if (state == Battlefield.TRIED) {
System.out.println("tried");
Appearance appearance = new Appearance();
ColoringAttributes coloringAttributes = new ColoringAttributes(
TRIED, ColoringAttributes.NICEST);
appearance.setColoringAttributes(coloringAttributes);
setAppearance(appearance);
} else if (state == Battlefield.SHIP_HIT) {
System.out.println("hit!");
}
return state;
}
}

The code works for the constructor where I set the appearance for the
first time, but when i try to change it in the hit-method the app throws
an exception, namely this one:

Exception in thread "AWT-EventQueue-0"
javax.media.j3d.CapabilityNotSetException: Shape3D: no capability to set
appearance
at javax.media.j3d.Shape3D.setAppearance(Shape3D.java:472)
at com.sun.j3d.utils.geometry.Box.setAppearance(Box.java:277)
at battleships.gui3d.GameField.hit(GameField.java:60)
at battleships.gui3d.PickBoxListener.mouseClicked(PickBoxListener.java:49)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

The Box-class is based on a Primitive, so I wonder what's that talk
about a Shape3D. Anyway, I want to change the color of this box, I've
been trying some variations of my approach, googled some groups, but
couldn't find a solution.

Please save a poor guy's nerves. If you know how to solve this, please
post your solution. Thx for any help u can offer.

Greetings,

Angus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top