4 Replies - 1618 Views - Last Post: 05 February 2013 - 10:13 AM Rate Topic: -----

#1 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

JavaFX painting question

Posted 05 February 2013 - 09:18 AM

I've slowly been trying to migrate towards using JavaFX as opposed to swing. I've read tutorials, coded some basic stuff, etc. I'm not particularly interested in understanding all the CSS FXML yadda yadda stuff. Right now I'm attempting to understand it in a manner similar to Swing (which I'm already familiar with). As I understand it, the levels are Stage -> Scene -> Nodes. Where nodes represent some graphical component that can, but doesn't necessarily, handle input. Buttons, panels, all the normal Swing stuff is basically a node.

I've searched on Google and here on Dream but can't seem to find anything that's even remotely analogous to painting in Swing. I understand of course that JavaFX and Swing are different. However, it doesn't seem an incongruous thought to think that nodes should provide some method akin to paint, paintComponent, repaint, etc. The only one similar ones I found were setGraphic and getGraphic which are methods of a Labeled object. Are these the "sister" methods to the Swing methods, or have I missed the point? Is it possible to paint on a direct subclass of a Node object, similar to the Canvas object? Is the Canvas object what I'm supposed to use?

In Swing, to alter the appearance of a lightweight component I would tinker with one of its paint methods. The overall question here is how would I do this in JavaFX? I realize there are CSS states for rollover, pressed, etc. However, I'm looking for a more fine-grained solution.

Is This A Good Question/Topic? 0
  • +

Replies To: JavaFX painting question

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: JavaFX painting question

Posted 05 February 2013 - 09:36 AM

Yeah I think the whole painting things has been for the most part replaced in JavaFX.

I believe how you apply custom styling to components is through 'skinning', although this is mainly done through CSS now.

That said you can still get the pure Java code version by implementing the Skin interface. You essentially add shapes to the root and apply it to your custom component. I haven't yet tried it but it seems to be the way things like that are done now.

Take a look at this link for a little example

What are you trying to make here though. Are you sure you can't do it in CSS?
Was This Post Helpful? 1
  • +
  • -

#3 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: JavaFX painting question

Posted 05 February 2013 - 09:59 AM

Thanks for that tutorial Ryano121. What I've concluded is that CSS would work if I wanted to make a custom skinned component. This makes sense if you just want to change the visual appearance of your application. What I'm talking about though is having a component change its appearance due to user interaction (not in the standard rollover, click, etc.) or some code-based feature.

Let's say I had 8 flags in my code whose values will vary through the application's lifecycle and I want my custom component to reflect these changes in some visual manner. I don't believe such a dynamic update would be readily achievable with CSS as it's just a styling definition, not a scripting language. Maybe I'm wrong in this?
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: JavaFX painting question

Posted 05 February 2013 - 10:04 AM

It is possible to change the CSS style associated with a component at any time you want through code.

For example if you have CSS classes called "red-text" and "blue-text", in your controller code for your component, you check your flags and assign the CSS appropriate CSS class e.g

if(flagSet) {
   component.getStyleClass().remove("red-text");
   component.getStyleClass().add("blue-text");
}
else {
   component.getStyleClass().remove("blue-text");
   component.getStyleClass().add("red-text");
}


Which will alternate between two completely different CSS styles depending on the current state of your program. It works very well.

This post has been edited by Ryano121: 05 February 2013 - 10:05 AM

Was This Post Helpful? 1
  • +
  • -

#5 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: JavaFX painting question

Posted 05 February 2013 - 10:13 AM

Hmm... that is pretty nifty. Would this work though if I have to paint custom vector shapes like circles, rectangles, etc.?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1