Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Nothing will print into the console

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Nothing will print into the console

    Hi, i'm new to java, and i'm trying to make a gaming, but for some reason nothing will print into the console. I want it to have the system to print out the frames, and ticks, but nothing will happen.

    package ca.trey.game;
     
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Dimension;
     
    import javax.swing.JFrame;
     
    public class Game extends Canvas implements Runnable{
     
    	private static final long serialVersionUID = 1L;
    // Screen Size
    	public static final int WIDTH = 160;
    	public static final int HEIGHT = WIDTH/12*9;
    	public static final int SCALE=3;
    	//Game Name
    	public static final String NAME = "Exploration";
    	//JFrame (Hope It Works!)
     
    	private JFrame frame;
     
    	public boolean running = false;
     
    	public Game(){
    		setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    		setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
    		setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
     
    		frame = new JFrame(NAME);
     
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLayout(new BorderLayout());
     
    		frame.add(this, BorderLayout.CENTER);
    		frame.pack();
     
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
    		frame.setVisible(true);
     
    	}
    	private synchronized void start() {
    		new Thread(this).start();
    	}
    	private synchronized void stop() {
    		running = false;
    	}
     
    	public void run(){
    		long lastTime = System.nanoTime();
    		double nsPerTick =10000000D/60D;
     
    		int frames = 0;
    		int ticks = 0;
     
    		long lastTimer = System.currentTimeMillis();
    		double delta = 0;
     
    		while(running){
    			long now = System.nanoTime();
    			delta += (now - lastTime) / nsPerTick;
    			lastTime = now;
     
    			while(delta >= 1){
    			ticks++;
    			tick();
    			delta -= 1;
    			}
    	     frames++;
    		render();
     
    		if(System.currentTimeMillis() - lastTimer>= 1000){
    			lastTimer +=1000;
    			System.out.println(frames + ", " + ticks);
    			frames = 0;
    			ticks = 0;
     
    		}
    		}
     
    	}
     
    	public void tick(){
     
     
    	}
     
    		public void render(){
     
    	}
    	public static void main(String[] args){
    	new Game().start();
    }
     
     
     
     
     
    	}


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Nothing will print into the console

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

    A while ( true ) loop is a non-optimal game loop in a Java Swing application. Please investigate more advanced game loops by searching the web for "java game loop" or similar. There are a couple very good tutorials that show several options with explanations why some are better than others.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Nothing will print into the console

    Quote Originally Posted by GregBrannon View Post
    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

    A while ( true ) loop is a non-optimal game loop in a Java Swing application. Please investigate more advanced game loops by searching the web for "java game loop" or similar. There are a couple very good tutorials that show several options with explanations why some are better than others.
    Thanks for the welcome, but I was wondering what I did wrong with the code, that it wont print out the ticks, and frames?

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Nothing will print into the console

    Try changing running to 'true'.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Hurleyboarder (May 7th, 2014)

  6. #5
    Junior Member
    Join Date
    May 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Nothing will print into the console

    Quote Originally Posted by GregBrannon View Post
    Try changing running to 'true'.
    Thank you so much! You have really shown me how great this website is. I'll make sure to recommend my class.

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Nothing will print into the console

    Glad to help. Don't forget to look into better game loops.

Similar Threads

  1. [SOLVED] System.Console.Writeline vs Console.Writeline
    By Techstudent in forum Java Theory & Questions
    Replies: 1
    Last Post: September 12th, 2013, 05:57 PM
  2. Replies: 7
    Last Post: March 29th, 2013, 07:38 AM
  3. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  4. Replies: 0
    Last Post: October 29th, 2012, 12:17 AM
  5. xfa.host.print: print a page + some page range.
    By gammaman in forum Totally Off Topic
    Replies: 2
    Last Post: May 10th, 2012, 08:07 AM