2

I am making a simple Java Swing GUI chessboard where the player can drag and drop pieces. The problem is that, because of the border around the frame (with the title on top, maximize/minimize/close buttons, etc), the coordinates are skewed off - (0, 0) is the upper-left-hand corner of the frame, that is, a little above the X button, but the GUI starts building itself right below the title bar, so the GUI doesn't align with the coordinates, and things do not end up working the way they should. Additionally, when I set the size of the frame to, for instance, 100 x 100, the lower part and some of the right-hand part of my GUI is cut off because the frame doesn't compensate for its border. When I run it as an applet, I don't have this problem, but I don't want to do that. How can I either get rid of that border around my frame window so I can just have the plain GUI, or have the coordinates set themselves up properly?

sscce:

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

public class class1 extends JFrame{
    public class1(){
        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent evt){
                System.out.print(evt.getPoint());
            }
        });
    }

    public static void main(String[] args){
        class1 c = new class1();
        c.setTitle("Test");
        c.setSize(320, 320);
        c.setLocationRelativeTo(null);
        c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        c.setVisible(true);
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Andrew Latham
  • 5,982
  • 14
  • 47
  • 87

1 Answers1

1

It's hard to know what is wrong with your code without the code, but I do know that if you go the easy way by using various layout managers, and let these managers do the laying out of components for you and the sizing of things as well, including calling pack() on the JFrame, usually things fall easily and well into place. So again, don't set the size of anything, but rather let the components' preferred sizes and the layout managers do this for you.

If this advice doesn't help, please give us more information and code, preferably an sscce, a small compilable and runnable program that doesn't do anything other than demonstrate your problem.

Edit: I am assuming that this is a Swing GUI. Please verify if this is so.

Edit 2: One problem you're having is that you're setting the size of a JFrame not taking into account its "decorations" including the menu bar, the resize/maximize/close icon. Again, you shouldn't be setting sizes directly, but if you must better override the getPreferredSize() method of the JPanel that holds your grid.

Edit 3: For example:

import java.awt.*;
import javax.swing.*;

public class Grid extends JPanel {
   public static final Color DARK_COLOR = Color.red.darker().darker().darker();
   public static final Color LIGHT_COLOR = Color.lightGray.brighter();
   public static final int SQUARE_SIDE = 60;
   private static final int ROW_COUNT = 8;

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(ROW_COUNT * SQUARE_SIDE, ROW_COUNT * SQUARE_SIDE);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      for (int i = 0; i < ROW_COUNT; i++) {
         for (int j = 0; j < ROW_COUNT; j++) {
            Color c = (i % 2 == j % 2) ? LIGHT_COLOR : DARK_COLOR;
            g.setColor(c);
            int x = i * SQUARE_SIDE;
            int y = j * SQUARE_SIDE;
            g.fillRect(x, y, SQUARE_SIDE, SQUARE_SIDE);
         }
      }
   }

   public Grid() {
      // TODO Auto-generated constructor stub
   }

   private static void createAndShowGui() {
      Grid mainPanel = new Grid();

      JFrame frame = new JFrame("Grid");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @Andrew: You've got a chit-load of files loaded there, probably too much to reasonably request volunteers to review for you. Can you condense your problem into one small program, one that is complete enough that we can run it and see your problem, yet small enough so that it be posted directly on this site, an [sscce](http://sscce.org) (please read the link)? – Hovercraft Full Of Eels May 13 '12 at 04:05
  • I added an sscce in the question. When run, clicking in the upper-left-hand corner of the GUI gives a (machine-dependent) coordinate that is not (0, 0). For instance, for me it is (3, 24). – Andrew Latham May 13 '12 at 04:13
  • I was actually able to solve my problem by making the function look at a Panel inside the Frame instead of the Frame itself. – Andrew Latham May 13 '12 at 04:41
  • @Andrew: glad you've got it working. Still you might take a look at my example as I think it might help. – Hovercraft Full Of Eels May 13 '12 at 04:41
  • @HovercraftFullOfEels : If you take `JFrame` as your base, I once realized as asked in the question, this issue comes up too, do have a look at this [answer](http://stackoverflow.com/questions/10122626/set-the-jpanel-size-to-fill-parent-with-little-margin/10125472#10125472) of mine, to overcome this you have to override your `getInsets()` method of your `JFrame`, though I still doubt, how much this answer will help here, except for making things more worse, in terms of calculations :-) – nIcE cOw May 13 '12 at 05:17