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: My code needs help. The frames are not showing one a second and are getting to high. It's suposed to look like

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

    Default My code needs help. The frames are not showing one a second and are getting to high. It's suposed to look like

    DO NOT LOOK AT THIS POST THIS ONE IS AN ACCIDENT. PLEASE LOOK AT MY OTHER ONE


  2. #2
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My code needs help. The frames are not showing one a second and are getting to high. If you dont understand please tell me

     
    package com.brandon.game;
     
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferInt;
     
    import javax.swing.JFrame;
     
    public class Game extends Canvas implements Runnable  {
     
    	private static final long serialVersionUID = 1L;
     
    	public static final int WIDTH = 160;
    	public static final int HEIGHT = WIDTH/12*9;
    	public static final int SCALE = 3;
    	public static final String NAME = "Tronic";
     
    	private JFrame frame;
     
    	public boolean Running = false;
    	public int tickCount = 0;
    	private BufferedImage Image = new BufferedImage(WIDTH,HEIGHT, BufferedImage.TYPE_INT_RGB);
    	private int[] pixels = ((DataBufferInt) Image.getRaster().getDataBuffer()).getData();
     
    	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);
    	}
    	public synchronized void Start(){
    		new Thread(this).start();
    		Running = true;
    }
    	public synchronized void Stop(){
    		Running = false;
    	}
    	public void run(){ 
    		long lastTime = System.nanoTime();
    		double nsPerTick = 10000000D/60D;
     
    		int ticks = 0;
    		int frames = 0;
    		long lastTimer = System.currentTimeMillis();
    		double delta = 0;
     
    		while(Running){
    			long now = System.nanoTime();
    			delta +=(now-lastTime) /nsPerTick;
    			lastTime = now;
    			boolean shouldRender = true;
     
    			while(delta>=1){
    				ticks++;
    				delta-=1;
    			    tick();
    			    shouldRender = true;
    			}
    			try {
    				Thread.sleep(2);
    			} catch (InterruptedException e) {
     
    				e.printStackTrace();
    			}
    			if(shouldRender){
    			frames++;
    			render();
    			}
    			if(System.currentTimeMillis() - lastTimer >= 1000){
    			System.out.println(frames +"."+ ticks);
    			lastTimer += 1000;
    			frames = 0;
    			ticks = 0;
     
    			}
    		}
     
    }
    	public void tick(){
    		tickCount++;
     
     
    		for(int i=0; i < pixels.length; i++){
    			pixels[i] = i + tickCount;
    		}
    	} 
     
    	public void render(){
    		BufferStrategy bs = getBufferStrategy();
    		if (bs == null){
    			createBufferStrategy(3);
    			return;
     
    		}
    		Graphics g = bs.getDrawGraphics();
     
    		g.setColor(Color.BLACK);
    		g.fillRect(0,0,getWidth(),getHeight());
     
    		g.drawImage(Image,0,0,getWidth(),getHeight(),null);
     
    		g.dispose();
    		bs.show();
     	}
     
    public static void main(String[] args){
    	new Game().Start();	
     
      }
     }
    Last edited by Brundi; April 7th, 2014 at 02:01 PM.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My code needs help. The frames are not showing one a second and are getting to high. It's suposed to look like

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Please explain what the code does when it is executed and what you want it to do differently.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My code needs help. The frames are not showing one a second and are getting to high. It's suposed to look like

    It is too fast and I want it every second to display 60 . 60 or any other number close to that

    but the numbers in the bottom are adding up very fast and the blue and black are supposed to move a lot slower across the screen

    By the way, this is from the tutorial: https://www.youtube.com/watch?v=VE7ezYCTPe4

    Thanks so much I have been stuck on this for a while
    Last edited by Brundi; April 7th, 2014 at 02:19 PM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My code needs help. The frames are not showing one a second and are getting to high. It's suposed to look like

    It is too fast
    Where in the code is time considered and used to change the display? Try changing the values it is using to slow down the movement.
    Do some experimenting.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: My code needs help. The frames are not showing one a second and are getting to high. It's suposed to look like

    Please investigate the use of javax.swing.Timer to control the animation or activity in a Swing application.

Similar Threads

  1. [SOLVED] My code was showing picture but now it's not!!!!!
    By GoodbyeWorld in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 29th, 2013, 10:58 PM
  2. linking frames
    By tegalinks in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 25th, 2013, 07:48 AM
  3. Replies: 3
    Last Post: March 6th, 2012, 03:50 AM
  4. Replies: 0
    Last Post: August 30th, 2011, 08:23 AM
  5. GIF frames
    By vsector in forum AWT / Java Swing
    Replies: 0
    Last Post: April 15th, 2010, 05:25 PM