Results 1 to 4 of 4

Thread: Quick question

  1. #1
    Dark Knight is offline Member
    Join Date
    Feb 2014
    Posts
    20
    Rep Power
    0

    Default Quick question

    Hi,

    Im working my way through a bit of code out of a tutorial in order to help me understand Java a bit better and I have come across the following bit which I dont fully understand. Basically the code is for a game which is displayed in a window. The statement if(false) //full screen mode is confusing me a little. How does this if work? it doesn't seem to be testing for anything so howdoes it ever get set to full screen mode? Is the if even needed? why not just code windowed mode?

    Anyway, Thanks for any help I may get.

    public class Window extends JFrame{

    private Window()
    {
    // Sets the title for this frame.
    this.setTitle("My test");

    // Sets size of the frame.
    if(false) // Full screen mode
    {
    // Disables decorations for this frame.
    this.setUndecorated(true);
    // Puts the frame to full screen.
    this.setExtendedState(this.MAXIMIZED_BOTH);
    }
    else // Window mode
    {
    // Size of the frame.
    this.setSize(800, 600);
    // Puts frame to center of the screen.
    this.setLocationRelativeTo(null);
    // So that frame cannot be resizable by the user.
    this.setResizable(false);
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Quick question

    The if block in question will not be executed. I am unaware of why that would be there unless some code was removed. And there are other issues here. First, they extended JFrame which is rarely if ever needed. Second, they named their class Window which is already a JDK class. Duplicating names of JDK classes is begging for trouble. In fact, it is Frame that extends Window and JFrame that extends Frame in the JDK. So their class structure is somewhat backwards.

    Regards,
    Jim
    Last edited by jim829; 02-17-2014 at 12:08 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Dark Knight is offline Member
    Join Date
    Feb 2014
    Posts
    20
    Rep Power
    0

    Default Re: Quick question

    Thanks for the reply, Ill post the whole code for this class to see if it makes any more sense as to why window is extending JFrame, though perhaps as you say it could have been done differently.

    package moon_lander;

    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;

    /**
    * Creates frame and set its properties.
    *
    * @author Game tutorial
    */

    public class Window extends JFrame{

    private Window()
    {
    // Sets the title for this frame.
    this.setTitle("My test");

    // Sets size of the frame.
    if(false) // Full screen mode
    {
    // Disables decorations for this frame.
    this.setUndecorated(true);
    // Puts the frame to full screen.
    this.setExtendedState(this.MAXIMIZED_BOTH);
    }
    else // Window mode
    {
    // Size of the frame.
    this.setSize(800, 600);
    // Puts frame to center of the screen.
    this.setLocationRelativeTo(null);
    // So that frame cannot be resizable by the user.
    this.setResizable(false);
    }

    // Exit the application when user close frame.
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

    this.setContentPane(new Framework());

    this.setVisible(true);
    }

    public static void main(String[] args)
    {
    // Use the event dispatch thread to build the UI for thread-safety.
    SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
    new Window();
    }
    });
    }
    }

  4. #4
    Dark Knight is offline Member
    Join Date
    Feb 2014
    Posts
    20
    Rep Power
    0

    Default Re: Quick question

    I had a look and basically changed the code to this below, it seems to work. Do you see any issues with it?

    public static void main(String[] args) {
    JFrame f = new JFrame("This is a test zzz");
    f.setSize(800, 600);
    // Puts frame to center of the screen.
    f.setLocationRelativeTo(null);
    // So that frame cannot be resizable by the user.
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    f.setContentPane(new Framework());

    f.setVisible(true);

Similar Threads

  1. Quick Question
    By crmbuddy in forum New To Java
    Replies: 5
    Last Post: 02-11-2013, 05:08 AM
  2. Quick 'this' question
    By stuckonjava in forum New To Java
    Replies: 3
    Last Post: 05-08-2012, 07:21 AM
  3. Quick question.
    By juxta in forum New To Java
    Replies: 11
    Last Post: 12-09-2011, 03:23 AM
  4. quick question
    By vouslavous in forum Java Applets
    Replies: 4
    Last Post: 04-24-2009, 08:35 PM
  5. Hello everyone! quick question.
    By irishhokie in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 04:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •