Results 1 to 6 of 6
  1. #1
    kratos9797 is offline Member
    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Default Retrieving full screen display to add menu.

    Hi everyone, I'm fairly new to programming. I found an older book on developing games in Java and have been trying out the examples in the book. For the most part things started off good in the first few chapters with just getting the basic stuff set up but then I ran into a snag when I got to adding an interactive menu to the display. I can create the menu but the way the code is explained in the book the menu is supposed to be added directly to the current/main game display. I know the problem is coming from this line below:

    JFrame frame = super.screen.getFullScreenWindow();

    This code is at lines 47 and 72 in the first .java file below

    Netbeans says that the JFrame and Window are incompatible class types or something like that, so it keeps compiling with errors.

    I tried creating the menu in a new JFrame but if I do that then the menu doesn't get added to the main game window/display like it's supposed to.

    I was hoping that someone would know of an alternative code to retrieve the current full screen display instead of using the above line so that I can get the game menu to work right.

    Any help would be greatly appreciated!

    Below is the .java file that the line is in:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    import com.brackeen.javagamebook.graphics.*;
    import com.brackeen.javagamebook.input.GameAction;
    
    /**
        Extends the InputManagerTest demo and adds Swing buttons
        for pause, config and quit.
    */
    public class MenuTest extends InputManagerTest
        implements ActionListener
    {
    
        public static void main(String[] args) {
            new MenuTest().run();
        }
    
        protected GameAction configAction;
    
        private JButton playButton;
        private JButton configButton;
        private JButton quitButton;
        private JButton pauseButton;
        private JPanel playButtonSpace;
    
        public void init() {
            super.init();
            // make sure Swing components don't paint themselves
            NullRepaintManager.install();
    
            // create an additional GameAction for "config"
            configAction = new GameAction("config");
    
            // create buttons
            quitButton = createButton("quit", "Quit");
            playButton = createButton("play", "Continue");
            pauseButton = createButton("pause", "Pause");
            configButton = createButton("config", "Change Settings");
    
            // create the space where the play/pause buttons go.
            playButtonSpace = new JPanel();
            playButtonSpace.setOpaque(false);
            playButtonSpace.add(pauseButton);
    
            JFrame frame = super.screen.getFullScreenWindow();
            Container contentPane = frame.getContentPane();
    
            // make sure the content pane is transparent
            if (contentPane instanceof JComponent) {
                ((JComponent)contentPane).setOpaque(false);
            }
    
            // add components to the screen's content pane
            contentPane.setLayout(new FlowLayout(FlowLayout.LEFT));
            contentPane.add(playButtonSpace);
            contentPane.add(configButton);
            contentPane.add(quitButton);
    
            // explicitly lay out components (needed on some systems)
            frame.validate();
        }
    
    
        /**
            Extends InputManagerTest's functionality to draw all
            Swing components.
        */
        public void draw(Graphics2D g) {
            super.draw(g);
            JFrame frame = super.screen.getFullScreenWindow();
    
            // the layered pane contains things like popups (tooltips,
            // popup menus) and the content pane.
            frame.getLayeredPane().paintComponents(g);
        }
    
    
        /**
            Changes the pause/play button whenever the pause state
            changes.
        */
        public void setPaused(boolean p) {
            super.setPaused(p);
            playButtonSpace.removeAll();
            if (isPaused()) {
                playButtonSpace.add(playButton);
            }
            else {
                playButtonSpace.add(pauseButton);
            }
        }
    
    
        /**
            Called by the AWT event dispatch thread when a button is
            pressed.
        */
        public void actionPerformed(ActionEvent e) {
            Object src = e.getSource();
            if (src == quitButton) {
                // fire the "exit" gameAction
                super.exit.tap();
            }
            else if (src == configButton) {
                // doesn't do anything (for now)
                configAction.tap();
            }
            else if (src == playButton || src == pauseButton) {
                // fire the "pause" gameAction
                super.pause.tap();
            }
        }
    
    
        /**
            Creates a Swing JButton. The image used for the button is
            located at "../images/menu/" + name + ".png". The image is
            modified to create a "default" look (translucent) and a
            "pressed" look (moved down and to the right).
            <p>The button doesn't use Swing's look-and-feel and
            instead just uses the image.
        */
        public JButton createButton(String name, String toolTip) {
    
            // load the image
            String imagePath = "../images/menu/" + name + ".png";
            ImageIcon iconRollover = new ImageIcon(imagePath);
            int w = iconRollover.getIconWidth();
            int h = iconRollover.getIconHeight();
    
            // get the cursor for this button
            Cursor cursor =
                Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
    
            // make translucent default image
            Image image = screen.createCompatibleImage(w, h,
                Transparency.TRANSLUCENT);
            Graphics2D g = (Graphics2D)image.getGraphics();
            Composite alpha = AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, .5f);
            g.setComposite(alpha);
            g.drawImage(iconRollover.getImage(), 0, 0, null);
            g.dispose();
            ImageIcon iconDefault = new ImageIcon(image);
    
            // make a pressed image
            image = screen.createCompatibleImage(w, h,
                Transparency.TRANSLUCENT);
            g = (Graphics2D)image.getGraphics();
            g.drawImage(iconRollover.getImage(), 2, 2, null);
            g.dispose();
            ImageIcon iconPressed = new ImageIcon(image);
    
            // create the button
            JButton button = new JButton();
            button.addActionListener(this);
            button.setIgnoreRepaint(true);
            button.setFocusable(false);
            button.setToolTipText(toolTip);
            button.setBorder(null);
            button.setContentAreaFilled(false);
            button.setCursor(cursor);
            button.setIcon(iconDefault);
            button.setRolloverIcon(iconRollover);
            button.setPressedIcon(iconPressed);
    
            return button;
        }

    Here is class for how the screen variable is created:

    Java Code:
    import java.awt.*;
    import java.awt.image.BufferStrategy;
    import javax.swing.JFrame;
    
    /**
        The ScreenManager class manages initializing and displaying
        full screen graphics modes.
    */
    public class ScreenManager {
    
        private GraphicsDevice device;
    
        /**
            Creates a new ScreenManager object.
        */
        public ScreenManager() {
            GraphicsEnvironment environment =
                GraphicsEnvironment.getLocalGraphicsEnvironment();
            device = environment.getDefaultScreenDevice();
        }
    
    
        /**
            Returns a list of compatible display modes for the
            default device on the system.
        */
        public DisplayMode[] getCompatibleDisplayModes() {
            return device.getDisplayModes();
        }
    
    
        /**
            Returns the first compatible mode in a list of modes.
            Returns null if no modes are compatible.
        */
        public DisplayMode findFirstCompatibleMode(
            DisplayMode modes[])
        {
            DisplayMode goodModes[] = device.getDisplayModes();
            for (int i = 0; i < modes.length; i++) {
                for (int j = 0; j < goodModes.length; j++) {
                    if (displayModesMatch(modes[i], goodModes[j])) {
                        return modes[i];
                    }
                }
    
            }
    
            return null;
        }
    
    
        /**
            Returns the current display mode.
        */
        public DisplayMode getCurrentDisplayMode() {
            return device.getDisplayMode();
        }
    
    
        /**
            Determines if two display modes "match". Two display
            modes match if they have the same resolution, bit depth,
            and refresh rate. The bit depth is ignored if one of the
            modes has a bit depth of DisplayMode.BIT_DEPTH_MULTI.
            Likewise, the refresh rate is ignored if one of the
            modes has a refresh rate of
            DisplayMode.REFRESH_RATE_UNKNOWN.
        */
        public boolean displayModesMatch(DisplayMode mode1,
            DisplayMode mode2)
        {
            if (mode1.getWidth() != mode2.getWidth() ||
                mode1.getHeight() != mode2.getHeight())
            {
                return false;
            }
    
            if (mode1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
                mode2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
                mode1.getBitDepth() != mode2.getBitDepth())
            {
                return false;
            }
    
            if (mode1.getRefreshRate() !=
                DisplayMode.REFRESH_RATE_UNKNOWN &&
                mode2.getRefreshRate() !=
                DisplayMode.REFRESH_RATE_UNKNOWN &&
                mode1.getRefreshRate() != mode2.getRefreshRate())
             {
                 return false;
             }
    
             return true;
        }
    
    
    /**
            Enters full screen mode and changes the display mode.
            If the specified display mode is null or not compatible
            with this device, or if the display mode cannot be
            changed on this system, the current display mode is used.
            <p>
            The display uses a BufferStrategy with 2 buffers.
        */
        public void setFullScreen(DisplayMode displayMode) {
            JFrame frame = new JFrame();
            frame.setUndecorated(true);
            frame.setIgnoreRepaint(true);
            frame.setResizable(false);
    
            device.setFullScreenWindow(frame);
            if (displayMode != null &&
                device.isDisplayChangeSupported())
            {
                try {
                    device.setDisplayMode(displayMode);
                }
                catch (IllegalArgumentException ex) { }
            }
            frame.createBufferStrategy(2);
        }
    
    
        /**
            Gets the graphics context for the display. The
            ScreenManager uses double buffering, so applications must
            call update() to show any graphics drawn.
            <p>
            The application must dispose of the graphics object.
        */
        public Graphics2D getGraphics() {
            Window window = device.getFullScreenWindow();
            if (window != null) {
                BufferStrategy strategy = window.getBufferStrategy();
                return (Graphics2D)strategy.getDrawGraphics();
            }
            else {
                return null;
            }
        }
    
    
        /**
            Updates the display.
        */
        public void update() {
            Window window = device.getFullScreenWindow();
            if (window != null) {
                BufferStrategy strategy = window.getBufferStrategy();
                if (!strategy.contentsLost()) {
                    strategy.show();
                }
            }
            // Sync the display on some systems.
            // (on Linux, this fixes event queue problems)
            Toolkit.getDefaultToolkit().sync();
        }
    
    
        /**
            Returns the window currently used in full screen mode.
            Returns null if the device is not in full screen mode.
        */
        public Window getFullScreenWindow() {
            return device.getFullScreenWindow();
        }
    
    
        /**
            Returns the width of the window currently used in full
            screen mode. Returns 0 if the device is not in full
            screen mode.
        */
        public int getWidth() {
            Window window = device.getFullScreenWindow();
            if (window != null) {
                return window.getWidth();
            }
            else {
                return 0;
            }
        }
    
    
        /**
            Returns the height of the window currently used in full
            screen mode. Returns 0 if the device is not in full
            screen mode.
        */
        public int getHeight() {
            Window window = device.getFullScreenWindow();
            if (window != null) {
                return window.getHeight();
            }
            else {
                return 0;
            }
        }
    
    
        /**
            Restores the screen's display mode.
        */
        public void restoreScreen() {
            Window window = device.getFullScreenWindow();
            if (window != null) {
                window.dispose();
            }
            device.setFullScreenWindow(null);
        }
    
    
        /**
            Creates an image compatible with the current display.
        */
        public BufferedImage createCompatibleImage(int w, int h,
            int transparency)
        {
            Window window = device.getFullScreenWindow();
            if (window != null) {
                GraphicsConfiguration gc =
                    window.getGraphicsConfiguration();
                return gc.createCompatibleImage(w, h, transparency);
            }
            return null;
        }
    }
    This is the class where the screen variable comes from (line 54):

    Java Code:
    package com.brackeen.javagamebook.test;
    
    import java.awt.*;
    import javax.swing.ImageIcon;
    
    import com.brackeen.javagamebook.graphics.ScreenManager;
    
    /**
        Simple abstract class used for testing. Subclasses should
        implement the draw() method.
    */
    public abstract class GameCore {
    
        protected static final int FONT_SIZE = 24;
    
        private static final DisplayMode POSSIBLE_MODES[] = {
            new DisplayMode(800, 600, 32, 0),
            new DisplayMode(800, 600, 24, 0),
            new DisplayMode(800, 600, 16, 0),
            new DisplayMode(640, 480, 32, 0),
            new DisplayMode(640, 480, 24, 0),
            new DisplayMode(640, 480, 16, 0)
        };
    
        private boolean isRunning;
        protected ScreenManager screen;
    
        /**
            Signals the game loop that it's time to quit
        */
        public void stop() {
            isRunning = false;
        }
    
    
        /**
            Calls init() and gameLoop()
        */
        public void run() {
            try {
                init();
                gameLoop();
            }
            finally {
                 screen.restoreScreen();
            }
        }
    
    
        /**
            Sets full screen mode and initiates and objects.
        */
        public void init() {
            screen = new ScreenManager();
            DisplayMode displayMode =
                screen.findFirstCompatibleMode(POSSIBLE_MODES);
            screen.setFullScreen(displayMode);
    
            Window window = screen.getFullScreenWindow();
            window.setFont(new Font("Dialog", Font.PLAIN, FONT_SIZE));
            window.setBackground(Color.blue);
            window.setForeground(Color.white);
    
            isRunning = true;
        }
    
    
        public Image loadImage(String fileName) {
            return new ImageIcon(fileName).getImage();
        }
    
    
        /**
            Runs through the game loop until stop() is called.
        */
        public void gameLoop() {
            long startTime = System.currentTimeMillis();
            long currTime = startTime;
    
            while (isRunning) {
                long elapsedTime =
                    System.currentTimeMillis() - currTime;
                currTime += elapsedTime;
    
                // update
                update(elapsedTime);
    
                // draw the screen
                Graphics2D g = screen.getGraphics();
                draw(g);
                g.dispose();
                screen.update();
    
                // take a nap
                try {
                    Thread.sleep(20);
                }
                catch (InterruptedException ex) { }
            }
        }
    
        /**
            Updates the state of the game/animation based on the
            amount of elapsed time that has passed.
        */
        public void update(long elapsedTime) {
            // do nothing
        }
    
    
        /**
            Draws to the screen. Subclasses must override this
            method.
        */
        public abstract void draw(Graphics2D g);
    
    }
    This is what it's supposed to look like:
    Retrieving full screen display to add menu.-jv-game-menu.png


    The menu buttons are supposed to appear in the upper left-had corner of the game screen like in the picture above.

    The line of code I originally posted was supposed to retrieve the game screen so that the menu could be added to it but since Netbeans says that JFrame and window are incompatible types it doesn't work.
    Last edited by kratos9797; 07-02-2013 at 12:31 AM. Reason: Adding full code.

  2. #2
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Retrieving full screen display to add menu.

    Quote Originally Posted by kratos9797 View Post
    Hi everyone, I'm fairly new to programming. I found an older book on developing games in Java and have been trying out the examples in the book. For the most part things started off good in the first few chapters with just getting the basic stuff set up but then I ran into a snag when I got to adding an interactive menu to the display. I can create the menu but the way the code is explained in the book the menu is supposed to be added directly to the current/main game display. I know the problem is coming from this line below:

    JFrame frame = super.screen.getFullScreenWindow();

    Netbeans says that the JFrame and Window are incompatible class types or something like that, so it keeps compiling with errors.

    I tried creating the menu in a new JFrame but if I do that then the menu doesn't get added to the main game window/display like it's supposed to.

    I was hoping that someone would know of an alternative code to retrieve the current full screen display instead of using the above line so that I can get the game menu to work right.

    Any help would be greatly appreciated!
    What exactly are you trying to do with that line of code. What is the method your calling returning? If it isn't returning an object of type JFrame, then it cannot be used like that in an assignment statement. Would you be able to post your source code (with code tags) so we could see it in context?

  3. #3
    kratos9797 is offline Member
    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Default Re: Retrieving full screen display to add menu.

    I updated my first post with the necessary code.

  4. #4
    chaucer345 is offline Member
    Join Date
    Dec 2012
    Posts
    44
    Rep Power
    0

    Default Re: Retrieving full screen display to add menu.

    Okay, this might not work, but have you tried making the ScreenManager class a subclass of JFrame, creating an instance of ScreenManager and then getting your content pane and everything else from that? Note, I have not try this, and I may have read the code incorrectly, but it seems plausible.

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

    Default Re: Retrieving full screen display to add menu.

    If you are serious about learning to program in Java then I would recommend either going thru the tutorials in my signature and/or buying a book on Java Programming. I do not know anything about the book you are using except that it sounds like it is for writing java based games. Usually, these types of books gloss over the finer points of Java in favor of simply getting a working game. It is important to understand why things work the way they do.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    kratos9797 is offline Member
    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Default Re: Retrieving full screen display to add menu.

    Quote Originally Posted by chaucer345 View Post
    Okay, this might not work, but have you tried making the ScreenManager class a subclass of JFrame, creating an instance of ScreenManager and then getting your content pane and everything else from that? Note, I have not try this, and I may have read the code incorrectly, but it seems plausible.
    I believe I have tried that but it still did not work. The reason that this particular line doesn't do what it's supposed to do is that before I can "get" my content pane from my frame I have to "get" my "window" first. Since the original line of code tries to "create" a JFrame by "getting" a Window it doesn't work since JFrames and windows are incompatible class types.


    Quote Originally Posted by jim829 View Post
    If you are serious about learning to program in Java then I would recommend either going thru the tutorials in my signature and/or buying a book on Java Programming. I do not know anything about the book you are using except that it sounds like it is for writing java based games. Usually, these types of books gloss over the finer points of Java in favor of simply getting a working game. It is important to understand why things work the way they do.

    Regards,
    Jim

    Thanks for the advice. I actually understand why the code isn't working correctly fairly well, I was just hoping someone may be able to provide a simple alternative to the incompatible classes or something so that I could get past that point.



    I've been messing around with various JFrame options and in doing so have come up with the following code in place of the code at line 47 of my MenuTest:

    Java Code:
            JFrame frame = super.screen;
            frame.setVisible(true);
            frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    I also changed the code at line 72 to the following:

    Java Code:
            JFrame frame = super.screen;
            frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    By eliminating the ".getFullScreenWindow()" from line 47 and then adding in the next 2 lines of code I have successfully accomplished having the images of my menu buttons appear on the main screen.

    However, the functionality of my buttons are separated from the images of the buttons. For some unknown reason, though the images of the buttons are passing through the graphics of my main screen the functionality is not, it is remaining in the new JFrame.

    I know this because, if I press ctrl + Esc to close my main/game screen and I hover the mouse over where the buttons would appear on the menu JFrame, the pointer changes to a pointing hand instead of staying as the default pointer.
    Last edited by kratos9797; 07-02-2013 at 03:33 AM. Reason: Update on progress of solving my problem.

Similar Threads

  1. Converting to full screen
    By dd2308 in forum New To Java
    Replies: 1
    Last Post: 09-11-2012, 04:33 AM
  2. Replies: 9
    Last Post: 09-06-2012, 09:37 AM
  3. Full screen problems
    By doctorned in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 09:40 AM
  4. Full screen test
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:24 PM
  5. Full screen
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 05:49 AM

Tags for this Thread

Posting Permissions

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