Results 1 to 7 of 7
  1. #1
    KCalderon is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Output issue w/ frame and FPS

    I'm writing a basic little plat-former just to keep developing my skills, however, I seem to have an issue on my FPS counter not retriveing my output, and while my frame does accept my callback to my final WIDTH and HEIGHT. It's not drawing on the frame. Here's what I have so far:

    Java Code:
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    
    import javax.swing.JFrame;
    
    
     public class TowerGame extends Canvas implements Runnable {
    
        /**
         */
      public static final long serialVersionUID = 1L;
    
        /**
         *
         */
        public static final int WIDTH = 640,
    
        /**
         *
         */
        HEIGHT = 480;
    
        /**
         *
         */
        public boolean running = false;
    
        /**
         *
         */
        public TowerGame(){
    				
    			}
    			//Starts out game
    			private void start() {
    				running = true;
    				new Thread(this).start();
    				
    			}
    			//Stops our game
    
        /**
         *
         */
        			public void stop() {
    				running = false;
    				
    			}
    			
    			@Override
    			public void run() {
    				int fps = 0;
    				int update = 0;
    				long fpsTimer = System.currentTimeMillis();
    				double nsPerUpdate = 1000000000.0/ 60;
    				// Last update in nanoseconds
    				double unprocessed = 0;
    				double then = System.nanoTime();
    				while(running){
    					double now = System.nanoTime();
    					unprocessed += (now + then) / nsPerUpdate;
    					then = now;
    					while(unprocessed >= 1) {
    						//Update
    						update ++;
    						update ();
    						unprocessed --;
    					}
                                            
                                            Thread td = new Thread();
    					try{
    					Thread.sleep(1);
    					}catch(InterruptedException e){
    						Thread.yield();
    						e.printStackTrace();
    					}
    					//Render
    					fps ++;
    					render();
    					//FPS timer
    					if(System.currentTimeMillis() - fpsTimer > 1000) {
    						System.out.printf("%d fps, %d updates%n",fps,update);
    						fps = 0;
    						update = 0;
    						fpsTimer += 1000;
    						
    					}
    				}
    			}
    			//Draws everything
    			private void render() {
    				BufferStrategy strategy = this.getBufferStrategy();
    				if(strategy == null){
    					//Create strategy
    				createBufferStrategy(2);
    				requestFocus();
    				return;
    			}
    				Graphics g = strategy.getDrawGraphics();
    				
    				
    				
    				g.dispose();
    				strategy.show();
    		}
    			// Updates game logic
    			private void update() {
    				
    			}
    
        /**
         *
         * @param args
         */
        public static void main(String[] args){
    				
                                TowerGame game = new TowerGame();
    				game.setPreferredSize(new Dimension (WIDTH, HEIGHT));
    				game.setMaximumSize(new Dimension (WIDTH, HEIGHT));
    				game.setMinimumSize(new Dimension (WIDTH, HEIGHT));
    				game.setSize(new Dimension (WIDTH, HEIGHT));
    				
    				
    				
    				
    				JFrame frame = new JFrame("Tower Game");
    				frame.setResizable(false);
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.add(game);
    				frame.pack();
    				frame.setLocationRelativeTo(null);
    				frame.setVisible(true);
    			
    				game.start();
    			}
    				
    	}
    I'm not seeing what the issue could possibly be. Any and all advise would be greatly appreciated. Thank you.
    Last edited by KCalderon; 11-23-2013 at 11:11 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Output issue w/ frame and FPS

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Output issue w/ frame and FPS

    Well, I have done quite a bit of graphics but I never use Canvas (I only use Swing components). In any event, I don't see what you are supposed to be draing.
    Normally, one overrides a paint method to render graphics.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    KCalderon is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: Output issue w/ frame and FPS

    Wait, isn't the purpose of the Canvas that it works more efficiently as a double buffer than a Swing components?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Output issue w/ frame and FPS

    Unlike AWT, Swing painting implements double buffering for you (behind the scenes). In JComponent, which most Swing components inherit from, there is a method setDoubleBuffering(boolean val). This defaults to true. Here is some good info on painting in Swing. Painting in AWT and Swing. Unfortunately, the example links were never updated when Oracle bought Sun. But the info is good.

    However, with regard to your program, I don't see where you are drawing anything.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Output issue w/ frame and FPS

    To see something drawn, add this line after g is assigned a value:
    Java Code:
    	g.drawLine(1,1,200,255); //<<<<<<<<<  this shows
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    KCalderon is offline Member
    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Default Re: Output issue w/ frame and FPS

    Sweet, thanks. But yeah that was my bad, I was screwing around with some ideas on how to get it to work and I must've not put everything back before I posted it.

Similar Threads

  1. Output issue
    By Jason in forum New To Java
    Replies: 5
    Last Post: 09-27-2011, 05:28 PM
  2. Saving output displayed in a frame in image format
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 05-25-2011, 02:28 PM
  3. Replies: 3
    Last Post: 08-07-2010, 05:28 AM
  4. Regarding issue in frame window
    By santhosh_el in forum AWT / Swing
    Replies: 1
    Last Post: 06-01-2009, 08:10 AM
  5. Sending output to a Frame
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-04-2008, 09:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •