mouseClicked() and draw on ControlP5 canvas

Hi all,

My program involves plotting points on a map (on a canvas). I have another canvas where, if I hover on different areas, rectangles appear. What I'm having trouble with clicking on the different rectangles and mapping the points. As a test, I tried clicking on an area and drawing a rectangle, which works fine but I would like it to draw only on the canvas (currently draws even when canvas isn't showing). How can I do this? Within mouseClicked(), or within controlEvent()? Thanks in advance for any advice.

Here is my code so far (I haven't included code for mapping events):

import de.fhpotsdam.unfolding.*;
import de.fhpotsdam.unfolding.geo.*;
import de.fhpotsdam.unfolding.utils.*;
import de.fhpotsdam.unfolding.providers.*;
import de.fhpotsdam.unfolding.mapdisplay.MapDisplayFactory;
import java.util.List;
import controlP5.*;

ControlP5 cp5;
Canvas cc;
UnfoldingMap map;

int mapX = 250;
int mapY = 10;
int mapW = 515;
int mapH = 630;

Location mapLocation = new Location(-37.7f, 144.8f);
//==============================================================

public void setup() {
  size(800, 650, P2D);
  smooth();

  //MAP
  map = new UnfoldingMap(this, "map", mapX, mapY, mapW, mapH, true, false, new Google.GoogleTerrainProvider());
  MapUtils.createDefaultEventDispatcher(this, map);
  map.zoomAndPanTo(mapLocation, 11); 

  //CANVAS
  cp5 = new ControlP5(this);
  cp5.setControlFont(new ControlFont(createFont("Arial", 14), 14));
  cp5.addButton("sequences")
    //.setValue(0)
    .setPosition(20, 80)
      .setSize(170, 50)
        ;
}

//==============================================================             

public void draw() {
  background(180);
  map.draw();
}    

//==============================================================

class SeqCanvas extends Canvas {
  public void draw(PApplet p) {
    p.fill(255, 170);
    p.rect(250, 10, 515, 630);

    rectAreas(1, 2, 1);
    rectAreas(2, 3, 2);
    rectAreas(3, 4, 3);
    rectAreas(4, 5, 4);
    rectAreas(5, 6, 5);
    rectAreas(6, 7, 6);
    rectAreas(7, 8, 7);
    rectAreas(8, 9, 8);
    rectAreas(9, 10, 9);
    rectAreas(10, 11, 10);
    rectAreas(11, 12, 14);
    rectAreas(12, 13, 17);
    rectAreas(13, 14, 15);
  }
}

//==============================================================

void rectAreas(int y1, int y2, int i) {
  int x = 270;
  int y = 22;
  int rx = 265;
  int ry =  20;
  int rw = 290;
  int rh = 13;

  if ( mouseX > x&& mouseX < x+rw &&
  mouseY > y+(rh*y1) && mouseY < y+(rh*y2) ) {
    stroke(165, 15, 21);
    strokeWeight(2);
    fill(165, 15, 21, 30);
    rect(rx, ry+(rh*i), rw, rh);
  }
}


//==============================================================

void controlEvent(ControlEvent theEvent) {

  if (theEvent.getName() == "sequences") {
    cc = new SeqCanvas();
    cp5.addCanvas(cc);
  }
}
Tagged:
Sign In or Register to comment.