Mouse Listener

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Mouse Listener

cnietocoria
Dear all,

I was wondering, is it possible to add a mouse listener. My idea is when I perform a double click with my mouse over the agent to call a different pop-up windows. In this pop-up I would like to plot the variable values (that I want to see) in a chart. 

Is it possible? Maybe I could override some repast code in the display. However, I do not know where this classes are implemented.

Can someone help me?

Thanks in advance,

Cesar

--
--------------------------------------------------------------
Ing. Cesar Augusto Nieto Coria


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mouse Listener

Tatara, Eric R.

You can add a ProbeListener to the display using the GUI Registry.   This is a bit clunky since the GUI registry is not really designed to be accessed via user code, but you can get access to individual displays if you add something like the following to your context builder or model initializer:

 

RunEnvironment.getInstance().getCurrentSchedule().schedule(

                           ScheduleParameters.createOneTime(0), new IAction(){

 

                     @Override

                     public void execute() {

                           List<IDisplay> displays = RunState.getInstance().getGUIRegistry().getDisplays();

                          

                           for (IDisplay display : displays){

                                  display.addProbeListener(new MyProbeListener());

                                 

                           }

                     }

              });

 

The critical part is to execute this code at tick 0 or later since the displays don’t yet exist when the context builder is called.  This example will add an instance of a custom ProbeListener to every display.  You can set which displays have the ProbeListener added by checking the IDisplay instances to say only add it to a certain display type (e.g. 2D, 3D, GIS).  There’s unfortunately no easy way to further specify (e.g. display name) display properties since the GUI registry is fairly restricted access.  Your ProbeListner implementation should look something like:

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object o = agents.get(0);  // First agent

             

              JOptionPane.showMessageDialog(null, "Hi from " + o.toString(), "Probe",

                           JOptionPane.INFORMATION_MESSAGE);

             

       }

 

}

 

And then just change the objectProbed() method to perform the action you want.

 

eric

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Tuesday, June 6, 2017 9:24 AM
To: [hidden email]
Subject: [Repast-interest] Mouse Listener

 

Dear all,

I was wondering, is it possible to add a mouse listener. My idea is when I perform a double click with my mouse over the agent to call a different pop-up windows. In this pop-up I would like to plot the variable values (that I want to see) in a chart. 

 

Is it possible? Maybe I could override some repast code in the display. However, I do not know where this classes are implemented.

Can someone help me?

Thanks in advance,

Cesar


--

--------------------------------------------------------------
Ing. Cesar Augusto Nieto Coria

 


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mouse Listener

cnietocoria
Dear Eric,

Thanks a lot, this worked! Well my idea is when I perform double click open a JPanel, in it i would like to plot one or two charts. This charts are like the ones with RS can be displayed. Now I'm learning about JFreeChart. Do you think that is possible to create something like what I described?

Other question about the display. There is a way to switch off the regular action of double click? This is just in general because I might want to personalize what is displaying when the double click is performed. 

Thanks again,

Cesar

On Fri, Jun 9, 2017 at 3:08 PM, Tatara, Eric R. <[hidden email]> wrote:

You can add a ProbeListener to the display using the GUI Registry.   This is a bit clunky since the GUI registry is not really designed to be accessed via user code, but you can get access to individual displays if you add something like the following to your context builder or model initializer:

 

RunEnvironment.getInstance().getCurrentSchedule().schedule(

                           ScheduleParameters.createOneTime(0), new IAction(){

 

                     @Override

                     public void execute() {

                           List<IDisplay> displays = RunState.getInstance().getGUIRegistry().getDisplays();

                          

                           for (IDisplay display : displays){

                                  display.addProbeListener(new MyProbeListener());

                                 

                           }

                     }

              });

 

The critical part is to execute this code at tick 0 or later since the displays don’t yet exist when the context builder is called.  This example will add an instance of a custom ProbeListener to every display.  You can set which displays have the ProbeListener added by checking the IDisplay instances to say only add it to a certain display type (e.g. 2D, 3D, GIS).  There’s unfortunately no easy way to further specify (e.g. display name) display properties since the GUI registry is fairly restricted access.  Your ProbeListner implementation should look something like:

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object o = agents.get(0);  // First agent

             

              JOptionPane.showMessageDialog(null, "Hi from " + o.toString(), "Probe",

                           JOptionPane.INFORMATION_MESSAGE);

             

       }

 

}

 

And then just change the objectProbed() method to perform the action you want.

 

eric

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Tuesday, June 6, 2017 9:24 AM
To: [hidden email]
Subject: [Repast-interest] Mouse Listener

 

Dear all,

I was wondering, is it possible to add a mouse listener. My idea is when I perform a double click with my mouse over the agent to call a different pop-up windows. In this pop-up I would like to plot the variable values (that I want to see) in a chart. 

 

Is it possible? Maybe I could override some repast code in the display. However, I do not know where this classes are implemented.

Can someone help me?

Thanks in advance,

Cesar


--

--------------------------------------------------------------
Ing. Cesar Augusto Nieto Coria

 




--
--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria
skype: cnietocoria


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mouse Listener

Tatara, Eric R.

Using JFreeChart is a great idea and the libraries are already included with Repast.   You can disable the default probe behavior on a display by assigning a new ProbeListenerSupport instance just before adding your new ProbeListener :

 

((AbstractDisplay)display).setProbeSupport(new ProbeListenerSupport());

display.addProbeListener(new MyProbeListener());

 

 

If you would like to restore the default probe behavior you could call getProbeSupport() on the display before assigning the new ProbeListenerSupport ,  store it somewhere and then assign it again later.

 

Here is an updated MyProbeListener that adds a sample JFreeChart to the Repast display.  If you don’t want to dock the display in the Repast window, you can always just add the chart to a JFrame which will appear as a floating window separate from the Repast GUI:

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object o = agents.get(0);  // First agent

             

//            JOptionPane.showMessageDialog(null, "Hi from " + o.toString(), "Probe",

//                         JOptionPane.INFORMATION_MESSAGE);

//           

              // create a dataset...

              DefaultPieDataset dataset = new DefaultPieDataset();

              dataset.setValue("Category 1", 43.2);

              dataset.setValue("Category 2", 27.9);

              dataset.setValue("Category 3", 79.5);

             

              JFreeChart chart = ChartFactory.createPieChart(

                           "Sample Pie Chart",

                           dataset,

                           true,    // legend?

                           true,    // tooltips?

                           false    // URLs?

                           );

 

             

              DockableFrame view = RSApplication.getRSApplicationInstance().getGui().addVizualization("MyChart", new ChartPanel(chart));

             

              view.toFront();

       }

 

 

 

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Monday, June 12, 2017 3:43 AM
To: Tatara, Eric R. <[hidden email]>
Cc: [hidden email]
Subject: Re: [Repast-interest] Mouse Listener

 

Dear Eric,

Thanks a lot, this worked! Well my idea is when I perform double click open a JPanel, in it i would like to plot one or two charts. This charts are like the ones with RS can be displayed. Now I'm learning about JFreeChart. Do you think that is possible to create something like what I described?

Other question about the display. There is a way to switch off the regular action of double click? This is just in general because I might want to personalize what is displaying when the double click is performed. 

Thanks again,

Cesar

 

On Fri, Jun 9, 2017 at 3:08 PM, Tatara, Eric R. <[hidden email]> wrote:

You can add a ProbeListener to the display using the GUI Registry.   This is a bit clunky since the GUI registry is not really designed to be accessed via user code, but you can get access to individual displays if you add something like the following to your context builder or model initializer:

 

RunEnvironment.getInstance().getCurrentSchedule().schedule(

                           ScheduleParameters.createOneTime(0), new IAction(){

 

                     @Override

                     public void execute() {

                           List<IDisplay> displays = RunState.getInstance().getGUIRegistry().getDisplays();

                          

                           for (IDisplay display : displays){

                                  display.addProbeListener(new MyProbeListener());

                                 

                           }

                     }

              });

 

The critical part is to execute this code at tick 0 or later since the displays don’t yet exist when the context builder is called.  This example will add an instance of a custom ProbeListener to every display.  You can set which displays have the ProbeListener added by checking the IDisplay instances to say only add it to a certain display type (e.g. 2D, 3D, GIS).  There’s unfortunately no easy way to further specify (e.g. display name) display properties since the GUI registry is fairly restricted access.  Your ProbeListner implementation should look something like:

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object o = agents.get(0);  // First agent

             

              JOptionPane.showMessageDialog(null, "Hi from " + o.toString(), "Probe",

                           JOptionPane.INFORMATION_MESSAGE);

             

       }

 

}

 

And then just change the objectProbed() method to perform the action you want.

 

eric

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Tuesday, June 6, 2017 9:24 AM
To: [hidden email]
Subject: [Repast-interest] Mouse Listener

 

Dear all,

I was wondering, is it possible to add a mouse listener. My idea is when I perform a double click with my mouse over the agent to call a different pop-up windows. In this pop-up I would like to plot the variable values (that I want to see) in a chart. 

 

Is it possible? Maybe I could override some repast code in the display. However, I do not know where this classes are implemented.

Can someone help me?

Thanks in advance,

Cesar


--

--------------------------------------------------------------
Ing. Cesar Augusto Nieto Coria

 




--

--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria

skype: cnietocoria

email: [hidden email]

 


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mouse Listener

cnietocoria
Excellent! It work very well. Although, the pie chart example returns me some errors, I manage to create other chart type. Thanks a lot!!

This generated me other question, Can I add more than one probe listener? For example, one for the double click behavior and other for drag my agents into other location. Lets suppose that I want to relocate my agents. My agents are in a discrete grid, with a default position, but maybe I would like to relocate then using the mouse (drag and drop). Is it possible?





On Mon, Jun 12, 2017 at 1:59 PM, Tatara, Eric R. <[hidden email]> wrote:

Using JFreeChart is a great idea and the libraries are already included with Repast.   You can disable the default probe behavior on a display by assigning a new ProbeListenerSupport instance just before adding your new ProbeListener :

 

((AbstractDisplay)display).setProbeSupport(new ProbeListenerSupport());

display.addProbeListener(new MyProbeListener());

 

 

If you would like to restore the default probe behavior you could call getProbeSupport() on the display before assigning the new ProbeListenerSupport ,  store it somewhere and then assign it again later.

 

Here is an updated MyProbeListener that adds a sample JFreeChart to the Repast display.  If you don’t want to dock the display in the Repast window, you can always just add the chart to a JFrame which will appear as a floating window separate from the Repast GUI:

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object o = agents.get(0);  // First agent

             

//            JOptionPane.showMessageDialog(null, "Hi from " + o.toString(), "Probe",

//                         JOptionPane.INFORMATION_MESSAGE);

//           

              // create a dataset...

              DefaultPieDataset dataset = new DefaultPieDataset();

              dataset.setValue("Category 1", 43.2);

              dataset.setValue("Category 2", 27.9);

              dataset.setValue("Category 3", 79.5);

             

              JFreeChart chart = ChartFactory.createPieChart(

                           "Sample Pie Chart",

                           dataset,

                           true,    // legend?

                           true,    // tooltips?

                           false    // URLs?

                           );

 

             

              DockableFrame view = RSApplication.getRSApplicationInstance().getGui().addVizualization("MyChart", new ChartPanel(chart));

             

              view.toFront();

       }

 

 

 

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Monday, June 12, 2017 3:43 AM
To: Tatara, Eric R. <[hidden email]>
Cc: [hidden email]
Subject: Re: [Repast-interest] Mouse Listener

 

Dear Eric,

Thanks a lot, this worked! Well my idea is when I perform double click open a JPanel, in it i would like to plot one or two charts. This charts are like the ones with RS can be displayed. Now I'm learning about JFreeChart. Do you think that is possible to create something like what I described?

Other question about the display. There is a way to switch off the regular action of double click? This is just in general because I might want to personalize what is displaying when the double click is performed. 

Thanks again,

Cesar

 

On Fri, Jun 9, 2017 at 3:08 PM, Tatara, Eric R. <[hidden email]> wrote:

You can add a ProbeListener to the display using the GUI Registry.   This is a bit clunky since the GUI registry is not really designed to be accessed via user code, but you can get access to individual displays if you add something like the following to your context builder or model initializer:

 

RunEnvironment.getInstance().getCurrentSchedule().schedule(

                           ScheduleParameters.createOneTime(0), new IAction(){

 

                     @Override

                     public void execute() {

                           List<IDisplay> displays = RunState.getInstance().getGUIRegistry().getDisplays();

                          

                           for (IDisplay display : displays){

                                  display.addProbeListener(new MyProbeListener());

                                 

                           }

                     }

              });

 

The critical part is to execute this code at tick 0 or later since the displays don’t yet exist when the context builder is called.  This example will add an instance of a custom ProbeListener to every display.  You can set which displays have the ProbeListener added by checking the IDisplay instances to say only add it to a certain display type (e.g. 2D, 3D, GIS).  There’s unfortunately no easy way to further specify (e.g. display name) display properties since the GUI registry is fairly restricted access.  Your ProbeListner implementation should look something like:

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object o = agents.get(0);  // First agent

             

              JOptionPane.showMessageDialog(null, "Hi from " + o.toString(), "Probe",

                           JOptionPane.INFORMATION_MESSAGE);

             

       }

 

}

 

And then just change the objectProbed() method to perform the action you want.

 

eric

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Tuesday, June 6, 2017 9:24 AM
To: [hidden email]
Subject: [Repast-interest] Mouse Listener

 

Dear all,

I was wondering, is it possible to add a mouse listener. My idea is when I perform a double click with my mouse over the agent to call a different pop-up windows. In this pop-up I would like to plot the variable values (that I want to see) in a chart. 

 

Is it possible? Maybe I could override some repast code in the display. However, I do not know where this classes are implemented.

Can someone help me?

Thanks in advance,

Cesar


--

--------------------------------------------------------------
Ing. Cesar Augusto Nieto Coria

 




--

--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria

skype: cnietocoria

email: [hidden email]

 




--
--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria
skype: cnietocoria


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mouse Listener

Tatara, Eric R.

It might be possible to add different mouse listeners, depending on the display type.  Which type of display (2D, 3D, GIS)  are you working with?

 

eric

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Tuesday, June 13, 2017 3:16 AM
To: Tatara, Eric R. <[hidden email]>
Cc: [hidden email]
Subject: Re: [Repast-interest] Mouse Listener

 

Excellent! It work very well. Although, the pie chart example returns me some errors, I manage to create other chart type. Thanks a lot!!

This generated me other question, Can I add more than one probe listener? For example, one for the double click behavior and other for drag my agents into other location. Lets suppose that I want to relocate my agents. My agents are in a discrete grid, with a default position, but maybe I would like to relocate then using the mouse (drag and drop). Is it possible?

 

 

 

On Mon, Jun 12, 2017 at 1:59 PM, Tatara, Eric R. <[hidden email]> wrote:

Using JFreeChart is a great idea and the libraries are already included with Repast.   You can disable the default probe behavior on a display by assigning a new ProbeListenerSupport instance just before adding your new ProbeListener :

 

((AbstractDisplay)display).setProbeSupport(new ProbeListenerSupport());

display.addProbeListener(new MyProbeListener());

 

 

If you would like to restore the default probe behavior you could call getProbeSupport() on the display before assigning the new ProbeListenerSupport ,  store it somewhere and then assign it again later.

 

Here is an updated MyProbeListener that adds a sample JFreeChart to the Repast display.  If you don’t want to dock the display in the Repast window, you can always just add the chart to a JFrame which will appear as a floating window separate from the Repast GUI:

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object o = agents.get(0);  // First agent

             

//            JOptionPane.showMessageDialog(null, "Hi from " + o.toString(), "Probe",

//                         JOptionPane.INFORMATION_MESSAGE);

//           

              // create a dataset...

              DefaultPieDataset dataset = new DefaultPieDataset();

              dataset.setValue("Category 1", 43.2);

              dataset.setValue("Category 2", 27.9);

              dataset.setValue("Category 3", 79.5);

             

              JFreeChart chart = ChartFactory.createPieChart(

                           "Sample Pie Chart",

                           dataset,

                           true,    // legend?

                           true,    // tooltips?

                           false    // URLs?

                           );

 

             

              DockableFrame view = RSApplication.getRSApplicationInstance().getGui().addVizualization("MyChart", new ChartPanel(chart));

             

              view.toFront();

       }

 

 

 

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Monday, June 12, 2017 3:43 AM
To: Tatara, Eric R. <[hidden email]>
Cc: [hidden email]
Subject: Re: [Repast-interest] Mouse Listener

 

Dear Eric,

Thanks a lot, this worked! Well my idea is when I perform double click open a JPanel, in it i would like to plot one or two charts. This charts are like the ones with RS can be displayed. Now I'm learning about JFreeChart. Do you think that is possible to create something like what I described?

Other question about the display. There is a way to switch off the regular action of double click? This is just in general because I might want to personalize what is displaying when the double click is performed. 

Thanks again,

Cesar

 

On Fri, Jun 9, 2017 at 3:08 PM, Tatara, Eric R. <[hidden email]> wrote:

You can add a ProbeListener to the display using the GUI Registry.   This is a bit clunky since the GUI registry is not really designed to be accessed via user code, but you can get access to individual displays if you add something like the following to your context builder or model initializer:

 

RunEnvironment.getInstance().getCurrentSchedule().schedule(

                           ScheduleParameters.createOneTime(0), new IAction(){

 

                     @Override

                     public void execute() {

                           List<IDisplay> displays = RunState.getInstance().getGUIRegistry().getDisplays();

                          

                           for (IDisplay display : displays){

                                  display.addProbeListener(new MyProbeListener());

                                 

                           }

                     }

              });

 

The critical part is to execute this code at tick 0 or later since the displays don’t yet exist when the context builder is called.  This example will add an instance of a custom ProbeListener to every display.  You can set which displays have the ProbeListener added by checking the IDisplay instances to say only add it to a certain display type (e.g. 2D, 3D, GIS).  There’s unfortunately no easy way to further specify (e.g. display name) display properties since the GUI registry is fairly restricted access.  Your ProbeListner implementation should look something like:

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object o = agents.get(0);  // First agent

             

              JOptionPane.showMessageDialog(null, "Hi from " + o.toString(), "Probe",

                           JOptionPane.INFORMATION_MESSAGE);

             

       }

 

}

 

And then just change the objectProbed() method to perform the action you want.

 

eric

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Tuesday, June 6, 2017 9:24 AM
To: [hidden email]
Subject: [Repast-interest] Mouse Listener

 

Dear all,

I was wondering, is it possible to add a mouse listener. My idea is when I perform a double click with my mouse over the agent to call a different pop-up windows. In this pop-up I would like to plot the variable values (that I want to see) in a chart. 

 

Is it possible? Maybe I could override some repast code in the display. However, I do not know where this classes are implemented.

Can someone help me?

Thanks in advance,

Cesar


--

--------------------------------------------------------------
Ing. Cesar Augusto Nieto Coria

 




--

--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria

skype: cnietocoria

email: [hidden email]

 




--

--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria

skype: cnietocoria

email: [hidden email]

 


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mouse Listener

Tatara, Eric R.

Hi Cesar,

 

The 2D display uses some pretty sophisticated OpenGL code and unfortunately the moue event related parts are buried so deep that it’s not possible to access them via Repast model code.  However, you can still do a lot with the double click ProbleListener.  You might consider attaching something like a menu selection to the ProbeListener that allows you to select from multiple options.  The example code below creates a popup menu on double clicking an agent and lets you move the agent by specifying the agent’s location in space.  This example uses a very simple dialog, but a more complex GUI panel could be used instead.  Note that if you are moving agents with this approach you will probably need to move the agent in all projections (space or grid) in which it resides.  The example code here explicitly calls the space.moveTo() method to show what’s needed, but I would recommend internalizing the movement code to the agent class and let the agent manage how it moves in the various projections, e.g. have a public getter and setter for the agent’s x,y coordinates so that you don’t need to reference contexts and projections in the ProbeListener code.

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object agent = agents.get(0);  // First agent

             

              JPopupMenu menu = new JPopupMenu();

 

           JMenuItem item1 = new JMenuItem("Action 1");

           JMenuItem item2 = new JMenuItem("Action 2");

          

           menu.add(item1);

           menu.add(item2);

             

           item1.addActionListener(new ActionListener() {

                     @Override

                     public void actionPerformed(ActionEvent e) {

                           showMoveDialog(agent, display);

                     }

              });

          

           item2.addActionListener(new ActionListener() {

                     @Override

                     public void actionPerformed(ActionEvent e) {

                           System.out.println("Action 2");

                          

                     }

              });

         

           

           JFrame frame = RSApplication.getRSApplicationInstance().getGui().getFrame();

          

           Point loc = frame.getMousePosition();

          

           menu.show(frame, (int)loc.getX(), (int)loc.getY());

             

       }

      

       /**

       * Create a simple dialog that lets the user change the agent's coordinates.

       *

        * @param agent

       * @param display

       */

       public void showMoveDialog(Object agent, IDisplay display){

             

              // TODO any other projections the agent is in may need to be updated

              Context context = ContextUtils.getContext(agent);

              ContinuousSpace space = (ContinuousSpace)context.getProjection("space");

             

              double x = space.getLocation(agent).getX();

              double y = space.getLocation(agent).getY();

             

              JTextField xField = new JTextField(String.valueOf(x));

              JTextField yField = new JTextField(String.valueOf(y));

             

              final JComponent[] inputs = new JComponent[] {

                      new JLabel("x coordinate"),

                      xField,

                      new JLabel("y coordinate"),

                      yField

              };

             

              String title = "Coordinates for " + agent.toString();

              int result = JOptionPane.showConfirmDialog(null, inputs, title,

                           JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

             

              if (result == JOptionPane.OK_OPTION) {

                 

                     double newX = Double.valueOf(xField.getText());

                     double newY = Double.valueOf(yField.getText());

                    

                     // Move only if coords change

                     if (newX != x || newY != y){           

                           space.moveTo(agent, newX, newY);

                          

                           display.update(); // to refresh the display

                           display.render();

                     }

                    

              } else {

                 // cancelled

              }

       }

}

 

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Wednesday, June 14, 2017 3:43 AM
To: Tatara, Eric R. <[hidden email]>
Subject: Re: [Repast-interest] Mouse Listener

 

I'm working with a 2D display.

 

On Tue, Jun 13, 2017 at 12:33 PM, Tatara, Eric R. <[hidden email]> wrote:

It might be possible to add different mouse listeners, depending on the display type.  Which type of display (2D, 3D, GIS)  are you working with?

 

eric

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Tuesday, June 13, 2017 3:16 AM


To: Tatara, Eric R. <[hidden email]>
Cc: [hidden email]
Subject: Re: [Repast-interest] Mouse Listener

 

Excellent! It work very well. Although, the pie chart example returns me some errors, I manage to create other chart type. Thanks a lot!!

This generated me other question, Can I add more than one probe listener? For example, one for the double click behavior and other for drag my agents into other location. Lets suppose that I want to relocate my agents. My agents are in a discrete grid, with a default position, but maybe I would like to relocate then using the mouse (drag and drop). Is it possible?

 

 

 

On Mon, Jun 12, 2017 at 1:59 PM, Tatara, Eric R. <[hidden email]> wrote:

Using JFreeChart is a great idea and the libraries are already included with Repast.   You can disable the default probe behavior on a display by assigning a new ProbeListenerSupport instance just before adding your new ProbeListener :

 

((AbstractDisplay)display).setProbeSupport(new ProbeListenerSupport());

display.addProbeListener(new MyProbeListener());

 

 

If you would like to restore the default probe behavior you could call getProbeSupport() on the display before assigning the new ProbeListenerSupport ,  store it somewhere and then assign it again later.

 

Here is an updated MyProbeListener that adds a sample JFreeChart to the Repast display.  If you don’t want to dock the display in the Repast window, you can always just add the chart to a JFrame which will appear as a floating window separate from the Repast GUI:

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object o = agents.get(0);  // First agent

             

//            JOptionPane.showMessageDialog(null, "Hi from " + o.toString(), "Probe",

//                         JOptionPane.INFORMATION_MESSAGE);

//           

              // create a dataset...

              DefaultPieDataset dataset = new DefaultPieDataset();

              dataset.setValue("Category 1", 43.2);

              dataset.setValue("Category 2", 27.9);

              dataset.setValue("Category 3", 79.5);

             

              JFreeChart chart = ChartFactory.createPieChart(

                           "Sample Pie Chart",

                           dataset,

                           true,    // legend?

                           true,    // tooltips?

                           false    // URLs?

                           );

 

             

              DockableFrame view = RSApplication.getRSApplicationInstance().getGui().addVizualization("MyChart", new ChartPanel(chart));

             

              view.toFront();

       }

 

 

 

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Monday, June 12, 2017 3:43 AM
To: Tatara, Eric R. <[hidden email]>
Cc: [hidden email]
Subject: Re: [Repast-interest] Mouse Listener

 

Dear Eric,

Thanks a lot, this worked! Well my idea is when I perform double click open a JPanel, in it i would like to plot one or two charts. This charts are like the ones with RS can be displayed. Now I'm learning about JFreeChart. Do you think that is possible to create something like what I described?

Other question about the display. There is a way to switch off the regular action of double click? This is just in general because I might want to personalize what is displaying when the double click is performed. 

Thanks again,

Cesar

 

On Fri, Jun 9, 2017 at 3:08 PM, Tatara, Eric R. <[hidden email]> wrote:

You can add a ProbeListener to the display using the GUI Registry.   This is a bit clunky since the GUI registry is not really designed to be accessed via user code, but you can get access to individual displays if you add something like the following to your context builder or model initializer:

 

RunEnvironment.getInstance().getCurrentSchedule().schedule(

                           ScheduleParameters.createOneTime(0), new IAction(){

 

                     @Override

                     public void execute() {

                           List<IDisplay> displays = RunState.getInstance().getGUIRegistry().getDisplays();

                          

                           for (IDisplay display : displays){

                                  display.addProbeListener(new MyProbeListener());

                                 

                           }

                     }

              });

 

The critical part is to execute this code at tick 0 or later since the displays don’t yet exist when the context builder is called.  This example will add an instance of a custom ProbeListener to every display.  You can set which displays have the ProbeListener added by checking the IDisplay instances to say only add it to a certain display type (e.g. 2D, 3D, GIS).  There’s unfortunately no easy way to further specify (e.g. display name) display properties since the GUI registry is fairly restricted access.  Your ProbeListner implementation should look something like:

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object o = agents.get(0);  // First agent

             

              JOptionPane.showMessageDialog(null, "Hi from " + o.toString(), "Probe",

                           JOptionPane.INFORMATION_MESSAGE);

             

       }

 

}

 

And then just change the objectProbed() method to perform the action you want.

 

eric

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Tuesday, June 6, 2017 9:24 AM
To: [hidden email]
Subject: [Repast-interest] Mouse Listener

 

Dear all,

I was wondering, is it possible to add a mouse listener. My idea is when I perform a double click with my mouse over the agent to call a different pop-up windows. In this pop-up I would like to plot the variable values (that I want to see) in a chart. 

 

Is it possible? Maybe I could override some repast code in the display. However, I do not know where this classes are implemented.

Can someone help me?

Thanks in advance,

Cesar


--

--------------------------------------------------------------
Ing. Cesar Augusto Nieto Coria

 




--

--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria

skype: cnietocoria

email: [hidden email]

 




--

--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria

skype: cnietocoria

email: [hidden email]

 




--

--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria

skype: cnietocoria

email: [hidden email]

 


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mouse Listener

cnietocoria
Thanks a lot! This is exactly what I was thinking to do. Now I try the code.

Thanks again :)

Cesar



On Wed, Jun 14, 2017 at 4:35 PM, Tatara, Eric R. <[hidden email]> wrote:

Hi Cesar,

 

The 2D display uses some pretty sophisticated OpenGL code and unfortunately the moue event related parts are buried so deep that it’s not possible to access them via Repast model code.  However, you can still do a lot with the double click ProbleListener.  You might consider attaching something like a menu selection to the ProbeListener that allows you to select from multiple options.  The example code below creates a popup menu on double clicking an agent and lets you move the agent by specifying the agent’s location in space.  This example uses a very simple dialog, but a more complex GUI panel could be used instead.  Note that if you are moving agents with this approach you will probably need to move the agent in all projections (space or grid) in which it resides.  The example code here explicitly calls the space.moveTo() method to show what’s needed, but I would recommend internalizing the movement code to the agent class and let the agent manage how it moves in the various projections, e.g. have a public getter and setter for the agent’s x,y coordinates so that you don’t need to reference contexts and projections in the ProbeListener code.

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object agent = agents.get(0);  // First agent

             

              JPopupMenu menu = new JPopupMenu();

 

           JMenuItem item1 = new JMenuItem("Action 1");

           JMenuItem item2 = new JMenuItem("Action 2");

          

           menu.add(item1);

           menu.add(item2);

             

           item1.addActionListener(new ActionListener() {

                     @Override

                     public void actionPerformed(ActionEvent e) {

                           showMoveDialog(agent, display);

                     }

              });

          

           item2.addActionListener(new ActionListener() {

                     @Override

                     public void actionPerformed(ActionEvent e) {

                           System.out.println("Action 2");

                          

                     }

              });

         

           

           JFrame frame = RSApplication.getRSApplicationInstance().getGui().getFrame();

          

           Point loc = frame.getMousePosition();

          

           menu.show(frame, (int)loc.getX(), (int)loc.getY());

             

       }

      

       /**

       * Create a simple dialog that lets the user change the agent's coordinates.

       *

        * @param agent

       * @param display

       */

       public void showMoveDialog(Object agent, IDisplay display){

             

              // TODO any other projections the agent is in may need to be updated

              Context context = ContextUtils.getContext(agent);

              ContinuousSpace space = (ContinuousSpace)context.getProjection("space");

             

              double x = space.getLocation(agent).getX();

              double y = space.getLocation(agent).getY();

             

              JTextField xField = new JTextField(String.valueOf(x));

              JTextField yField = new JTextField(String.valueOf(y));

             

              final JComponent[] inputs = new JComponent[] {

                      new JLabel("x coordinate"),

                      xField,

                      new JLabel("y coordinate"),

                      yField

              };

             

              String title = "Coordinates for " + agent.toString();

              int result = JOptionPane.showConfirmDialog(null, inputs, title,

                           JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

             

              if (result == JOptionPane.OK_OPTION) {

                 

                     double newX = Double.valueOf(xField.getText());

                     double newY = Double.valueOf(yField.getText());

                    

                     // Move only if coords change

                     if (newX != x || newY != y){           

                           space.moveTo(agent, newX, newY);

                          

                           display.update(); // to refresh the display

                           display.render();

                     }

                    

              } else {

                 // cancelled

              }

       }

}

 

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Wednesday, June 14, 2017 3:43 AM
To: Tatara, Eric R. <[hidden email]>

Subject: Re: [Repast-interest] Mouse Listener

 

I'm working with a 2D display.

 

On Tue, Jun 13, 2017 at 12:33 PM, Tatara, Eric R. <[hidden email]> wrote:

It might be possible to add different mouse listeners, depending on the display type.  Which type of display (2D, 3D, GIS)  are you working with?

 

eric

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Tuesday, June 13, 2017 3:16 AM


To: Tatara, Eric R. <[hidden email]>
Cc: [hidden email]
Subject: Re: [Repast-interest] Mouse Listener

 

Excellent! It work very well. Although, the pie chart example returns me some errors, I manage to create other chart type. Thanks a lot!!

This generated me other question, Can I add more than one probe listener? For example, one for the double click behavior and other for drag my agents into other location. Lets suppose that I want to relocate my agents. My agents are in a discrete grid, with a default position, but maybe I would like to relocate then using the mouse (drag and drop). Is it possible?

 

 

 

On Mon, Jun 12, 2017 at 1:59 PM, Tatara, Eric R. <[hidden email]> wrote:

Using JFreeChart is a great idea and the libraries are already included with Repast.   You can disable the default probe behavior on a display by assigning a new ProbeListenerSupport instance just before adding your new ProbeListener :

 

((AbstractDisplay)display).setProbeSupport(new ProbeListenerSupport());

display.addProbeListener(new MyProbeListener());

 

 

If you would like to restore the default probe behavior you could call getProbeSupport() on the display before assigning the new ProbeListenerSupport ,  store it somewhere and then assign it again later.

 

Here is an updated MyProbeListener that adds a sample JFreeChart to the Repast display.  If you don’t want to dock the display in the Repast window, you can always just add the chart to a JFrame which will appear as a floating window separate from the Repast GUI:

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object o = agents.get(0);  // First agent

             

//            JOptionPane.showMessageDialog(null, "Hi from " + o.toString(), "Probe",

//                         JOptionPane.INFORMATION_MESSAGE);

//           

              // create a dataset...

              DefaultPieDataset dataset = new DefaultPieDataset();

              dataset.setValue("Category 1", 43.2);

              dataset.setValue("Category 2", 27.9);

              dataset.setValue("Category 3", 79.5);

             

              JFreeChart chart = ChartFactory.createPieChart(

                           "Sample Pie Chart",

                           dataset,

                           true,    // legend?

                           true,    // tooltips?

                           false    // URLs?

                           );

 

             

              DockableFrame view = RSApplication.getRSApplicationInstance().getGui().addVizualization("MyChart", new ChartPanel(chart));

             

              view.toFront();

       }

 

 

 

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Monday, June 12, 2017 3:43 AM
To: Tatara, Eric R. <[hidden email]>
Cc: [hidden email]
Subject: Re: [Repast-interest] Mouse Listener

 

Dear Eric,

Thanks a lot, this worked! Well my idea is when I perform double click open a JPanel, in it i would like to plot one or two charts. This charts are like the ones with RS can be displayed. Now I'm learning about JFreeChart. Do you think that is possible to create something like what I described?

Other question about the display. There is a way to switch off the regular action of double click? This is just in general because I might want to personalize what is displaying when the double click is performed. 

Thanks again,

Cesar

 

On Fri, Jun 9, 2017 at 3:08 PM, Tatara, Eric R. <[hidden email]> wrote:

You can add a ProbeListener to the display using the GUI Registry.   This is a bit clunky since the GUI registry is not really designed to be accessed via user code, but you can get access to individual displays if you add something like the following to your context builder or model initializer:

 

RunEnvironment.getInstance().getCurrentSchedule().schedule(

                           ScheduleParameters.createOneTime(0), new IAction(){

 

                     @Override

                     public void execute() {

                           List<IDisplay> displays = RunState.getInstance().getGUIRegistry().getDisplays();

                          

                           for (IDisplay display : displays){

                                  display.addProbeListener(new MyProbeListener());

                                 

                           }

                     }

              });

 

The critical part is to execute this code at tick 0 or later since the displays don’t yet exist when the context builder is called.  This example will add an instance of a custom ProbeListener to every display.  You can set which displays have the ProbeListener added by checking the IDisplay instances to say only add it to a certain display type (e.g. 2D, 3D, GIS).  There’s unfortunately no easy way to further specify (e.g. display name) display properties since the GUI registry is fairly restricted access.  Your ProbeListner implementation should look something like:

 

public class MyProbeListener implements ProbeListener {

 

       @Override

       public void objectProbed(ProbeEvent evt) {

             

              IDisplay display = (IDisplay)evt.getSource();

             

              List agents = evt.getProbedObjects();

             

              Object o = agents.get(0);  // First agent

             

              JOptionPane.showMessageDialog(null, "Hi from " + o.toString(), "Probe",

                           JOptionPane.INFORMATION_MESSAGE);

             

       }

 

}

 

And then just change the objectProbed() method to perform the action you want.

 

eric

 

From: Cesar Nieto Coria [mailto:[hidden email]]
Sent: Tuesday, June 6, 2017 9:24 AM
To: [hidden email]
Subject: [Repast-interest] Mouse Listener

 

Dear all,

I was wondering, is it possible to add a mouse listener. My idea is when I perform a double click with my mouse over the agent to call a different pop-up windows. In this pop-up I would like to plot the variable values (that I want to see) in a chart. 

 

Is it possible? Maybe I could override some repast code in the display. However, I do not know where this classes are implemented.

Can someone help me?

Thanks in advance,

Cesar


--

--------------------------------------------------------------
Ing. Cesar Augusto Nieto Coria

 




--

--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria

skype: cnietocoria

email: [hidden email]

 




--

--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria

skype: cnietocoria

email: [hidden email]

 




--

--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria

skype: cnietocoria

email: [hidden email]

 




--
--------------------------------------------------------------
PhD. Student at University of Camerino
Ing. Cesar Augusto Nieto Coria
skype: cnietocoria


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Repast-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/repast-interest
Loading...