Drawing Java Components On Game Pieces


I’m making a new Vassal game which is based on a free board game I like which is like the Civilisation PC games.

I don’t yet want to announce what game I’m working on but when I do announce it I will create a forum topic for it and I will open source its development.

I’ve decided that I want this game’s unit pieces to have a AttributesOverlay trait which will be responsible for drawing attribute labels and icons on unit pieces having looked up what the attributes for each unit piece should be by obtaining them from this game’s “Unit” enumeration class. This trait will align attribute labels with their icons in a JPanel and then it will draw the contents of the JPanel on to games pieces.

I know I could instead use the Vassal module editor’s property, label and layer traits to display the attributes and their icons: this is how attributes are displayed in the current WIP version of this game. One reason why I want to make this change is because I want as much of this game to be implemented in Java code as possible with the Vassal modules and traits just being responsible for GUI rendering.

I’ve not been able to make the contents of the attributes JPanel be drawn on to game pieces, so I’ve been trying to start by making a JLabel be drawn on game pieces but my trait’s implementation of the Decorator draw() function isn’t drawing the label:

	public void draw(Graphics g, int x, int y, Component obs, double zoom) 
		piece.draw(g, x, y, obs, zoom);
		JLabel test = new JLabel("test");
		test.setLocation(x, y);

It is probably drawing the JLabel, but in the wrong place where you can’t see it. Swing components aren’t really supposed to be used in that way.

Try using the Graphics class directly to draw to see if you can get anything happening:

g.setFont(new Font(“Dialog”, 0, (int) (14 * zoom));
g.drawString(“Hello”, x, y);

Thanks for your advice, I will try doing what you’ve suggested.

I’m now just going to use the Graphics object to draw the attribute labels and icons directly.

I was able to make the JLabel text appear in the piece editor by setting its size to be the size of the piece but I couldn’t prevent it from being drawn at 0,0.

The Graphic object you get is based on the whole map, so the default location for drawing will be 0, 0. If you are going to keep trying to use a JLabel, then try calling setLocation (x, y) on the JLabel before drawing.

Thanks for your help Brent, I did try changing its location with setLocation() and it was still drawn at 0,0.

I’ve ended up still using the label and layer traits to draw the attributes and their icons, with my UnitAttributes trait getting the attribute values from my Unit enum class and then making them available as properties for the labels to display.

Ok. I have written many Java traits that draw stuff on counters. I still think your problem is stemming from trying to use a JLabel to do the drawing, rather than graphics primitives. Also, have you taken the zoom factor into account?