Hey everybody, 
I started making the beginning of a car game and setup the double buffer, game loop, and the code for the movement of all the sprites (based off of the spaceinvaders java tutorial) and ran it.
I looked alright (No vertical flickering) but it “stutters” or “skips” slightly unless I MOVE THE MOUSE (cursor) over the program?!
-It only runs SMOOTHLY if I move the mouse within the program (JFrame) and even if I resize it and move the mouse it will run smoothly.
Note: The cursor has to be in motion for the “skip” to stop, once I stop moving the cursor the skipping begins again.
Any Idea what this could be?
Thanks for the help!
Here’s the code without the Car Class, I can include that also if it helps…
package road;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Road extends Canvas {
//Create 100 Car Spots
public ArrayList<Car> carlist = new ArrayList<Car>();
public BufferStrategy bs;
//Constructor: Initialize Everything here?
//I think I should only initialze Road Stuff
public Road(){
JFrame frame = new JFrame("Road App");
JPanel panel = (JPanel) frame.getContentPane();
panel.setPreferredSize(new Dimension(1000,300));
panel.setLayout(null);
// setup our canvas size and put it into the content of the frame
setBounds(0,0,1000,300);
panel.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set the size of the canvas and add it to the frame via content pane
//this.setPreferredSize(new Dimension(1000,300));
//frame.getContentPane().add(this);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
setIgnoreRepaint(true);
panel.setIgnoreRepaint(true);
//Create Buffer Strategy for painting actively
createBufferStrategy(2);
bs = getBufferStrategy();
}
//Entry into Program
public static void main(String[] args) {
// TODO code application logic here
Road road = new Road();
road.init();
road.programLoop(); // get out of main...
}
//
//THIS IS THE MAIN LOOP
//
public void programLoop(){
boolean isRunning = true;
long lastIterationTime = System.currentTimeMillis();// Initialize it
long lastTimedIteration = System.currentTimeMillis();
long startTime = System.currentTimeMillis();
//
while(isRunning){
long change = System.currentTimeMillis() -lastTimedIteration;
long delta = System.currentTimeMillis() -lastIterationTime;
long runTime = System.currentTimeMillis() - startTime;
System.out.println("delta is " + delta);
lastIterationTime = System.currentTimeMillis();
if (change>30){
//Update the last time we went through this loop
lastTimedIteration = System.currentTimeMillis();
//Print out delta value and current time
System.out.println(change + " " + lastTimedIteration);
System.out.println("\n::Class Car Stats::" + Car.idcount + " " + Car.totalcars);
//////////////////////////////////////////
//CarPlacer: Places New Cars on map at specified times
//////////////////////////////////////////
for (int i = 0; i < carlist.size(); i++){
//Is the current car active, do logic if so...
if (carlist.get(i).getActive() == false){
if (carlist.get(i).getStartTime() < runTime){
carlist.get(i).setActive(true);
}
}
}
///////////////////////////////////////////
//DO LOGIC FOR ALL ACTIVE CARS
///////////////////////////////////////////
//Go through all cars in the list
for (int i = 0; i < carlist.size(); i++){
//Is the current car active, do logic if so...
if (carlist.get(i).getActive()){
carlist.get(i).doLogic();
}
}
}//End of timed Section
//Beginning of code executed Every loop.
for (int i = 0; i < carlist.size(); i++){
//Is the current car active, do logic if so...
if (carlist.get(i).getActive()){
carlist.get(i).moveCar(delta);
}
}
////////////////////////////////////
//Do all Drawing
/////////////////////////////////////
doDrawing();
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
Logger.getLogger(Road.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void init(){
//Create a list of Cars to put into program
Car c1 = new Car(75,0,0,150,0);
Car c2 = new Car(75,0,1,75,500);
Car c3 = new Car(75,0,0,100,100);
//Create another car for kicks
carlist.add(c1);
carlist.add(c2);
carlist.add(c3);
}
public void doDrawing(){
Graphics2D g = (Graphics2D) bs.getDrawGraphics();
//Draw Background
g.setColor(Color.BLACK);
g.fillRect(0,0,this.getWidth(),this.getHeight());
//Draw the Road
g.setColor(Color.GRAY);
g.drawLine(0, 160, 1000, 160);
g.setColor(Color.WHITE);
g.drawLine(0, 135, 1000, 135);
g.drawLine(0, 185, 1000, 185);
//Draw all the cars that are ACTIVE
for (int i = 0; i < carlist.size(); i++){
//Is the current car active, Draw it on Screen then.
if (carlist.get(i).getActive()){
carlist.get(i).draw(g);
}
}
//Throw away our container and flip buffer.
g.dispose();
bs.show();
}
}