Skip to main content

Java IconImage or Icon to JavaFX

4 replies [Last post]
mishela
Offline
Joined: 2007-08-18

Can somebody please tell me how do I paint a java Icon or Image Icon or JavaFX Image in JavaFX? I have this in Java:

//>>>>>class CardIcon
public class CardIcon {
public static Icon getIcon(int value, Suit suit) {
if (value == -1) {
return new ImageIcon("cards/" + suit.getFile() + ".gif");
} else {
return new ImageIcon("cards/" + value + suit.getFile() + ".gif");
}
}
public static String getPath(int value, Suit suit) {
if (value == -1) {
return "cards/" + suit.getFile() + ".gif";
} else {
return "cards/" + value + suit.getFile() + ".gif";
}
}
}

the method getIcon gets value and suit of a specific card and it is painted in another class:

//>>>>>class FreeHomecellPanel
public class FreeHomecellPanel extends JPanel implements MouseListener, PropertyChangeListener {

//... some code

public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setSize(400, 96);
CardIcon.getIcon(someValue, someSuit).paintIcon(this, g, x, y);
}

//...blaahblaah some other code
}

I need this class to do in JavaFX. I need the cards to react to mouseClicked events. Can somebody tell me how do I do this please? Thanks forward.

null

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
alexsch
Offline
Joined: 2007-07-12

Try this:

[code]
import javafx.ui.*;
import javafx.ui.canvas.*;
import java.lang.System;

Frame{
width: 300
height: 300
title: "Image Example"
content: Canvas{
content: ImageView{
image: Image{ url: "{__DIR__}/image.png"}
onMouseClicked: function(e:CanvasMouseEvent){
System.out.println("Mouse Clicked!");
}
}
}
visible: true
}
[/code]

mishela
Offline
Joined: 2007-08-18

I have it, thanks :)
and one more question.. How do I get width of Frame in the body of it? Smth like:
Frame {width: 600, height: 400
content: Canvas {
onMouseMoved: function(e:MouseEvent) {if(e.x But this doesn't work.

tball
Offline
Joined: 2003-06-10

[b]var f =[/b] Frame {
width: 600, height: 400
content: Canvas {
onMouseMoved: function(e:MouseEvent) {
if (e.x < f.width/2 ...

mishela
Offline
Joined: 2007-08-18

thanks :)