DO NOT LOOK AT THIS POST THIS ONE IS AN ACCIDENT. PLEASE LOOK AT MY OTHER ONE
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.
DO NOT LOOK AT THIS POST THIS ONE IS AN ACCIDENT. PLEASE LOOK AT MY OTHER ONE
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.
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.
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.
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.It is too fast
Do some experimenting.
If you don't understand my answer, don't ignore it, ask a question.
Please investigate the use of javax.swing.Timer to control the animation or activity in a Swing application.