EDIT: Sent the project to a friend, who compiled it and says it appears fine on his display, this had led me to believe that it's either my OS, GPU or Monitor, either way I guess this means it is independent of my code. Please check over if you will and insure I have done everything correctly, in case the error can be resolved this way! Thanks
Ok, so I took a first try at making a simple window using a JFrame, ready to be drawn on with images. My CanvasHolder class extends Canvas, and is the component I'm using with a BufferStrategy of 2 to draw to the screen. For the moment, I am not drawing anything other than a clearRect to clear the screen of the previous data (when some will be present).
As this code stands, -sometimes- it works (3/5 times), and I get a red background as I should (I'm using red for the sake of ease of knowing that what I'm writing is working). The other 2/5 times I just get a white screen that when clicked outside of closes.
Also, the times where it does work, the boundary seems to start a certain amount of pixels to the right, giving me a black band across the left which is not accessible by the mouse. This occurs no matter what resolution I set it to perform at.
Like I said, this is my first attempt at something in Java using a JFrame, I might have blatently done something very wrong or that is very bad practice, any advice on the following code would be great.
Thanks guys.
Main Class:
package mainPackage; import java.awt.DisplayMode; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import javax.swing.JFrame; import canvasPackage.CanvasHolder; public class MainMethod { //fields to hold screen resolution private static int xResolution; private static int yResolution; //JFrame variable to hold JFrame that holds the canvas private static JFrame jFrame; //main public static void main(String[] args) { //apply wanted resolution setXResolution(800); setYResolution(600); //see 'initialiseGraphics()' below jFrame = initialiseGraphics(xResolution, yResolution, 32, true); //creates an instance of the canvas holder, applying a dimension of the given resolution CanvasHolder canvas = new CanvasHolder(xResolution, yResolution); //apply canvas and set characteristics jFrame.add(canvas); jFrame.setVisible(true); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //modified method which creates bufferstrategy of required amount canvas.addNotify(2); //main 'loop', see CanvasHolder class canvas.mainRenderer(xResolution, yResolution); } public static JFrame initialiseGraphics(int xResolution, int yResolution, int bitDepth, boolean isFullscreen) { //detect graphics environment and deduce graphics config GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice device = environment.getDefaultScreenDevice(); GraphicsConfiguration config = device.getDefaultConfiguration(); //create new JFrame with necessary config JFrame jFrame = new JFrame(config); //if fullscreen is specified, check it is supported and change resolution to given values if ( isFullscreen == true ) { if ( device.isFullScreenSupported() ) { device.setFullScreenWindow(jFrame); if ( device.isDisplayChangeSupported() ) { device.setDisplayMode( new DisplayMode( xResolution, yResolution, bitDepth, DisplayMode.REFRESH_RATE_UNKNOWN ) ); } else { System.err.println("Change display mode not supported"); } } else { System.err.println("Full screen not supported"); } } return jFrame; } public static int getXResolution() { return xResolution; } public static void setXResolution(int x) { xResolution = x; } public static int getYResolution() { return yResolution; } public static void setYResolution(int y) { yResolution = y; } }
CanvasHolder class:
package canvasPackage; import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; public class CanvasHolder extends Canvas { //holds bufferstrategy BufferStrategy bufStrat; //constructor initiates canvas with given resolution and has focus. Background colour set here public CanvasHolder(int xRes, int yRes) { setBackground(Color.red); this.setPreferredSize(new Dimension(xRes, yRes)); setFocusable(true); requestFocus(); } //custom addNotify() to create buffer strategy public void addNotify(int buffer) { super.addNotify(); this.createBufferStrategy(buffer); this.bufStrat = this.getBufferStrategy(); } public void mainRenderer(int xRes, int yRes) { //create graphics object to 'represent' current off-screen image Graphics g = bufStrat.getDrawGraphics(); //wipe last iteration's pixels from screen ready for re-drawing g.setColor(Color.red); g.clearRect(0, 0, xRes, yRes); /* * Image drawing goes here */ //switch off-screen and on-screen images and release unneeded graphics representation bufStrat.show(); g.dispose(); //apply a sleep time to control tickrate try { Thread.sleep(19); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //draw image method goes here public void drawImage(Graphics g, BufferedImage bufImage, int xLocation, int yLocation) { } }