loading...

Why doesn't my custom canvas display when using the same Graphics context? [repost]

baenencalin profile image Calin Baenen ・2 min read

So, I've slightly changed my custom canvas class (CPane), and, it doesn't throw any errors anymore (where, when I tried to keep both display, and ctx as their own variables, it threw NullPointerException). And, it doesn't display anything.

Why does it do that?

Class (also, I'm excluding some methods so there isn't too much to read):

/**
* Calin Baenen's version of a canvas element.
*/
class CPane {

    // Debug variables.
    private boolean canCatch = true; // See if this canvas can still catch errors.
    private final JPanel canvas; // Canvas.
    private BufferedImage buffer; // Buffer.
    private final Graphics ctx; // Graphics.



    // Constructor.
    public CPane(int width, int height) {
        this(0, 0, width, height);
    }
    public CPane(int width, int height, Color initialColor) {
        this(0, 0, width, height, initialColor);
    }
    public CPane(int x, int y, int width, int height) {
        this(x, y, width, height, Color.WHITE);
    }
    public CPane(int x, int y, int width, int height, Color initialColor) {
        this(x, y, width, height, initialColor, Color.WHITE);
    }
    public CPane(int x, int y, int width, int height, Color initialColor, Color baseColor) {

        this.canvas = new JPanel(); // Make an element to display the drawing on.
        canvas.setBounds(x, y, width, height); // Set the bounds of the canvas.
        canvas.setBackground(baseColor); // Make the canvas white.
        canvas.setDoubleBuffered(true); // Make the canvas double buffered.

        buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); // Set the buffer.

        ctx = buffer.getGraphics(); // Get the graphics for the buffer.

        // Paint the image with an initisl color.
        ctx.setColor(initialColor); // Set color.
        ctx.drawRect(0, 0, width, height); // Draw.

    }



    /**
    * Draws a rectangle to the screen by filling an area with color.
    */
    public void drawRect(int x, int y, int width, int height, Color color) {
        ctx.setColor(color); // Set the context's color.
        ctx.fillRect(x, y, width, height); // Draw a rectangle.
        this.update(); // Update the canvas.
    }



    /**
    * Draw an image to the canvas.
    */
    public void drawImage(Image image, int x, int y, int width, int height) {
        Image shot = image.getScaledInstance(width, height, Image.SCALE_FAST); // Get the scaled image to draw.
        ctx.drawImage(shot, x, y, null); // Draw the image.
        this.update(); // Update the canvas.
    }



    /**
    * Update the graphics, and log the error if it fails.
    */
    public void update(boolean catcherror) {
        Graphics display = canvas.getGraphics(); // Get the canvas' display.
        try {
            display.drawImage(buffer, 0, 0, null); // Draw the buffer to the canvas.
        } catch(Exception error) {
            if(catcherror && canCatch) {Book.eog(error); canCatch = false;} // Log the error and disable error catching.
        }
        display.dispose(); // Dispose of the display.
    }

}
Enter fullscreen mode Exit fullscreen mode

Thanks!
Cheers!

Discussion

pic
Editor guide
Collapse
sfiquet profile image
Sylvie Fiquet

It's been a long time since I last wrote java code, but aren't supposed to prefix all properties with this. when you access them?

Collapse
baenencalin profile image
Calin Baenen Author

Not unless there is a loxal var with the same name.

Collapse
sfiquet profile image
Sylvie Fiquet

You're totally right, sorry. Are you using Swing? I don't know the library but there seem to be lots of people with similar problems on StackOverflow. There's probably the answer to your problem somewhere in there.

Thread Thread
baenencalin profile image
Calin Baenen Author

Yes (I think) as well as awt, all the code is shown, and it's a custom implementation, so idk how many other questions are compatible with this class I've made (unless the code I've written is actually common).

Thanks for your help thus far, though.