1

I am creating a pong game in Java. I originally had an undecorated JFrame with a width of 700 and a height of 400. I later changed my mind and switched to a decorated frame. The error is that the ball travels off of the screen for the top 30px or so in the decorated frame. I measured the screen, and indeed it is 700 x 400 when decorated; however, I need the area where the ball will be painted to be 700 x 400, not the entire JFrame. What can I do?

How I set the size of my frame:

this.setSize(new Dimension(WIDTH, HEIGHT));

I am happy to post all of the code needed to solve this problem upon request.

EDIT

import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Tester extends JFrame {

    public Tester() {
        this.add(new Window());
        pack();

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
    }

    private void formMouseClicked(java.awt.event.MouseEvent evt) {                                  
        System.out.println("X: " + evt.getX() + ", Y: " + evt.getY());
    }  

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Tester().setVisible(true);
            }
        });
    }

    public class Window extends JComponent {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    }
}
Arc
  • 441
  • 1
  • 9
  • 26
  • 1
    `gamePanel.setPreferredSize(new java.awt.Dimension(700, 400));` See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) I suggested to **`@Override`** the method because that is the correct way to go about it. – Andrew Thompson Dec 21 '14 at 16:30

1 Answers1

2

What can I do?

Do the custom painting in a panel that is added to the frame. That way the co-ordinates will be as you expect. Another good idea of using this approach is to override getPreferredSize() to return the size of the viewable play area, then pack() the frame to get it perfect size for the content.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Could you please post the code on how I would go upon doing this? – Arc Dec 21 '14 at 14:42
  • 2
    *"Could you please post the code.."* SO is not a code generation service. Why not try to implement it, and get back to us with an [MCVE](http://stackoverflow.com/help/mcve) of your best attempt? – Andrew Thompson Dec 21 '14 at 15:00
  • Having said that, here is a [small example](http://stackoverflow.com/questions/25965176/jframe-code-compiles-and-runs-but-does-not-open-window/25965564#25965564) that I had lying around.. – Andrew Thompson Dec 21 '14 at 15:05
  • "SO is not a code generation service." I know, and I'm sorry. I just had no idea at all what you were talking about. I'm working on implementing it right now and I'll send an update afterwards. – Arc Dec 21 '14 at 15:19
  • Ok, so just to recap: is my problem that I set the screen's size to 700 x 400, but Java automatically designates the title bar within those coordinates? Also, check my edited question. I attempted something similar to what you recommended; however, it still doesn't work. – Arc Dec 21 '14 at 16:25
  • 1
    When I suggest you post an MCVE, I mean an MCVE, not *uncompilable code snippets.* I linked to the article about an MCVE, as well as the linked code example being in the form of an MCVE. Read the article, understand what it is telling you, then *post an MCVE.* – Andrew Thompson Dec 21 '14 at 16:29
  • Once again, sorry. I re-edited my original post. It still has the same problem of being 700 x 400, but the area where I would draw is much smaller; 687 x 364, or something along those lines. – Arc Dec 21 '14 at 16:51
  • That code is overriding the preferred size of the *frame*, as opposed to a child component as I advised. But ..heck, take the comment lines out of the code I linked to and there is an example in 25 LOC! Does *that* example work as advertised? (I'm having trouble understanding how anyone could manage to stuff this up when supplied with such a short, working example!) – Andrew Thompson Dec 21 '14 at 17:17
  • Thank you, your example is great and works as promised. I selected as best answer. I would up vote, but I need 15 rep. – Arc Dec 21 '14 at 17:49
  • Hold on, I'm still having a problem. The x and y coordinates of where I'm supposed to be drawing are still off. Check my re-edited question. The top left of where I'm supposed to be drawing is supposed to be (0, 0); however, it is not. – Arc Dec 21 '14 at 18:07
  • Yes I know, but I'm still having a problem. Please do read my comment above, thanks. – Arc Dec 21 '14 at 18:13
  • 1
    *"Hold on, I'm still having a problem."* That's because you're still 'coding by magic'. That is what I call copy/pasting bits and pieces of code hoping they will work ..magically. It is necessary to *understand* what is going on the pasted code. This requires checking methods in the Java Docs and doing other tests and research. The problem *this time* is that the mouse listener is being added to the frame rather than the custom painted component. If you had tried drawing something at a specific co-ordinate in the `JComponent`, it would be obvious that it is relative to the panel. – Andrew Thompson Dec 21 '14 at 18:13
  • Also note that you seem to be treating SO like a help desk. I've already answered the original question (which was about rendering coords, not a mouse listener).. – Andrew Thompson Dec 21 '14 at 18:14
  • Your advice, "If you had tried drawing something at a specific co-ordinate in the JComponent, it would be obvious that it is relative to the panel," helped to solve the problems. Thank you. – Arc Dec 21 '14 at 18:20
  • On a side note, originally I did not know what was causing the problem; hence my statement: "I am happy to post all of the code needed to solve this problem upon request." I thought the problem was with the drawing area not being the full size; however, upon your help with fixing that problem, I realized another which concerned the x and y coordinates. I did not want to post another question with similar code for fear of it being called "duplicate"; so, I asked another simple question which you helped to solve immediately. – Arc Dec 21 '14 at 18:26
  • I've been very polite with my questions for you, but I can't help but mention your condescending attitude with me. Yes, I am an amateur programmer, so patience is needed with me. Thank you for your help Andrew. – Arc Dec 21 '14 at 18:28