0

I am trying to add a jinternal frame to a frame that is in full screen exclusive mode. However when i add it it fills up the whole screen with white. Here is my code.

    JFrame f = new JFrame();
    f.setTitle("Platform Game"); 
    f.setLocationRelativeTo(null); 
    f.setUndecorated(true);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    gs.setFullScreenWindow(f);
    f.setVisible(true);
    createFrame(f);


protected static void createFrame(JFrame f)
{
    JInternalFrame frame = new JInternalFrame("Options", false, true, false, false);
    frame.setVisible(true);
    frame.setSize(10, 10);
    frame.setLocation(10, 10);
    JDesktopPane pane = new JDesktopPane();
    pane.add(frame);
    f.setContentPane(pane);

    try 
    {
        frame.setSelected(true);
    } 
    catch (java.beans.PropertyVetoException e) 
    {
        e.printStackTrace();
    }
}

If anyone knows how to do this properly please post it, or tell me what i am doing wrong.

Details

OS : windows 7

JDK : jdk 1.7.0_11

IDE : eclipse

I am using a panel on the jframe, and i am drawing on the panel

painting code

public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    for(int i = 0; i<1000; i++)
        g.drawImage(bi, i*bi.getWidth()-getXs(), 0, bi.getWidth(), Main.HEIGHT, null);

    g.setColor(Color.WHITE);

    for(int x = 0; x<Main.X_TILES; x++)
    {
        for(int y = 0; y<Main.Y_TILES; y++)
        {
            Block block = map[x][y];

            if(block!=null && x*Block.WIDTH>=xs-Block.WIDTH && x*Block.WIDTH<=xs+Main.WIDTH)
                g.drawImage(block.getImage(x,y), x*Block.WIDTH-getXs(), y*Block.HEIGHT, Block.WIDTH, Block.HEIGHT, null);
        }
    }

    for(Entity entity : entities)
    {
        if(entity!=null && entity.getX()>=xs-Block.WIDTH && entity.getX()<=xs+Main.WIDTH)
            g.drawImage(entity.getImage(), entity.getX()-getXs(), entity.getY(), entity.getWidth(), entity.getHeight(), null);
    }

    if(displayDebug)
    {
        g.drawString("Free memory "+Runtime.getRuntime().freeMemory()/1024+" KB", 10, 12);
        g.drawString("Total memory "+Runtime.getRuntime().totalMemory()/1024+" KB", 10, 24);
        g.drawString("Max memory "+Runtime.getRuntime().maxMemory()/1024+" KB", 10, 36);
        g.drawString("X "+character.getX(), 10, 48);
        g.drawString("Y "+character.getY(), 10, 60);
        g.drawString("XS "+xs, 10, 72);
    }

    g.setColor(Color.BLACK);
    g.drawString("Life "+character.getHealth(), 700, 12);
    g.dispose();
}

I saw an answer to a similar question JInternalFrame in full-screen mode that said to put it directly on panel but i tried that and it still didn't work.

EDIT: When i commented out my painting code, and i added it directly to the panel instead it worked, however it wouldn't drag around, so my new question is how do i make it appear when a paint job is being done. Is their a way to draw a component on using paint component?

For clarification it still didn't work normally when added to the panel without the painting code.

Community
  • 1
  • 1
Josh Sobel
  • 1,268
  • 3
  • 14
  • 27

1 Answers1

1

You are saying you add a JPanel (2nd code snippet) to your frame, but in fact you are replacing the whole contents of your frame with the JDesktopPane in this line:

f.setContentPane(pane);

so unless you add your Game Panel (the one with the overridden paintComponent method) somewhere on the Frame's glass pane or somewhere similarly odd, it will never be displayed, since the JDesktopPane (and the JInternalFrame you add to it) are the only components on the frame at this point.

Also, for completeness sake, you should move your setVisible(true) invocation to the very end of the initialization (i.e. after createFrame(f);) and completely remove the call to dispose() in your paintComponent method (g.dispose();).

At least this is what I imagine going on, try posting a more concise and self contained example that exhibits the problem you're having if that doesn't help.

Here's a modified example that will display custom graphics (just text in this case) and a JInternalFrame in the same full screen component:

public class FrameTest {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setTitle("Platform Game");
        f.setLocationRelativeTo(null);
        f.setUndecorated(true);
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        gs.setFullScreenWindow(f);

        JDesktopPane pane = new MyPanel();
        JInternalFrame frame = new JInternalFrame("Options", false, true, false, false);
        frame.setVisible(true);
        frame.setSize(100, 100);
        frame.setLocation(10, 10);
        pane.add(frame);
        f.setContentPane(pane);

        f.setVisible(true);
    }

    private static class MyPanel extends JDesktopPane {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(Color.BLACK);

            g.drawString("Free memory " + Runtime.getRuntime().freeMemory() / 1024 + " KB", 10, 12);
            g.drawString("Total memory " + Runtime.getRuntime().totalMemory() / 1024 + " KB", 10, 24);
            g.drawString("Max memory " + Runtime.getRuntime().maxMemory() / 1024 + " KB", 10, 36);
        }
    }
}
jkovacs
  • 3,470
  • 1
  • 23
  • 24
  • i tried doing this but i cant figure out how to add the panel and the internal frame at the same time. – Josh Sobel Apr 05 '13 at 21:56
  • @JoshSobel for example, instead of extending your "Game Panel" from JPanel, extend from JDesktopPane in order to use the same component for both your custom painting and the JInternalFrame. OR, since JDesktopPane extends JLayeredPane, use its mechanism to add your custom panel to a layer lower than the default layer. – jkovacs Apr 05 '13 at 21:58
  • ill try that tomorrow going to bed now if it works ill accept ur answer – Josh Sobel Apr 06 '13 at 01:53
  • i tried making my panel extend jdesktop pane then i set my panel as the content pane on the frame than when i tried adding the jinternal frame to the panel it still didnt work – Josh Sobel Apr 06 '13 at 11:26
  • @JoshSobel I have added an example to my post. When running it displays the custom graphics (just text in this case) in the background of the fullscreen frame with a functional JInternalFrame in the foreground. Can you check if the example works on your machine and, if necessary, update your original post with your current code? – jkovacs Apr 06 '13 at 13:15