Using set() and get() without displaying the image

Hello,

It may sound like a weird question but it has a logical explanation, trust me .

I'm trying to make a TileMap class for my current project. The only way i found to select a part of the map and display it is using get() and set(). My problem is the following :I only want the selected part of the image to be displayed, not the full map... And i have no idea how to do this.

I don't know if anyone will understant my problem so here is my code, hoping it can help (i'm working on eclipse) :

import processing.core.PApplet;
import processing.core.PImage;

public class TileMap {
    PApplet parent;
    RectLect rectLect;
    int _size;//size in pixels of the image loaded
    int _x, _y;
    PImage _image; //tileMap
    PImage _selection; //To be drawn

    public TileMap(int x, int y, int rectW, int rectH, String image, PApplet p){
        parent = p;
        _image = parent.loadImage(image);
        _size = _image.width;
        rectLect =  new RectLect(0, 0, rectW, rectH, _size);
        _x = x;
        _y = y;
    }

    public void draw(){
        parent.set(0, 0, _image); //Here, this displays the full image on screen, I don't want that !
        _selection = parent.get(rectLect._x, rectLect._y, rectLect._w, rectLect._h);
        parent.set(_x, _y, _selection);//I only want that 
    }

    public void setPosition(int x, int y){
        _x = x;
        _y = y;
    }
}


public class RectLect {
    int _x, _y, _w, _h;
    int _size;//Size of the associated TileMap

    public RectLect(int x, int y, int w, int h, int size){
        _x = x;
        _y = y;
        _w = w;
        _h = h;
        _size = size;
    }

    public void move(int dir, int val){// |0 up ; 1 down ; 2 left ; 3 right| , |number of moves|
        switch(dir){
            case 0 : if(_y >= 0){ _y -=_h*val; } break;
            case 1 : if(_y <= _size){ _y +=_h*val; } break;
            case 2 : if(_x >= 0){ _x -=_w*val; } break;
            case 3 : if(_x <= _size){ _x +=_w*val; } break;
        }
    }

    public void set(int x, int y){
        _x = x;
        _y = y;
    }
}

Thanks for helping !

Tagged:

Answers

  • Your code:

    public void draw(){
        parent.set(0, 0, _image); //Here, this displays the full image on screen, I don't want that !
        _selection = parent.get(rectLect._x, rectLect._y, rectLect._w, rectLect._h);
        parent.set(_x, _y, _selection);//I only want that
    }
    

    Looks like you solved the problem yourself, so what is your issue? Just remove the first line of draw(), no?

    Note that unless I am mistaken, set(x, y, img) looks identical to image(img, x, y)...

  • No, if i remove the first line, the get() apply on what's displayed on screen...

  • edited October 2013 Answer ✓

    Ah, then replace parent with the reference to the PImage or PGraphics you want to capture. This kind of command, applying to the sketch, often also apply to a PGraphics (they are actually used against the main PGraphics, the one displayed in the sketch).

    I missed the "without displaying the image" part, that was only in your subject, not in your question...

  • Yay ! it's working !

    Thank you so much!

Sign In or Register to comment.