javagraphics2.5d

Is it possible to map a texture to quads similar to OpenGL with just Graphics?


I am creating a skyline game as a side project for fun, and I was having trouble when I would get to texturing the walls of the buildings, I currently am just drawing a polygon to that area, but I want to know if there is a way to texture it such that a texture like this:

Square

could be rendered like this:

Square mapped to parallelogram

Here is an image of the game, and the parts I want to texture are the gray walls of the buildings.

Gameplay image

I know I can do this by creating a separate rendering system that draws it pixel by pixel, but I am trying to avoid that.

My specific question is if there exists methods in Java's Graphics or something similar that can accomplish this.


Solution

  • if you want to perform a shear transformation AffineTransformation is your solution.

    AffineTransform at;
    
    double shx, shy;
    at.shear(shx, shy);
    

    use the AffineTransformation when draw image - sadly it's not so obvious that you have to use Graphics2D for that purpose

    @Override
    public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
        BufferedImage img = ...;
        AffineTransform at = ...;
        g2d.drawImage(img, at, null);//null-param is the ImageObserver
        ....
    }