Results 1 to 3 of 3
  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default problems with outputting to screen

    I've started the newboston java gmae developement tutorials and I have a problem, I've set the background to PINK but whenever I run it its actually black. can someone give an explaination and possible fix for why this might be the case?

    Java Code:
    package gameDevelopement;
    
    import java.awt.*;
    import javax.swing.JFrame;
    
    public class Game extends JFrame
    {
    	public void run(DisplayMode dm)
    	{
    		// set window properties
    		setBackground(Color.PINK);
    		setForeground(Color.WHITE);
    		setFont(new Font("Arial", Font.PLAIN, 24));
    		
    		// screen variable to use screen methods
    		Screen screen = new Screen();
    		try
    		{
    			// set full screen
    			screen.setFullScreen(dm, this);
    			try
    			{
    				// do nothing for 5 secs
    				Thread.sleep(5000);
    			}
    			catch(Exception ex)
    			{
    				
    			}
    		}
    		finally
    		{
    			// after 5 secs return to regular size
    			screen.restoreScreen();
    		}
    	}
    	
    	public void paint(Graphics g)
    	{
    		// whenever you make a JFrame it calls the paint method automatically
    		g.drawString("This is gonna be awesome", 200, 200);
    	}
    	
    	public static void main(String[] args)
    	{
    		// in order to use displayMode method we need a display mode object
    		DisplayMode dm = new DisplayMode(800, 600, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
    		// we need an object of this class for run method
    		Game game = new Game();
    		game.run(dm);
    	}
    }
    Java Code:
    package gameDevelopement;
    
    import java.awt.*;
    import javax.swing.JFrame;
    
    public class Screen 
    {
    	//gives interface to graphics card
    	private GraphicsDevice gc;
    	
    	public Screen()
    	{
    		//env contains all your graphics device objects
    		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    		//gives access to computer screen
    		gc = env.getDefaultScreenDevice();
    	}
    	
    	// convert window to full screen
    	// DisplayMode contains 4 parameters (screen res x, screen res y, bit depth, refresh rate)
    	public void setFullScreen(DisplayMode dm, JFrame window)
    	{
    		//no tool bars
    		window.setUndecorated(true);
    		//can not be resized
    		window.setResizable(false);
    		//converts window to full screen
    		gc.setFullScreenWindow(window);
    		
    		// if graphics card is able to display those settings
    		if( dm != null && gc.isDisplayChangeSupported())
    		{
    			try
    			{
    				gc.setDisplayMode(dm);
    			}
    			catch(Exception ex)
    			{
    				
    			}
    		}
    	}
    	
    	public Window getFullScreenWindow()
    	{
    		return gc.getFullScreenWindow();
    	}
    	
    	// return screen to normal
    	public void restoreScreen()
    	{
    		Window w = gc.getFullScreenWindow();
    		// if the window contains something
    		if( w != null )
    		{
    			// free resources
    			w.dispose();
    		}
    		// takes anything away from full screen
    		gc.setFullScreenWindow(null);
    	}
    }
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

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

    Default Re: problems with outputting to screen

    I've set the background to PINK but whenever I run it its actually black.
    Read the API for JRootPane, which is the single child of a JFrame container (also see the API for the interface RootPaneContainer) and you'll easily see why.

    Don't try doing any fancy stuff directly in the content area of any top level window. Base your GUI on a JPanel (or in comparatively rare cases, on another subclass of JComponent) and either add(...) that to the top level window or set it as the top level window's content pane.

    Additionally, please post in an appropriate section. I'm moving this to AWT/Swing

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: problems with outputting to screen

    I cant make that kind of judgement call, because swing topics can also be a subset of new to java.

    this is bugging me
    Java Code:
    public void restoreScreen()
        {
            Window w = gc.getFullScreenWindow();
            // if the window contains something
            if( w != null )
            {
                // free resources
                w.dispose();
            }
            // takes anything away from full screen
            gc.setFullScreenWindow(null);
        }
    I dont understand this... it looks like I've just created a variable w and immediately destroyed it? why... surely w would be a copy of the screen output but not the actual one thats executing.

    wouldnt this be all I need?
    gc.setFullScreenWindow(null);
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

Similar Threads

  1. help with buttons and outputting to a file
    By tom2zip in forum New To Java
    Replies: 1
    Last Post: 11-09-2011, 04:21 AM
  2. outputting different letters in different colours
    By Sarah24 in forum Java Applets
    Replies: 4
    Last Post: 03-02-2011, 02:32 PM
  3. Outputting a * triangle
    By blackbeltsas in forum New To Java
    Replies: 1
    Last Post: 10-17-2010, 11:39 AM
  4. Full screen problems
    By doctorned in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 09:40 AM
  5. Screen Resolution Problems
    By RicKaufman in forum AWT / Swing
    Replies: 1
    Last Post: 10-04-2009, 06: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
  •