title: Java - Swing GUI renders incorrectly in Windows 7
body: <p>I'm building a Tic Tac Toe game in Java with a Swing GUI, and it renders correctly in Ubuntu 10.4 and Windows XP. This is how it looks like in Ubuntu:</p>
  
  <p><a href="https://i.stack.imgur.com/ElWt0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ElWt0.png" alt="http://img266.imageshack.us/img266/2432/tictactoe2.png"></a>
  </p>
  
  <p>When I copied the bin-folder with all the class files and tried to run the program in Windows 7 it looked like this instead:</p>
  
  <p><a href="https://i.stack.imgur.com/FFFQR.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FFFQR.gif" alt="img413.imageshack.us/img413/6144/tictactoe1.gif"></a>
  <br>
  <a href="https://i.stack.imgur.com/y7bbi.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/y7bbi.gif" alt="img708.imageshack.us/img708/4387/tictactoe2.gif"></a>
  </p>
  
  <p>I just can't understand what's wrong. As I said, it works perfectly in Ubuntu 10.4 and Windows XP.</p>
  
  <p>I would be very happy if someone could help me out! I'll post the code related to the GUI, just in case it is needed to solve the problem.</p>
  
  <p>Here is the code I use to initialize the GUI:</p>
  
  <pre><code>//Initializing GUI.
      frame = new JFrame();  //Creating the window.
      frame.setTitle("Tic Tac Toe"); //Setting the title of the window.
      frame.addMouseListener(this);
      frame.getContentPane().add(BorderLayout.CENTER, grid.getPanel());  //Adding the grid panel.
      info = new JLabel(" Initializing game...");         //Creating info text.
      frame.getContentPane().add(BorderLayout.SOUTH, info);  //Adding info text.
  
      //Setting GUI properties.
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(300, 300);
      frame.setVisible(true);
  </code></pre>
  
  <p>The panel with the grid itself is created in my GameGrid class, which have a method "JPanel getPanel()". Here is the initialization of that panel (the code belongs in the constructor of GameGrid):</p>
  
  <pre><code>     GridBox temp;
      layout = new GridLayout(getHeight(), getWidth());
      panel = new JPanel(layout);
      panel.setBorder(
          BorderFactory.createCompoundBorder(
                  BorderFactory.createTitledBorder("Click in a box to place a marker:"),
                      BorderFactory.createEmptyBorder(5,5,5,5)));
  
      //Creating a GridBox for each cell, and adding them to the panel in the right order..
      for(int i = 0; i &lt; getHeight(); i++) {    
          for(int j = 0; j &lt; getWidth(); j++) {
              temp = new GridBox(j, i);
              temp.addMouseListener(listener);
              panel.add(temp);
          }
      }
  </code></pre>
  
  <p>GridBox is a subclass of JPanel, which I modified to automatically show the contents of the grid at the coordinates specified.</p>
  
  <pre><code>class GridBox extends JPanel {
      private static final long serialVersionUID = 1L;
      int fontsize, x, y, value, signHeight, signWidth;
      char print;
      FontMetrics fm;
      LineMetrics lm;
  
      public GridBox(int a, int b) {
          x = a;     //TODO - input control
          y = b;
      }
  
      public Move getMove() {
          Move m = new Move(x, y);
          return m;
      }
  
  
      public void paintComponent(Graphics g) {
          Border blackline = BorderFactory.createLineBorder(Color.black);
          setBorder(blackline);
          Dimension size = getSize();
          Rectangle2D rect;
          fontsize = (int)(size.getHeight()*0.75);
          value = getGridValue(x, y);
          if(value == EMPTY)
              print = ' ';
          else if(value == 0)
              print = 'X';
          else if(value == 1)
              print = 'O';
          else
              print = (char)value;
  
  
          Font font = new Font("Times New Roman", Font.PLAIN, fontsize);
          g.setFont(font);
          fm = g.getFontMetrics();
          rect = fm.getStringBounds(Character.toString(print), g);
          signHeight = (int)rect.getHeight();
          signWidth = (int)rect.getWidth();
  
  
          g.setColor(Color.black);
          g.drawString(Character.toString(print), (size.width/2)-(signWidth/2), (size.height/2)-(signHeight/2)+fm.getAscent());
      }
  }
  </code></pre>
  
  <p>Thanks in advance!</p>