Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Drawing in Full Screen

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Drawing in Full Screen

    I am attempting to write a program that can draw an image in full screen. I am trying to accomplish this without a paint() method and I am not sure how to go about it. Here is the current state of my code:
    package gametest;

    import java.awt.*;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class GameTest extends JFrame
    {
        private GraphicsDevice device;
     
        public GameTest(GraphicsDevice device) 
        {
            super(device.getDefaultConfiguration());
            this.device = device;
            setTitle("Game Test");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        private void initComponents(Container c) 
        {
            setContentPane(c);
            c.setLayout(new BorderLayout());
            // Current DM
            JPanel currentPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            c.add(currentPanel, BorderLayout.NORTH);
        }
     
        public void begin() 
        {
            setUndecorated(true);
            setResizable(false);
            device.setFullScreenWindow(this);
            validate();
        }
     
        public void render()
        {
            boolean done=false;
            while(!done)
            {
                Graphics g = getGraphics();
                ImageIcon icon = new ImageIcon("Test.jpeg");
                Image img = icon.getImage();
                g.drawImage(img, 0, 0, rootPane);
                g.dispose();
            }
        }
     
        public static void main(String[] args) 
        {
            GraphicsEnvironment env = GraphicsEnvironment.
                getLocalGraphicsEnvironment();
            GraphicsDevice[] devices = env.getScreenDevices();
            GameTest test = new GameTest(devices[0]);
            test.initComponents(test.getContentPane());
            test.begin();
            test.render();
        }
    }

    All I get is a full screen grey window.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Drawing in Full Screen

    Why are you trying to not use a paint method? Or better yet, a paintComponent() method? Your active rendering loop seems pretty suspect to me.

    Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Full Screen


  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Drawing in Full Screen

    My advice is to get it working using the paintComponent() method first. If you then really want to use active rendering, go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Full Screen

    Here is the current state of my code,

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package gametest;
     
    /**
     *
     * @author Mizuki
     */
    import java.awt.*;
    import javax.swing.*;
     
    public class GameTest extends JFrame
    {
        private GraphicsDevice device;
     
        public GameTest(GraphicsDevice device) 
        {
            super(device.getDefaultConfiguration());
            this.device = device;
            setTitle("Game Test");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        private void initComponents(Container c) 
        {
        }
     
        public void begin() 
        {
            setUndecorated(true);
            setResizable(false);
            device.setFullScreenWindow(this);
            validate();
        }
     
        public void render()
        {
            JLabel a;
            ImageIcon icon = createImageIcon("C:\\Users\\Mizuki\\Desktop\\Test.gif","work");
     
            if(icon!=null)
            {
                a = new javax.swing.JLabel(icon);
            }
            else
            {
                a = new javax.swing.JLabel("hello");
            }
     
            add(a);
        }
     
        public static void main(String[] args) 
        {
            GraphicsEnvironment env = GraphicsEnvironment.
                getLocalGraphicsEnvironment();
            GraphicsDevice[] devices = env.getScreenDevices();
            GameTest test = new GameTest(devices[0]);
            test.initComponents(test.getContentPane());
            test.begin();
            test.render();
            test.setVisible(true);
        }
        protected ImageIcon createImageIcon(String path, String description) 
        {
            java.net.URL imgURL = getClass().getResource(path);
            if (imgURL != null) 
            {
                return new ImageIcon(imgURL, description);
            } 
            else 
            {
            System.err.println("Couldn't find file: " + path);
            return null;
            }
        }
     
    }

    It is showing the JLabel saying hello and there is a system error that it can't find the path name. I have double and tripple checked that the path is correct and that the file is there, and tried a few different file types for the image. I am now confused...

    I also tried putting an images folder in my src (I am using netbeans) and using the path images/Test.jpeg, and it still doesn't work...
    Last edited by MizukiTHPS; March 27th, 2012 at 11:32 AM.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Drawing in Full Screen

    What is the absolute path of imgURL?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Drawing in Full Screen

    I am confused as to what you are asking... I tried giving it an absolute and a relative path...

    Edit: Never mind it is randomly working now... didn't even change anything.

    For the sake of anyone else, here is the final version of my code using graphics like originally intended.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package gametest;
     
    /**
     *
     * @author Mizuki
     */
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
     
    public class GameTest extends JFrame
    {
        private GraphicsDevice device;
     
        public GameTest(GraphicsDevice device) 
        {
            super(device.getDefaultConfiguration());
            this.device = device;
            setTitle("Game Test");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        private void initComponents(Container c) 
        {
        }
     
        public void begin() 
        {
            setUndecorated(true);
            setResizable(false);
            device.setFullScreenWindow(this);
            validate();
        }
     
        public void render()
        {
            JLabel a;
            ImageIcon icon = createImageIcon("Test.jpg","work");
     
            if(icon!=null)
            {
                a = new javax.swing.JLabel(icon);
            }
            else
            {
                a = new javax.swing.JLabel("hello");
            }
     
            Graphics g = this.getGraphics();
            g.drawImage(icon.getImage(), 0, 0, null);
        }
     
        public static void main(String[] args) 
        {
            GraphicsEnvironment env = GraphicsEnvironment.
                getLocalGraphicsEnvironment();
            GraphicsDevice[] devices = env.getScreenDevices();
            GameTest test = new GameTest(devices[0]);
            test.initComponents(test.getContentPane());
            test.begin();
            test.render();
            test.setVisible(true);
        }
        protected ImageIcon createImageIcon(String path, String description) 
        {
            java.net.URL imgURL = getClass().getResource(path);
            if (imgURL != null) 
            {
                return new ImageIcon(imgURL, description);
            } 
            else 
            {
            System.err.println("Couldn't find file: " + path);
            return null;
            }
        }
     
    }
    Last edited by MizukiTHPS; March 27th, 2012 at 11:53 AM.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Drawing in Full Screen

    Quote Originally Posted by MizukiTHPS View Post
    I am confused as to what you are asking... I tried giving it an absolute and a relative path...

    Edit: Never mind it is randomly working now... didn't even change anything.
    Sometimes that happens if you're working from an IDE. An IDE can get out of sync with the file system, so if you moved the image file, the IDE doesn't know about it and can't find it. Not intuitive, but doing a refresh seems to fix it most of the time.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 9
    Last Post: December 31st, 2011, 01:22 AM
  2. Full Window when run the program.
    By faysal40 in forum AWT / Java Swing
    Replies: 1
    Last Post: May 12th, 2011, 11:37 PM
  3. Replies: 4
    Last Post: July 20th, 2010, 07:15 AM
  4. Drawing "Hello world" on screen
    By shikh_albelad in forum Java SE APIs
    Replies: 4
    Last Post: June 11th, 2009, 03:21 AM