Application Problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Application Problem

  1. #1
    Join Date
    Feb 2005
    Posts
    10

    Application Problem

    I've been having a problem the past week or two that I can't get over, or find help. It really should be simple though...

    I'm using JCreator to create an Empty Project, because the app template is not good to use. Also I am using JDK 1.4.2 I think.

    I have a program that is supposed to run a full screen app and paint hello world. Here is the code from the two files:

    FullScreenTest.Java


    Code:
    import java.awt.*;
    import javax.swing.JFrame;
    
    public class FullScreenTest extends JFrame
    {
    	public static void main(String[] args)
    	{
    		DisplayMode displayMode;
    		
    		if(args.length == 3)
    		{
    			displayMode = new DisplayMode(Integer.parseInt(args[0]),
    			Integer.parseInt(args[1]),
    			Integer.parseInt(args[2]),
    			DisplayMode.REFRESH_RATE_UNKNOWN);
    		}
    		else
    		{
    			displayMode = new DisplayMode(800,600,16,DisplayMode.REFRESH_RATE_UNKNOWN);
    		}
    		FullScreenTest test = new FullScreenTest();
    		test.run(displayMode);
    	}
    	
    	private static final long DEMO_TIME = 5000;
    	
    	public void run(DisplayMode displayMode)
    	{
    		setBackground(Color.blue);
    		setForeground(Color.white);
    		setFont(new Font("Dialog", Font.PLAIN,24));
    		
    		SimpleScreenManager screen = new SimpleScreenManager();
    		try
    		{
    			Thread.sleep(DEMO_TIME);
    		}
    		catch (InterruptedException ex)
    		{
    		}
    		finally
    		{
    			screen.restoreScreen();
    		}
    	}
    
    
    	public void paint (Graphics g)
    	{
    		g.drawString("Hello World!",20,50);
    	}
    }

    SimpleScreenManager.Java

    Code:
    import java.awt.*;
    import javax.swing.JFrame;
    
    public class SimpleScreenManager
    {
    	private GraphicsDevice device;
    	
    	public SimpleScreenManager()
    	{
    		GraphicsEnvironment enviornment = 
    			GraphicsEnvironment.getLocalGraphicsEnvironment();
    		device = enviornment.getDefaultScreenDevice();
    	}
    	
    	public void setFullScreen(DisplayMode displayMode,
    	JFrame window)
    	{
    		window.setUndecorated(true);
    		window.setResizable(false);
    		
    		device.setFullScreenWindow(window);
    		if (displayMode!= null && device.isDisplayChangeSupported())
    		{
    			try
    			{
    				device.setDisplayMode(displayMode);
    			}
    			catch(IllegalArgumentException ex)
    			{
    			}
    		}
    	}
    
    	public Window getFullScreenWindow()
    	{
    		return device.getFullScreenWindow();
    	}
    
    	public void restoreScreen()
    	{
    		Window window = device.getFullScreenWindow();
    		if (window != null)
    		{
    			window.dispose();
    		}
    		device.setFullScreenWindow(null);
    	}
    }


    When I compile and execute only a dos window comes up and nothing else happens. Please help!

    P.S. I can write applets in java with all sorts of graphical interface with no problem. I just can't seem to write any programs for an app....!!
    Last edited by anothrguitarist; 05-19-2005 at 07:19 PM.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    That was a very complicated "hello world" app...

    ..with a lot of strange code.

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    /**
     * hello world panel
     */
    class HelloPanel extends JPanel {
      Font bigFont=new Font("monospace",Font.BOLD, 22);
      public void paint (Graphics g) {
        Color c=g.getColor();
        g.setColor(Color.BLUE);
        g.fillRect(0,0,getWidth(),getHeight());
        g.setColor(Color.white);
        Font f=g.getFont();
        g.setColor(Color.WHITE);
        g.setFont(bigFont);
        g.drawString("Hello World !",50,50);
        g.setFont(f);
        g.setColor(c);
      }
    }
    /**
     *
     * Hellow World - JFrame
     */
    public class HelloWorld extends JFrame {
    
      public HelloWorld() {
      }
      /**
       * Basic application launch:  create a JFrame, set its bounds,
       * add component(s) to it and make it visible.  Use an inner
       * windowadapter class to ensure proper application termination,
       */
      public static void main(String[] args) {
        HelloWorld hw = new HelloWorld();
    
        Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
        hw.setBounds(0,0,d.width,d.height);
        hw.getContentPane().add(new HelloPanel());
        hw.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }
        });
        hw.setVisible(true);
      }
    }
    eschew obfuscation

  3. #3
    Join Date
    Feb 2005
    Posts
    10
    Thanks that helps and will be good for what I need to do. I think all the strange code was to set the refresh rate, resolution, and bit depth. I got it from a book and the code s eemeda bit foriegn to me but what you gave me helps.

    Thanks again.

  4. #4
    Join Date
    Feb 2005
    Posts
    10
    I'm having trouble with this again...

    What I've done to the program is add a thread and code for a buffer. I think I'm having trouble because I don't know what code should be replaced from using applets.

    Here's my new code:

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    /**
     * The canvas to draw on.
     */
    class ScreenCanvas extends JPanel 
    {
        Thread runner;
        private Image Buffer;
        private Graphics gBuffer;
      
        public void init()
        {
                //create graphics buffer, the size of the applet
                Buffer=createImage(size().width,size().height);
                gBuffer=Buffer.getGraphics();
        }
                public void start()
            {
                    if (runner == null)
                    {
                        runner = new Thread ();
                        runner.start();
                    }
            }
    
            public void stop()
            {
                   if (runner != null)
                   {
                        runner.stop();
                        runner = null;
                   }
            }
    
            public void run()
            {
                    while(true)
                    {
                            //Thread sleeps for 15 milliseconds here
                            try {runner.sleep(15);}
                            catch (Exception e) { }
                            repaint();
                    }
            }
    
            //is needed to avoid erasing the background by Java
            public void update(Graphics g)
            {
                    paint(g);
            }
    
            public void paint(Graphics g)
            {
            	g.setColor(Color.black);
            	g.drawLine(1,1,23,23);
            	//g.drawImage (Buffer,0,0, this);
            }
    }
    /**
     *
     * Side Scroller - JFrame
     */
    public class SideScroller extends JFrame {
    
      public SideScroller() {
      }
      /**
       * Basic application launch:  create a JFrame, set its bounds,
       * add component(s) to it and make it visible.  Use an inner
       * windowadapter class to ensure proper application termination,
       */
      public static void main(String[] args) {
        SideScroller sideScroller = new SideScroller();
    
        Dimension dim=Toolkit.getDefaultToolkit().getScreenSize();
        sideScroller.setBounds(0,0,dim.width,dim.height);
        sideScroller.getContentPane().add(new ScreenCanvas());
        sideScroller.addWindowListener(new WindowAdapter() 
        {
          public void windowClosing(WindowEvent e) 
          {
            System.exit(0);
          }
        });
        sideScroller.setVisible(true);
      }
    }
    The problem was with the line I commented out...


    line 58
    //g.drawImage (Buffer,0,0, this);
    There is a run time error when this happens because the pointer this is not supposed to be used. Do you know what the parameters should be. I tried using sideScroller, but an error came up.

    also when I do a thread in an applet, I use

    runner = new Thread (this); to declare a new thread, but it had an error, so I took out 'this' and it worked fine.


    Any help would be appreciated.

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Runtime error ??

    There is nothing wrong with this invokation:

    g.drawImage (Buffer,0,0, this);

    .. except for the fact that Buffer is an abstract java class defined in
    the java.nio package as a linear, finite sequence of elements of a specific primitive type.

    My advice, use uncommon names in lower case; how about memImage
    and memBuffer ?

    The frequency you set to 15Ms, that is 66,67 sceen refreshes per second,
    rather fast consdering that movies do smoothly with 24 images per sec.

    You invoke paint from update, when I do that I get a stack overflow
    immediately. When I do animations I use a steup where I draw it all in the
    update method and the paint method just looks like:
    Code:
    public void paint(Graphics g) {
      update(g);
    }
    In order to use an object in a thred constructor like:
    Thread t=new Thread(this);

    Then the this (the panel in this case), has to implement the Runnable
    interface, - it has to implement the method

    public void run()

    and declare itself as a Runnable in the class header (you forgot that) :

    class ScreenCanvas extends JPanel implements Runnable {
    .
    .
    }
    Without the pointer (this) in the constructor your little run method is
    never going to be called...

    You should not use the stop method for terminating threads, its unreliable.
    This is one of the recommended ways of starting/stopping threads.

    Code:
    boolean running=false; // class global variable
    
    public synchronized void setRunning(boolean running) {
      this.running=running;
    }
    public void start() {
      if (runner != null && runner.isAlive()) {
        runner.interrupt();
      }
      runner = new Thread (this);
      setRunning(true);
      runner.start();
    }
    
    public void stop()  {
      setRunning(false);
    }
    
    public void run()  {
      while(running) {
           //Thread sleeps for 15 milliseconds here
           try {runner.sleep(15);
           } catch (InterruptedException e) { 
             return; // my advice...
           }
           repaint();
        }
    }
    The init/start/stop methods are applet specific methods that are autoinvoked
    by the browser. When converting an applet to an application the init &
    start methods chould be considered part of the applications constructor
    processing. The stop method should be incorporated in the application's
    termination code (application frame window closing event)

    A final advice, when using loaded/created images and graphics in an application
    you should always use a MediaTracker for each image to ensure proper
    loading prior to displaying. If you're only using a timy gif or two it may not
    be a big deal, but for a big app with, say 50-100 images or more, you must have
    some means of determining when the movie is ready to roll.
    Last edited by sjalle; 05-22-2005 at 08:23 PM.
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center