Class FrameBuffer.Viewport

  • Enclosing class:
    FrameBuffer

    public class FrameBuffer.Viewport
    extends Object
    A Viewport is an inner (non-static nested) class of FrameBuffer. That means that a Viewport has access to the pixel data of its "parent" FrameBuffer.

    A Viewport is a two-dimensional sub array of its "parent" FrameBuffer. A Viewport is represented by its upper-left-hand corner and its lower-right-hand corner in the FrameBuffer.

    When you set a pixel in a Viewport, you are really setting a pixel in its parent FrameBuffer.

    A FrameBuffer can have multiple Viewports.

    Viewport coordinates act like Java Graphics2D coordinates; the positive x direction is to the right and the positive y direction is downward.

    • Constructor Detail

      • Viewport

        public Viewport()
        Create a Viewport that is the whole of its parent FrameBuffer. The default background Color is the FrameBuffer's background color. (Note: This constructor does not set the pixels of this Viewport. If you want the pixels of this Viewport to be cleared to the background color, call the clearVP() method.)
      • Viewport

        public Viewport​(Color c)
        Create a Viewport that is the whole of its parent FrameBuffer and with the given background color. (Note: This constructor does not use the background color to set the pixels of this Viewport. If you want the pixels of this Viewport to be cleared to the background color, call the clearVP() method.)
        Parameters:
        c - background Color for the Viewport
      • Viewport

        public Viewport​(int vp_ul_x,
                        int vp_ul_y,
                        int width,
                        int height)
        Create a Viewport with the given upper-left-hand corner, width and height within its parent FrameBuffer. (Note: This constructor does not set the pixels of this Viewport. If you want the pixels of this Viewport to be cleared to the background color, call the clearVP() method.)
        Parameters:
        vp_ul_x - upper left hand x-coordinate of new Viewport rectangle
        vp_ul_y - upper left hand y-coordinate of new Viewport rectangle
        width - Viewport's width
        height - Viewport's height
      • Viewport

        public Viewport​(int vp_ul_x,
                        int vp_ul_y,
                        int width,
                        int height,
                        Color c)
        Create a Viewport with the given upper-left-hand corner, width and height within its parent FrameBuffer, and with the given background color. (Note: This constructor does not use the background color to set the pixels of this Viewport. If you want the pixels of this Viewport to be cleared to the background color, call the clearVP() method.)

        (Using upper-left-hand corner, width, and height is like Java's Rectangle class and Graphics.drawRect(int, int, int, int) method.)

        Parameters:
        vp_ul_x - upper left hand x-coordinate of new Viewport rectangle
        vp_ul_y - upper left hand y-coordinate of new Viewport rectangle
        width - Viewport's width
        height - Viewport's height
        c - background Color for the Viewport
      • Viewport

        public Viewport​(int vp_ul_x,
                        int vp_ul_y,
                        FrameBuffer sourceFB)
        Create a Viewport, within its parent FrameBuffer, from the pixel data of another FrameBuffer.

        The size of the Viewport will be the size of the source FrameBuffer.

        Parameters:
        vp_ul_x - upper left hand x-coordinate of new Viewport rectangle
        vp_ul_y - upper left hand y-coordinate of new Viewport rectangle
        sourceFB - FrameBuffer to use as the source of the pixel data
      • Viewport

        public Viewport​(int vp_ul_x,
                        int vp_ul_y,
                        FrameBuffer.Viewport sourceVP)
        Create a Viewport, within its parent FrameBuffer, from the pixel data of a Viewport.

        The size of the new Viewport will be the size of the source Viewport.

        This constructor makes the new Viewport into a copy of the source Viewport.

        Parameters:
        vp_ul_x - upper left hand x-coordinate of new Viewport rectangle
        vp_ul_y - upper left hand y-coordinate of new Viewport rectangle
        sourceVP - FrameBuffer.Viewport to use as the source of the pixel data
      • Viewport

        public Viewport​(int vp_ul_x,
                        int vp_ul_y,
                        String inputFileName)
        Create a Viewport, within its parent FrameBuffer, from a PPM image file.

        The size of the Viewport will be the size of the image.

        This can be used to initialize a Viewport with a background image.

        Parameters:
        vp_ul_x - upper left hand x-coordinate of new Viewport rectangle
        vp_ul_y - upper left hand y-coordinate of new Viewport rectangle
        inputFileName - must name a PPM image file with magic number P6.
    • Method Detail

      • setViewport

        public void setViewport​(int vp_ul_x,
                                int vp_ul_y,
                                int width,
                                int height)
        Mutate this Viewport into the given upper-left-hand corner, width and height within its parent FrameBuffer.

        (Using upper-left-hand corner, width, and height is like Java's Rectangle class and Graphics.drawRect(int, int, int, int) method.)

        Parameters:
        vp_ul_x - new upper left hand x-coordinate of this Viewport rectangle
        vp_ul_y - new upper left hand y-coordinate of this Viewport rectangle
        width - Viewport's new width
        height - Viewport's new height
      • getWidthVP

        public int getWidthVP()
        Get the width of this Viewport.
        Returns:
        width of this Viewport rectangle
      • getHeightVP

        public int getHeightVP()
        Get the height of this Viewport.
        Returns:
        height of this Viewport rectangle
      • setBackgroundColorVP

        public void setBackgroundColorVP​(Color c)
        Set the Viewport's background color.

        NOTE: This method does not clear the pixels of the Viewport to the given Color. To actually change all the Viewport's pixels to the given Color, use the clearVP() method.

        Parameters:
        c - Viewport's new background Color
      • clearVP

        public void clearVP()
        Clear this Viewport using its background color.
      • clearVP

        public void clearVP​(Color c)
        Clear this Viewport using the given Color.
        Parameters:
        c - Color to clear this Viewport with
      • getPixelVP

        public Color getPixelVP​(int x,
                                int y)
        Get the Color of the pixel with coordinates (x,y) relative to this Viewport.
        Parameters:
        x - horizontal coordinate within this Viewport
        y - vertical coordinate within this Viewport
        Returns:
        the Color of the current pixel at the given Viewport coordinates
      • setPixelVP

        public void setPixelVP​(int x,
                               int y,
                               Color c)
        Set the Color of the pixel with coordinates (x,y) relative to this Viewport.
        Parameters:
        x - horizontal coordinate within this Viewport
        y - vertical coordinate within this Viewport
        c - Color for the pixel at the given Viewport coordinates
      • setPixelVP

        public void setPixelVP​(int x,
                               int y,
                               int c)
        Set the combined RGB value of the pixel with coordinates (x,y) relative to this Viewport.
        Parameters:
        x - horizontal coordinate within this Viewport
        y - vertical coordinate within this Viewport
        c - combined RGB value for the pixel at the given Viewport coordinates
      • convertVP2FB

        public FrameBuffer convertVP2FB()
        Create a new FrameBuffer containing the pixel data from this Viewport rectangle.
        Returns:
        FrameBuffer object holding pixel data from this Viewport
      • dumpVP2File

        public void dumpVP2File​(String filename,
                                String formatName)
        Write this Viewport to the specified image file using the specified file format.
        Parameters:
        filename - name of the image file to hold Viewport data
        formatName - informal name of the image format
      • vpTestPattern

        public void vpTestPattern()
        A simple test of the Viewport.

        It fills the viewport with a test pattern.