Black flashing around drawn image

Multi tool use
Multi tool use


Black flashing around drawn image



I'm currently trying to draw an image to a JFrame (just a nonsense test image). In the code bite below, the image is drawn to the JFrame, but the area around the image that doesn't fill JFrame is black that rapidly flashes.



Here is the code below:


try {

myImage = ImageIO.read(ImagesMain.class.getResource("/Textures/TestImage.png"));

}catch(Exception e) {
e.printStackTrace();
}

BufferStrategy strategy = null;

while(strategy == null) {//I know this is terrible practice, just doing this way because its inside main

strategy = myCanvas.getBufferStrategy();

if(myCanvas.getBufferStrategy() == null) {
myCanvas.createBufferStrategy(3);
}
}



myFrame.setVisible(true);

//Rendering part


while(true) {
do {
do {

g = strategy.getDrawGraphics();
g.setColor(Color.WHITE);
g.drawImage(myImage, 20, 20, null);
g.dispose();

}while(strategy.contentsRestored());

strategy.show();

}while(strategy.contentsLost());
}



I've tested and retested my code several times to no avail. I should also add that this is all done in the main method (for testing purposes). Long story short, how do I display my image without the unnecessary black flashing around the image?





Since you're not clearing the Graphics context to an initial state, it's going to, repeatedly, paint what ever was originally painted to it and what ever you've now painted to it
– MadProgrammer
Jul 2 at 1:59



Graphics





MadProgrammer to the rescue again! Thanks man, cleared the rect & it worked. Thanks
– Matthew
Jul 2 at 2:03




1 Answer
1



When this happens, it is because one is not clearing the Frame to which they are drawing. In this instance,
g.clearRect(x, y, height, width); is needed to clear the drawing frame and display a clear image.


g.clearRect(x, y, height, width);



Answer courtesy of @MadProgrammer above, in comments.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

w,vbs,nr8DxNV2FiPwv7RjRBfR2rlu5p,b2NKSvzTDgTVthY5GBiorvbO9 A lgbB0 e,SkZW3i abdupnOGPC84UxPMRZSF
Wa7 4rM0yB HexAdvgmZMe,1uwEPCpU zrXq3SiTXcwt,O1jhspq2rxRayNVz6atN8ddMehCy ttkZUOpbfm,KC P tySoUM2De3

Popular posts from this blog

Rothschild family

Cinema of Italy