Results 1 to 5 of 5
  1. #1
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Displaying a JTextField in fullscreen exclusive mode without flickering

    I have been stuck on this now for almost a week, the problem is that the textfield keeps flickering. I have read that swing does not work well with fullscreen exclusive mode, but I'm not sure what else to do other than create my own class similar to a textfield and detect every single key press which doesn't seem the right thing to do. I have created a cut down version of the problem and have pasted below, any help would be appreciated :)
    - If this line is uncommented "TextFieldTest.this.tfChatField.paint(g);" then it will display the textfield at the top of the screen, and a flickering textfield at the bottom(which is where it's meant to be)

    Java Code:
    package textfieldtest;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.DisplayMode;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.image.BufferStrategy;
    import java.lang.reflect.InvocationTargetException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class TextFieldTest implements Runnable {
        
        private JFrame frame;
        private BufferStrategy bufferStrategy;
        private JTextField tfChatField;
    
        public TextFieldTest(){
            frame = new JFrame();
            tfChatField = new JTextField("Type here to chat");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(tfChatField, BorderLayout.SOUTH);
            frame.setVisible(true);
            frame.createBufferStrategy(2);
            DisplayMode dm = new DisplayMode(640,480,32,60);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            gd.setFullScreenWindow(frame);
            gd.setDisplayMode(dm);
            bufferStrategy = frame.getBufferStrategy();
        }
        public static void main(String[] args) {
            TextFieldTest test = new TextFieldTest();
            new Thread(test).start();
        }
        
        public void render(final Graphics g){
            g.setColor(Color.black);
            g.fillRect(0, 0, 640, 480);
            try {
                SwingUtilities.invokeAndWait(new Runnable() {
                        @Override
                        public void run() {
                            //TextFieldTest.this.tfChatField.paint(g);
                        }
                });
            } catch (InterruptedException ex) {
                Logger.getLogger(TextFieldTest.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(TextFieldTest.class.getName()).log(Level.SEVERE, null, ex);
            }
            g.dispose();
            bufferStrategy.show();
        }
    
        @Override
        public void run() {
            while(true){
                Graphics g = bufferStrategy.getDrawGraphics();
                render(g);
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
    Last edited by Teedo; 03-02-2014 at 01:45 AM.

  2. #2
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Displaying a JTextField in fullscreen exclusive mode without flickering

    I'm just attempting to implement chatting (like in any multiplayer game) in my full screen game......the work-around way I can think of would involve creating my own class with a LOT of if statements checking if keys A-Z,0-9 and shift are pressed, and then that would create too many strings since the text needs to displayed on the screen as the user types.

  3. #3
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Displaying a JTextField in fullscreen exclusive mode without flickering

    The following code has had the best results so far...There is a solid JTextField being shown at the top of the screen(though it should be at the bottom), and there is a flickering copy of it being displayed at the bottom.

    Java Code:
    package textfieldtest;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.DisplayMode;
    import java.awt.Graphics;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.image.BufferStrategy;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    public class TextFieldTest implements Runnable {
        
        private JFrame frame;
        private BufferStrategy bufferStrategy;
        private JTextField tfChatField;
    
        public TextFieldTest(){
            frame = new JFrame();
            tfChatField = new JTextField("Type here to chat");
            frame.setIgnoreRepaint(true);
            tfChatField.setIgnoreRepaint(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(tfChatField, BorderLayout.SOUTH);
            frame.setVisible(true);
            frame.createBufferStrategy(2);
            DisplayMode dm = new DisplayMode(640,480,32,60);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gd.getDefaultConfiguration();
            gd.setFullScreenWindow(frame);
            gd.setDisplayMode(dm);
            bufferStrategy = frame.getBufferStrategy();
        }
        public static void main(String[] args) {
            TextFieldTest test = new TextFieldTest();
            new Thread(test).start();
        }
        
        public void render(final Graphics g){
            g.setColor(Color.black);
            g.fillRect(0, 0, 640, 480);
            tfChatField.paint(g);
            g.dispose();
            bufferStrategy.show();
        }
    
        @Override
        public void run() {
            while(true){
                Graphics g = bufferStrategy.getDrawGraphics();
                render(g);
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Displaying a JTextField in fullscreen exclusive mode without flickering

    What you're trying to do is totally bizarre. I don't think there's any "right" way to do it.

    If you're making a game, why don't you use something like LWJGL or Slick2D?
    Get in the habit of using standard Java naming conventions!

  5. #5
    Teedo is offline Member
    Join Date
    Apr 2012
    Posts
    61
    Rep Power
    0

    Default Re: Displaying a JTextField in fullscreen exclusive mode without flickering

    Nevermind, i decided to create my own custom class to use for chatting and it wasn't as difficult as I thought it would be :)
    Lesson learnt: Don't use JComponents with active rendering

Similar Threads

  1. Displaying a JTextField?
    By Gamedisolver in forum AWT / Swing
    Replies: 1
    Last Post: 10-23-2012, 04:57 PM
  2. JTextArea in Full-Screen Exclusive Mode
    By Willi in forum AWT / Swing
    Replies: 7
    Last Post: 10-14-2009, 09:10 PM
  3. Replies: 4
    Last Post: 10-13-2009, 03:10 PM
  4. displaying a formatted output on a jTextField
    By techbossmb in forum AWT / Swing
    Replies: 1
    Last Post: 09-24-2009, 05:56 PM
  5. Fullscreen mode problems
    By devunion in forum AWT / Swing
    Replies: 1
    Last Post: 04-01-2009, 05:07 PM

Posting Permissions

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