Results 1 to 12 of 12
Thread: Full Screen Image Viewer Java
- 06-14-2011, 09:10 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
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"); } }
- 06-14-2011, 09:17 PM #2
Have you tried removing setIgnoreRepaint(true)?
- 06-14-2011, 09:34 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
Yes, and it is quite the same issue, now I get a white screen. Maybe I try to load the picture wrong?
- 06-14-2011, 09:40 PM #4
Hmm. Probably found the issue:
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).Java Code:Graphics gr=mainFrame.getGraphics(); lPicture.setIcon(image); gr.fillRect(0,0,mainFrame.getWidth(),mainFrame.getHeight());
- 06-14-2011, 10:00 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
Do you mean this?
orJava Code:Graphics gr=mainFrame.getGraphics(); lPicture.setIcon(image);
Both ways don`t work out... Or maybe I missunderstood you? How would you write the code?Java Code:Graphics gr=mainFrame.getGraphics(); gr.drawImage(null, WIDTH, WIDTH, WIDTH, WIDTH, rootPane); gr.fillRect(0,0,mainFrame.getWidth(),mainFrame.getHeight());
- 06-14-2011, 10:13 PM #6
You'd have to paint the picture after you clear the screen. So something along the lines of
(note: This is taken straight from my head, and I might be forgetting some things in it. YMMV)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();
- 06-14-2011, 10:40 PM #7
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
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);
- 06-14-2011, 10:52 PM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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?
- 06-15-2011, 12:13 AM #9
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.
- 06-15-2011, 07:50 PM #10
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
I have re-written the Full Screen part and tried to do it as according to your tipps....
Thats the code:
But I have got one more problem... When I open a picture, which is bigger than screen I am getting the scroll barsJava 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"); } }
.... Is there a way to adjust the picture to the screen?
- 06-15-2011, 08:59 PM #11
Read the API for Graphics and use a suitable overload of drawImage(...)
db
- 06-16-2011, 06:45 PM #12
Member
- Join Date
- Jun 2011
- Posts
- 6
- Rep Power
- 0
Hi,
I tried once more time to read it, but the only thing I understood is I should use:
is that correct? But how do I get the coordinate of the corners?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.
P.S. sorry for the amount of question, but I am a beginner and somethimes do not really understand what API "says"...
Similar Threads
-
Render Java App Over Non-Java App In Direct3D Full Screen
By amp88 in forum Java 2DReplies: 0Last Post: 04-05-2010, 01:38 AM -
Full screen problems
By doctorned in forum New To JavaReplies: 4Last Post: 12-10-2009, 09:40 AM -
Full screen test
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 11:24 PM -
how to set full screen dimensions
By valery in forum New To JavaReplies: 1Last Post: 08-03-2007, 06:08 PM -
Full screen
By Jack in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 05:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks