Java swing how to draw an image as a quadrilateral by using 4 points

364 views Asked by At

I want to draw an image as a quadrilateral by using the points of the 4 corners as parameters. Does Java have anything already built in for that? I've seen a similar post but the solution given did not seem to do what I want. The function call would look like this:

wall.drawTexture(Point topLeft, Point topRight, Point bottomLeft, Point bottomRight);

My wall class already contains an attribute "BufferedImage texture".

enter image description here

Context: I'm doing a raycaster engine. I can split my image into smaller columns to correspond to my pixel columns on the 3D view but the texture integrity isn't very high. I thought by only using the first and last ray to hit the same wall face and convert to a parallelogram instead would solve my problem. It would also probably be faster than drawing column by column.

2

There are 2 answers

3
Smartsav10 On BEST ANSWER

You could use drawLine. Something where it draws a line between 1-2, 1-3, 4-2, 4-3 .

0
WJS On

All I can suggest is that you consider the following:

paintComponent(Graphics g) {
     super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;
    // Rendering hints can visually smooth out the graphics with
    // anti aliasing.
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
}

Other possibilities would be looking at the AffineTransform and related classes where you can manipulate various aspects of a graphics context (e.g. shearing, blurring, etc).