1 Replies - 3923 Views - Last Post: 12 February 2012 - 07:24 AM Rate Topic: -----

#1 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 116
  • View blog
  • Posts: 363
  • Joined: 20-August 08

My "widescreen bars" are out of whack.

Posted 11 February 2012 - 11:20 PM

I'm writing a small game in Java, and I'm trying to force some widescreen bars in my JFrame since I think they look nice (that, and they'll also help me be sure where I'm plotting pixels.) The way I have it set up right now is that I set the JFrame to a temporary size, and as I continue to initialize it, I read in the window Insets so I can adjust the window to give me a canvas of exactly 800x500. When its time for the rendering portion of my code, it's passed a Graphics2D handle, and it's translated and setClip()'d to give the actual game renderings a view of 800x400 (the other 100 being split into 2 50px tall black bars.)

To get you up to speed, here's the code for the class:
public class GameFrame extends JFrame {
    GameFrame(int width, int height) {
        try {
            setSize(width, height);
            setTitle("Derp.");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
            setResizable(false);

            insets = getInsets();

            setSize(
                width  + insets.right, 
                height + insets.bottom + insets.top
            );
            
            setLocationRelativeTo(null);
            createBufferStrategy(2);
        }
        catch(Exception e) {
            System.out.println("GameFrame failed to initialize!");
            System.out.println(e.getMessage());
            System.exit(1);
        }
    }

    public Graphics2D getHandle() {
        BufferStrategy strategy = getBufferStrategy();
        Graphics2D g = (Graphics2D)strategy.getDrawGraphics();

        /* Note: A quick Insets hack gaurantees cross-platform surface origin.*/
        g.translate(insets.left, insets.top);
        
        return g;
    } 

    public void update() {
        BufferStrategy strategy = getBufferStrategy();
        strategy.show();
    }

    private Insets insets = null;
}



Now, this code works perfectly in Windows 7. The problem arises when I tried it out in Linux (Fedora 16 with Openbox). The game itself works, but one of the two widescreen bars is out of size. Here's a good screenshot:
Posted Image (http://dumpt.com/img/files/3tc3wn49jdcuk8evtah2.png)

As you can see both bars should be the same size, but aren't (although like I said, they were in Windows.)

Now, I attempted to debug it, and I narrowed it down to the line that reads:
setResizable(false);


This is when things started to get weird. If I make the window resizeable the problem is fixed, and even more so if I go back to the code, make it unresizeable again, it will remain fixed, but only temporarily. After a while the same problem arises. I verified that it happens again after I issue a sync command in a terminal, so this seems to be some kind of cache problem (maybe? :P)

Anyways, does anyone have any idea what may be causing it? I'm hoping it's not a problem with the Window Manager/Desktop Environment itself, but it might be. Is there any better way to have an unresizeable JFrame with locked in widescreen bars?

Thanks in advance! :)

This post has been edited by athlon32: 11 February 2012 - 11:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: My "widescreen bars" are out of whack.

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1373
  • View blog
  • Posts: 3,025
  • Joined: 05-April 11

Re: My "widescreen bars" are out of whack.

Posted 12 February 2012 - 07:24 AM

If I were you, I would let a JPanel handle everything that has to do with buffer strategies (it is by default double buffered), or make a Canvas (look up some of pbl's tutorials).

Also I would do all of my painting in a JPanel by overriding the paintComponent(Graphics g) method. By painting in a JPanel, you will not have to worry about the frame's border size.

As for the widescreen borders goes, I have made an example of how it could be done. I am sure you will understand how it works :)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class Widescreen extends JFrame {

	public static void main(String[] args) {
		new Widescreen();
	}
	
	public Widescreen() {
		super();
		setResizable(false);
		
		add(new GamePanel(800, 400), BorderLayout.CENTER);
		add(new WidescreenBorder(800, 50), BorderLayout.NORTH);
		add(new WidescreenBorder(800, 50), BorderLayout.SOUTH);
		
		setVisible(true);
		pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	private class GamePanel extends JPanel {
		
		public GamePanel(int width, int height) {
			//Our parent will ask us about the size we want
			setPreferredSize(new Dimension(width, height));
		}
	}
	
	private class WidescreenBorder extends JPanel {
		
		public WidescreenBorder(int width, int height) {
			setPreferredSize(new Dimension(width, height));
			setBackground(Color.BLACK);
		}
	}
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1