Can't get object to display correctly using PGraphics

Hello, Why does my code not display while using PGraphics to create an image? The compiler doesn't throw any errors, but it doesn't display anything at all. I've been working on this for days, but I'm having a hard time understanding why it's not running correctly.

    PGraphics pg;
    int [] degree_right = {0, 45, 90, 135, 180, 225, 270, 315};

    void setup() {
      size(1920, 1080);
      pg = createGraphics(640, 200);
    }

    void Bearing_right (int x, int y)
    {
      pg.beginDraw();
      pg.background (0);
      pg.pushMatrix();
      pg.translate (x, y);
      for (int i = 1; i < 700/135; i+=1)
      {
        pg.line (i*135, 20, i*135, 40);
        pg.text (degree_right [i], i*135, 55);
        for (int j = 0; j < 555/15; j+=1)
        {
          pg.line (j*15, 20, j*15, 25);
        }
      }
      pg.popMatrix();
      pg.endDraw();
    }

    void draw ()
    {
      Bearing_right (mouseX, 250);
      image (pg, 640, 250);
    }

I am trying to create a linear compass that represents bearing, similar to the compass in this picture. http://media.pcgamer.com/files/2011/08/Battlefield-3-first-man-to-shoot-a-jet-with-a-tank-deserves-a-medal.jpg

I have successfully written the code (shown at the bottom), but my problem is that I can't seem to contain the compass inside of it's own designated area, like in the picture. My goal is to have the compass simply display only in the center 1/3 of the screen, instead of reaching to one side, to the other.

///////// This is the successful code: /////////

int [] degree_right = {0, 45, 90, 135, 180, 225, 270, 315};
int [] degree_left = {0, -45, - 90, -135, -180, -225, -270, -315};

void setup ()
{
  PFont font;
  font = loadFont ("LucidaConsole-48.vlw");
  textFont (font, 14);
  textAlign (CENTER);
  size (1920, 1080);
}

void draw ()
{
  noCursor();
  background (77, 79, 70);
  stroke (255);
  strokeWeight(1);
  smooth ();
  noFill ();
  Bearing_right (mouseX, 250);
  Bearing_left (mouseX, 250);
}

void Bearing_right (int x, int y)
{
  pushMatrix();
  translate (x, y);
  for (int i = 1; i < 700/135; i+=1)
  {
//draw big tick mark
    line (i*135, 20, i*135, 40);
//calls array
    text (degree_right [i], i*135, 55);
    for (int j = 0; j < 555/15; j+=1)
    {
//draw little tick mark
      line (j*15, 20, j*15, 25);
    }
  }
  popMatrix();
}

void Bearing_left (int x, int y)
{
  pushMatrix();
  translate (x, y);
  for (int i = 0; i < 700/135; i+=1)
  {
//draw big tick mark
    line (i*-135, 20, i*-135, 40);
//calls array
    text (degree_left [i], i*-135, 55);
    for (int j = 0; j < 555/15; j+=1)
    {
//draw little tick mark
      line (j*-15, 20, j*-15, 25);
    }
  }
  popMatrix();
}

Answers

  • A PGraphics object is derived from PImage.
    In order to display it, we gotta use image() or set() or background():

    void draw() {
      Bearing_right(mouseX, 250);
      image(pg, 640, 250);
    }
    
  • edited August 2014

    I see the PGraphics displayed in the middle of the sketch area. It is a black rectangle. You use background() with a black color, and draw with the default color which is black too...

    Moreover, you translate to x, y, the latter being 250 on a PGraphics of 200 pixels.

Sign In or Register to comment.