Example #1
0
/* Main function */
int main() {
    GWindow window(WIDTH, HEIGHT);
    GBufferedImage *image = new GBufferedImage(WIDTH, HEIGHT, 0xFFFFFF);
    window.add(image);

    Point newPoint(WIDTH - OFFSET, HEIGHT - OFFSET); // start point

    for (int i = 0; i <= 200000; i++){
        image->setRGB(newPoint.getX() - 1, newPoint.getY() - 1, 0x000000);
        newPoint = moveRandomly(newPoint);
    }

    return 0;
}
/*
 * Function:  floodFillRecursion()
 * --------------------
 * Helper function that does the actual recursion for floodFill
 *
 * Preconditions: None
 *
 *  @param: A buffered image object that tells us where and what was clicked
 *          x,y position clicked
 *          color selected to change to
 *  @return: The number of pixels actually changed.
 */
int floodFillRecursion(GBufferedImage& image, int x, int y, int colorClicked, int color,int& numChangedPixels) {
    if(image.getRGB(x,y)==colorClicked){
        image.setRGB(x,y,color);
        numChangedPixels++;
    } else {
        return numChangedPixels;
    }
    if(image.inBounds(x-1,y)){
        floodFillRecursion(image,x-1,y,colorClicked,color,numChangedPixels);
    }
    if(image.inBounds(x+1,y)){
        floodFillRecursion(image,x+1,y,colorClicked,color,numChangedPixels);
    }
    if(image.inBounds(x,y-1)){
        floodFillRecursion(image,x,y-1,colorClicked,color,numChangedPixels);
    }
    if(image.inBounds(x,y+1)){
        floodFillRecursion(image,x,y+1,colorClicked,color,numChangedPixels);
    }
    return numChangedPixels;
}
GBufferedImage* GBufferedImage::diff(GBufferedImage& image, int diffPixelColor) const {
    int w1 = (int) getWidth();
    int h1 = (int) getHeight();
    int w2 = (int) image.getWidth();
    int h2 = (int) image.getHeight();
    int wmin = std::min(w1, w2);
    int hmin = std::min(h1, h2);
    int wmax = std::max(w1, w2);
    int hmax = std::max(h1, h2);
    
    GBufferedImage* result = new GBufferedImage(wmax, hmax, diffPixelColor);
    result->fillRegion(0, 0, w1, h1, m_backgroundColor);
    for (int y = 0; y < hmin; y++) {
        for (int x = 0; x < wmin; x++) {
            int px1 = m_pixels[y][x];
            int px2 = image.m_pixels[y][x];
            if (px1 != px2) {
                result->setRGB(x, y, diffPixelColor);
            }
        }
    }
    return result;
}
void gbufferedImageTest() {
    GWindow gw;
    gw.setSize(900, 700);
    gw.setTitle("Test");

    GButton* button1 = new GButton("Click Me 1");
    gw.add(button1, 250, 80);
    //GButton* button2 = new GButton("Click Me 2");
    //gw.addToRegion(button2, "NORTH");

    GLabel* label = new GLabel("test!");
    gw.add(label, 10, 60);
    
    std::cout << "About to construct GBufferedImage." << std::endl;
    GBufferedImage* img = new GBufferedImage(10, 80, 200, 250);
    std::cout << "Done constructing GBufferedImage." << std::endl;
    gw.add(img, 50, 50);
    // gw.addToRegion(img, "SOUTH");
    gw.setVisible(true);
    
//    GBufferedImage* img2 = new GBufferedImage(20, 20);
//    img2->fill(GBufferedImage::createRgbPixel(255, 0, 255));
//    Grid<int> grid = img2->toGrid();
//    cout << "grid of pixels before: " << grid << endl;
//    for (int y = 4; y <= 18; y++) {
//        for (int x = 2; x <= 9; x++) {
//            grid[y][x] = GBufferedImage::createRgbPixel(0, 255, 0);
//        }
//    }
//    cout << "grid of pixels after: " << grid << endl;
//    img2->fromGrid(grid);
//    gw.add(img2, 350, 20);
    
    GBufferedImage* img3 = new GBufferedImage();
    img3->load("rainbow.png");
    cout << "adding the image!" << endl;
    gw.add(img3, 10, 20);
    // pause(2000);
    
    cout << "start toGrid" << endl;
    Grid<int> grid3 = img3->toGrid();
    cout << "end toGrid, start rgb shit" << endl;
    for (int y = 0; y < grid3.height(); y++) {
        for (int x = 0; x < grid3.width(); x++) {
            int red, green, blue;
            GBufferedImage::getRedGreenBlue(grid3[y][x], red, green, blue);
            grid3[y][x] = GBufferedImage::createRgbPixel(green, red, blue);
        }
    }
    cout << "end rgb shit, start fromGrid" << endl;
    img3->fromGrid(grid3);
    cout << "end fromGrid" << endl;
    
    pause(2000);
    return;

    // fill
    img->fill(0xff00ff);  // purple

    std::cout << "About to setRGB on GBufferedImage." << std::endl;
    for (int y = 2; y < img->getHeight() - 2; y++) {
        for (int x = 5; x <= img->getWidth() - 5; x++) {
            img->setRGB(x, y, 0xffcc33);
        }
    }
    std::cout << "Done setting RGB on GBufferedImage." << std::endl;
    border(img);
    pause(500);
    
    std::cout << "About to resize on GBufferedImage." << std::endl;
    img->resize(100, 50);
    border(img);
    pause(500);
    
    std::cout << "About to resize on GBufferedImage." << std::endl;
    img->resize(200, 80);
    border(img);
    pause(500);
    
    std::cout << "About to setRGB on GBufferedImage." << std::endl;
    for (int y = 10; y < img->getHeight() - 10; y++) {
        for (int x = 10; x <= img->getWidth() - 10; x++) {
            img->setRGB(x, y, 0xff33cc);
        }
    }
    border(img);
    
    std::cout << "About to remove other shit." << std::endl;
    pause(200);
    gw.remove(label);
    gw.remove(button1);
    //gw.removeFromRegion(button2, "NORTH");
    pause(200);
    
    std::cout << "About to remove GBufferedImage." << std::endl;
    pause(200);
    gw.remove(img);
    // gw.removeFromRegion(img, "SOUTH");
    pause(200);
    std::cout << "Test complete." << std::endl;
    std::cout << std::endl;
}