Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawing pixels on Canvas with BufferedImage not filling the whole Canvas
    primarykey
    data
    text
    <p>I'm trying to draw pixels on a Canvas using a BufferedImage. In my Canvas constructor I am initializing the image and pixel array like so:</p> <pre><code>public MyCanvas() { Dimension size = new Dimension(WIDTH, HEIGHT); setSize(size); setPreferredSize(size); setMinimumSize(size); setMaximumSize(size); image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData(); } </code></pre> <p>I draw every render by doing:</p> <pre><code>private void render() { BufferStrategy bs = getBufferStrategy(); if (bs == null) { createBufferStrategy(3); return; } for (int i = 0; i &lt; pixels.length; i++) { pixels[i] = 0xFFFFFF; } Graphics g = bs.getDrawGraphics(); g.fillRect(0, 0, getWidth(), getHeight()); g.drawImage(img, 0, 0, WIDTH, HEIGHT, null); g.dispose(); bs.show(); } </code></pre> <p>I'm expecting that my canvas is filled with white pixels, but I get black borders on the right and bottom of the white area, i.e. it's doesn't seem to be filling my entire canvas, or it has some negative offset. I've checked that the length of the pixel array is WIDTH * HEIGHT. I'm putting the canvas into a JPanel with BorderLayout.CENTER, in a JFrame.</p> <p>EDIT:</p> <p>Changing</p> <pre><code>g.drawImage(img, 0, 0, WIDTH, HEIGHT, null); </code></pre> <p>to</p> <pre><code>g.drawImage(img, 0, 0, getWidth(), getHeight(), null); </code></pre> <p>makes it do what I expect. However I don't understand why I have to fetch the canvas size when I have set the size myself (to WIDTH, HEIGHT), and it doesn't seem to be the same as the size I gave it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload