Results 1 to 12 of 12
  1. #1
    kamirru is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default Full Screen Image Viewer Java

    Hi,

    I have a question about the full screen mode in JAVA. I write a very simple image viewer and got a problem with the full screen.
    Could you help me to write the code in the proper way, because now I get a black screen when I try to go to the full screen mode...

    Here is the code:

    I would appreciate the help as I am just started my adventure with JAVA...


    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package projekt;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.io.File;
    import javax.swing.Action;
    import javax.swing.ImageIcon;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    
    
    
    
    public class viewer extends JFrame {
    
    
    
        private JMenuBar menubar;
        private JMenu mFile;
        private JMenu mHelp;
        private JMenuItem mIOpen;
        private JMenuItem mIFullScreen;
        private JMenuItem mIExit;
        private JMenuItem mIAbout;
    
        private JScrollPane sPPicture;
        private JLabel lPicture;
    
        private void init (){
        menubar= new JMenuBar();
        mFile = new JMenu ("File");
        mHelp = new JMenu ("Help");
        mIOpen = new JMenuItem ("Open");
        mIFullScreen = new JMenuItem ("Full Screen");
        mIExit = new JMenuItem ("Exit");
        mIAbout = new JMenuItem ("About...");
        menubar.add(mFile);
        menubar.add(mHelp);
        mFile.add(mIOpen);
        mFile.add(mIFullScreen);
        mFile.add(mIExit);
        mHelp.add(mIAbout);
        setJMenuBar(menubar);
        setSize (500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    
        
        lPicture = new JLabel();
        Container show = getContentPane();
        show.add(lPicture);
    
        sPPicture = new JScrollPane();
        sPPicture.getViewport().add(lPicture);
        getContentPane().add(BorderLayout.CENTER, sPPicture);
    
        
    
             mIOpen.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    Open ();
    
                                             }
                
            });
    
      
    
             mIExit.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    Exit();
                }
            });
    
             mIAbout.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    About ();
                }
            });
    
            mIFullScreen.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    FullScreen();
                }
            });
    
    
    
        }
    
          public Semestr2() {
             super("Image Viewer");
             init();
         }
         public static void  main(String[] args) {
             new Semestr2().setVisible(true);
         }
    
    
    
         private JFileChooser fC=new JFileChooser();
    
         private void Open() {
             if(fC.showOpenDialog(this)==JFileChooser.APPROVE_OPTION) {
    
                 ImageIcon image=new ImageIcon(fC.getSelectedFile().getAbsolutePath());
                 lPicture.setIcon(image);
    
    
             }
         }
    
    
             public  void FullScreen() {
    
            if(fC.showOpenDialog(this)==JFileChooser.APPROVE_OPTION) {
            ImageIcon image=new ImageIcon(fC.getSelectedFile().getAbsolutePath());
    
    
            GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice device = env.getDefaultScreenDevice();
            GraphicsConfiguration gc = device.getDefaultConfiguration();
            final Frame mainFrame = new Frame(gc);
            mainFrame.setUndecorated(true);
            mainFrame.setIgnoreRepaint(true);
            device.setFullScreenWindow(mainFrame);
            Graphics gr=mainFrame.getGraphics();
            lPicture.setIcon(image);
            gr.fillRect(0,0,mainFrame.getWidth(),mainFrame.getHeight());
            mainFrame.addKeyListener(new KeyAdapter() {
    
                
                public void keyPressed(KeyEvent ev) {
                    mainFrame.setVisible(false);
                    System.exit(0);
        }
            });
                 }
        }
    
         private void Exit (){
             System.exit(0);
         }
    
         private void About(){
             JOptionPane.showMessageDialog(null, "Image Viewer 2011");
         }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     
        }

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Have you tried removing setIgnoreRepaint(true)?

  3. #3
    kamirru is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Yes, and it is quite the same issue, now I get a white screen. Maybe I try to load the picture wrong?

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    Hmm. Probably found the issue:


    Java Code:
            Graphics gr=mainFrame.getGraphics();
            lPicture.setIcon(image);
            gr.fillRect(0,0,mainFrame.getWidth(),mainFrame.getHeight());
    First you get the Graphics (okay, good to have), then display the picture (I assume; never really used JLabel and setIcon in combination)... and then paint the whole screen white. Assuming white is the default color. Try removing the last line there, or add a gr.drawImage after it (removing the lPicture bit).

  5. #5
    kamirru is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Do you mean this?

    Java Code:
            Graphics gr=mainFrame.getGraphics();
            lPicture.setIcon(image);
    or

    Java Code:
            Graphics gr=mainFrame.getGraphics();
            gr.drawImage(null, WIDTH, WIDTH, WIDTH, WIDTH, rootPane);
            gr.fillRect(0,0,mainFrame.getWidth(),mainFrame.getHeight());
    Both ways don`t work out... Or maybe I missunderstood you? How would you write the code?

  6. #6
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    You'd have to paint the picture after you clear the screen. So something along the lines of

    Java Code:
    Graphics gr=mainFrame.getGraphics();
    gr.fillRect(0, 0, mainFrame.getWidth(), mainFrame.getHeight());
    gr.drawImage(image, 0, 0, mainFrame.getWidth(), mainFrame.getHeight(), null);
    gr.dispose();
    (note: This is taken straight from my head, and I might be forgetting some things in it. YMMV)

  7. #7
    kamirru is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    there is something wrong with the IMAGE part I think. Well I will think about it tomorow... there is 11 P.M. where I live....
    Java Code:
    gr.drawImage(image, 0, 0, mainFrame.getWidth(), mainFrame.getHeight(), null);

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    You should never use the getGraphics() method to do custom painting.

    In your normal JFrame you use a JLabel with an Icon to display the image. Why not do the same thing with when using full screen mode? Also why not use a JFrame?

  9. #9
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    There we go; managed to get it working. First of all... You don't add the lPicture to your mainFrame; you essentially display an empty frame.

    Second, you use setIgnoreRepaint(true), which makes it... well, not repaint, which means no objects would be drawn anyway.

    Interestingly enough, it didn't make a difference if I had gr.fillRect in or not; probably because it repaints when it goes to fullscreen anyway. So you can remove your Graphics part entirely.

  10. #10
    kamirru is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    I have re-written the Full Screen part and tried to do it as according to your tipps....
    Thats the code:

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package projekt;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.GraphicsConfiguration;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.Action;
    import javax.swing.ImageIcon;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    
    
    
    
    public class Viewer extends JFrame {
    
    
    
        private JMenuBar menubar;
        private JMenu mFile;
        private JMenu mHelp;
        private JMenuItem mIOpen;
        private JMenuItem mIFullScreen;
        private JMenuItem mIExit;
        private JMenuItem mIAbout;
    
        private JScrollPane sPPicture;
        private JLabel lPicture;
    
        private void init (){
        menubar= new JMenuBar();
        mFile = new JMenu ("File");
        mHelp = new JMenu ("Help");
        mIOpen = new JMenuItem ("Open");
        mIFullScreen = new JMenuItem ("Full Screen");
        mIExit = new JMenuItem ("Exit");
        mIAbout = new JMenuItem ("About...");
        menubar.add(mFile);
        menubar.add(mHelp);
        mFile.add(mIOpen);
        mFile.add(mIFullScreen);
        mFile.add(mIExit);
        mHelp.add(mIAbout);
        setJMenuBar(menubar);
        setSize (500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    
        
    
        
        lPicture = new JLabel();
        Container show = getContentPane();
        show.add(lPicture);
    
        sPPicture = new JScrollPane();
        sPPicture.getViewport().add(lPicture);
        getContentPane().add(BorderLayout.CENTER, sPPicture);
    
        
    
             mIOpen.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    Open ();
    
                                             }
                
            });
    
      
    
             mIExit.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    Exit();
                }
            });
    
             mIAbout.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    About ();
                }
            });
    
            mIFullScreen.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                                    try {
                        FullScreen(getGraphics());
                    } catch (IOException ex) {
                        Logger.getLogger(Viewer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
    
    
    
        }
    
          public Viewer() {
             super("Image Viewer");
             init();
         }
         public static void  main(String[] args) {
             new Viewer().setVisible(true);
         }
    
    
    
         private JFileChooser fC=new JFileChooser();
         int w, h;
        Image image;
        File f;
    
    
    
         private void Open() {
             if(fC.showOpenDialog(this)==JFileChooser.APPROVE_OPTION) {
    
                 ImageIcon image=new ImageIcon(fC.getSelectedFile().getAbsolutePath());
                 lPicture.setIcon(image);
    
    
             }
         }
    
            private void setFullScreen(final JFrame frame) {
            frame.dispose();
            frame.setResizable(true);
            frame.setUndecorated(true);
            frame.setLocation(0, 0);
            frame.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
            frame.setVisible(true);
            frame.repaint();
    
            frame.addKeyListener(new KeyAdapter() {
                public void keyPressed(KeyEvent ev) {
                    frame.setVisible(false);
                    System.exit(0);
                }
            });
        }
    
                public void FullScreen(Graphics g) throws IOException {
                Open();
    
            menubar.setVisible(false);
            setFullScreen(Semestr2.this);
            w = this.getWidth();
            h = this.getHeight();
            f = new File(fC.getSelectedFile().getAbsolutePath());
            image = ImageIO.read(f);
    
    
    
            if (image != null) {
                g.drawImage(image, w / 2 - image.getWidth(this) / 2, h / 2 - image.getHeight(this) / 2, this);
            }
    
    
    
    
        }
    
    
    
    
         private void Exit (){
             System.exit(0);
         }
    
         private void About(){
             JOptionPane.showMessageDialog(null, "Image Viewer 2011");
         }
    
    
     
        }
    But I have got one more problem... When I open a picture, which is bigger than screen I am getting the scroll bars.... Is there a way to adjust the picture to the screen?

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Read the API for Graphics and use a suitable overload of drawImage(...)

    db

  12. #12
    kamirru is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Hi,

    I tried once more time to read it, but the only thing I understood is I should use:

    Java Code:
    drawImage
    
    public abstract boolean drawImage(Image img,
                                      int dx1,
                                      int dy1,
                                      int dx2,
                                      int dy2,
                                      int sx1,
                                      int sy1,
                                      int sx2,
                                      int sy2,
                                      ImageObserver observer)
    
        Draws as much of the specified area of the specified image as is currently available, scaling it on the fly to fit inside the specified area of the destination drawable surface. Transparent pixels do not affect whatever pixels are already there.
    is that correct? But how do I get the coordinate of the corners?

    P.S. sorry for the amount of question, but I am a beginner and somethimes do not really understand what API "says"...

Similar Threads

  1. Replies: 0
    Last Post: 04-05-2010, 01:38 AM
  2. Full screen problems
    By doctorned in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 09:40 AM
  3. Full screen test
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:24 PM
  4. how to set full screen dimensions
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 06:08 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
  •