Create account / Log in

Drawing Java Components On Game Pieces

Support for existing Vassal modules. Ask here if you need help with a specific module.

Moderators: Tim M, uckelman

Drawing Java Components On Game Pieces

Postby Joe12 » May 7th, 2014, 3:22 pm

Hi,

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:
Code: Select all
@Override
   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);
      test.paint(g);
   }
Joe12
 
Posts: 13
Joined: May 5th, 2012, 5:45 pm

Re: Drawing Java Components On Game Pieces

Postby Brent Easton » May 7th, 2014, 10:17 pm

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.setColor(Color.red);
g.setFont(new Font("Dialog", 0, (int) (14 * zoom));
g.drawString("Hello", x, y);
User avatar
Brent Easton
 
Posts: 2757
Joined: December 21st, 2007, 3:06 am
Location: Berry, NSW, Australia

Re: Drawing Java Components On Game Pieces

Postby Joe12 » May 9th, 2014, 1:49 pm

Thanks for your advice, I will try doing what you've suggested.
Joe12
 
Posts: 13
Joined: May 5th, 2012, 5:45 pm

Re: Drawing Java Components On Game Pieces

Postby Joe12 » May 9th, 2014, 5:18 pm

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.
Joe12
 
Posts: 13
Joined: May 5th, 2012, 5:45 pm

Re: Drawing Java Components On Game Pieces

Postby Brent Easton » May 9th, 2014, 10:20 pm

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.
User avatar
Brent Easton
 
Posts: 2757
Joined: December 21st, 2007, 3:06 am
Location: Berry, NSW, Australia

Re: Drawing Java Components On Game Pieces

Postby Joe12 » May 11th, 2014, 11:50 am

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.
Joe12
 
Posts: 13
Joined: May 5th, 2012, 5:45 pm

Re: Drawing Java Components On Game Pieces

Postby Brent Easton » May 12th, 2014, 1:37 am

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?
User avatar
Brent Easton
 
Posts: 2757
Joined: December 21st, 2007, 3:06 am
Location: Berry, NSW, Australia


Return to Module Support

Who is online

Users browsing this forum: No registered users and 0 guests

cron