Bottom and Right-Hand Unwanted Border

I’ve encountered a problem that I’ve also seen in a few completed Java games

First off, I’m making an app using regular Java2D/Graphics2D setup

So my problem is that there’s a border on the right hand side of my app and the bottom of my app
It’s light grey
I don’t know why it exists. My JFrame is the same dimensions of my Game and everything, but for some reason there’s this region where nothing is drawn and it looks unprofessional

Has anyone else encountered this? How is it fixed?

Thanks

Thinking it’s the Frames ContentPane, it’s default color is gray ::slight_smile:

You could add a new loop/Jpanel onto the Frames ContentPane, than paint it to blend as you wish.

  • Or

JFrame frame = (JFrame) yourFrame;
frame.getContentPane().setBackground(java.awt.Color.black.darker());

Is there a way to get rid of the border? As in, I don’t want to change the color, I just want to destroy the part of the frame that has none of the game drawn on it.

Could you post some more details, don’t know how exactly you’re setup here.
JFrame >> ContentPane >> new JPanel with paint method?
JFrame >> Canvas?

Does your painting involve grabbing the JFrames dimensions?, know the getWidth()/getHeight() both change a little bit when frames switched from resizable to non resizable vise versa.

Sorry- I’m using a canvas and drawing a buffered image. This is what the class looks like:



public class GameComponent extends Canvas implements Runnable{
	private static final long serialVersionUID = 1L;
	
	private static final int WIDTH = 640;
	private static final int HEIGHT = 420;
	
	private BufferedImage bi;
	private Graphics graphics = null;
	private Graphics2D g2d = null;
	public BufferStrategy buffer;
	
	private boolean running;
	private Thread thread;
	Game game;

	public GameComponent () {
		Dimension size = new Dimension(WIDTH, HEIGHT);
		setSize(size);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		
		setIgnoreRepaint(true);
	    
		game = new Game(WIDTH, HEIGHT);
	}

	public synchronized void start() {
		if (running) return;
		running = true;
		thread = new Thread(this);
		thread.start();
	}

	public synchronized void stop() {
		if (!running) return;
		running = false;
		try {
			thread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void run() {
		//unimportant
	}
	
	public void draw(){
		g2d = bi.createGraphics();
		
		game.draw(g2d);
        
		graphics = buffer.getDrawGraphics();
		graphics.drawImage(bi, 0, 0, null);
				
        	if(!buffer.contentsLost())
			buffer.show();
	}
	
	public void setUpBufferedImage(){
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice gd = ge.getDefaultScreenDevice();
	 	GraphicsConfiguration gc = gd.getDefaultConfiguration();
	    
		bi = gc.createCompatibleImage(WIDTH,HEIGHT);

		createBufferStrategy(2);
		buffer = getBufferStrategy();
	}
	
	public static void main(String[] args) {
		GameComponent gameComponent = new GameComponent ();
		JFrame frame = new JFrame("Video Game");

		frame.setIgnoreRepaint(true);
		frame.add(gameComponent);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		
		gameComponent.setUpBufferedImage();

		gameComponent.start();
	}
}

(Ran the code, messed around a bit)
Has something to do within your Game class, thinking in your draw method since it’s the only “visible” method being called upon while rendering.
Try messing around with your Dimensions by a few increments in the Game class yet?

I had this same problem too, and managed to fix it by subtracting 10 from my width and height when creating the dimension object.

Example:


Dimension size = new Dimension(g.WIDTH * g.SCALE - 10, g.HEIGHT * g.SCALE - 10);
g.setSize(size);

g refers to my Game class, which extends Canvas.

Haha, that’s silly but it does work. Kinda mysterious… Oh well. Thanks to everybody

Yeah, I don’t quite understand it, but hey, it works! It doesn’t even mess up where things are drawn, as in if your width is 350 and your height is 200, drawing at (349, 199) still renders at the bottom right corner.

Glad I could help. :slight_smile:

i got dat problem too. the best solution is to call setResizable(false) before adding a panel to the frame

example


        this.frame = new JFrame(Game.TITLE);   
        this.frame.setResizable(false);
        
        JPanel panel = new JPanel(new BorderLayout());
        [...]     

Yeah, I fiddled with the code a bit too. I found that if you just pack() after you do frame.setResizable(false) instead, the “borders” are gone.