Floodfill with primefaces. Get click coordinate in p: graphicImage

1

I want to make an image painting application using java. As it is for the web environment I chose primefaces as a framework for the vast documentation that exists about it.

In the meantime I did not find any component that was useful in this type of application.

seeing the primefaces showcase vi there is a p: graphicImage component that could solve what I need. But I have no idea how to figure out the click coordinate on such a component. If I could get this information I could process the image and display it painted for the user.

Has anyone done any such applications yet?

Thank you.

    
asked by anonymous 21.07.2014 / 19:14

1 answer

2

The great @BalusC posted a solution for this, but not specific to PrimeFaces. I will transcribe the relevant part below ...

The best way to do this is to put hidden fields with their coordinates, linked to their managed bean properties, and update those values via Javascript / jQuery.

Example:

<h:form>
    <h:graphicImage id="image" name="image1.png">
        <f:ajax event="click" execute="x y" listener="#{bean.listener}" />
    </h:graphicImage>
    <h:inputHidden id="x" value="#{bean.x}" />
    <h:inputHidden id="y" value="#{bean.y}" />
</h:form>

<h:outputScript>
    jQuery("img[id$=':image']").on("mousedown", function(event) {
        var $form = jQuery(this).closest("form");
        $form.find("input[id$=':x']").val(event.pageX);
        $form.find("input[id$=':y']").val(event.pageY);
    });
</h:outputScript>

Note: You need jQuery at least in version 1.7 to use jQuery.on() . In previous versions, use jQuery.bind() .

And Managed Bean would look like this:

@ManagedBean
@RequestScoped
public class Bean {

    private Integer x;
    private Integer y;

    public void listener() {
        System.out.println(x + ", " + y);
    }

    // ...
}

However, in its place I would not use JSF components for this task.

Create a REST Web Service or a simple Servlet that receives the appropriate parameters and returns the image.

Then you can put a normal HTML image ( <img/> ), use jQuery to capture the click and then, at each event, go changing the URL of the image tag, because the Servlet will be called and will return the image updated.

    
22.07.2014 / 16:22