How can i better this game-loop without using thread.sleep(); ? [CHANGED TOPIC]

I just changed the topic and I got replies that the thread.sleep() method is bad practice how can I better this game loop for smoother running on multiple computers?

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;

import javax.swing.JPanel;




public class Panel extends JPanel 
	implements Runnable, KeyListener{
	
	// dimensions
	public static final int WIDTH = 320;
	public static final int HEIGHT = 240;
	public static final int SCALE = 2;
	
	// game thread
	private Thread thread;
	private boolean running;
	
	// image
	private BufferedImage image;
	private Graphics2D g;
	
	public Panel() {
		super();
		setPreferredSize(
			new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
		setFocusable(true);
		requestFocus();
	}
	
	public void addNotify() {
		super.addNotify();
		if(thread == null) {
			thread = new Thread(this);
			addKeyListener(this);
			thread.start();
		}
	}
	
	private void init() {
		
		image = new BufferedImage(
					WIDTH, HEIGHT,
					BufferedImage.TYPE_INT_RGB
				);
		g = (Graphics2D) image.getGraphics();
		
		running = true;
	
	}
	
	public void run() {
		
		init();
		
		// game loop
		while(running) {
			
			update();
			draw();
			drawToScreen();
			
			
			try {
				Thread.sleep(10);
			}
			catch(Exception e) {
				e.printStackTrace();
			}
			
		}
		
	}
	
	private void update() {
	}
	private void draw() {
	}
	private void drawToScreen() {
		Graphics g2 = getGraphics();
		g2.drawImage(image, 0, 0,
				WIDTH * SCALE, HEIGHT * SCALE,null);
		
		g.drawString("2014 Jay H.", 10, 230);
		g2.dispose();
	}
	
	public void keyTyped(KeyEvent key) {}
	
	// PUBLIC KEYRELEASES
	public void keyPressed(KeyEvent key) {
		
	}
	// PUBLIC KEYRELEASES
	public void keyReleased(KeyEvent key) {

	}

}

hmm, looking at the code you should be able to double buffer it. take a look at using createbufferstrategy();


BufferStrategy bs = getBufferStrategy();
		if (bs == null) {
			createBufferStrategy(2);
			return;
		}

this is an example from the code in the engine for my ludum dare game, no tearing was present so you shouldnt need more than double buffering.

The main problem you are having is your Game-Loop. It is very inconsistent because [icode]Thread.sleep()[/icode] is not guaranteed across platform. Here are two tutorials that helped me very much learn game-loops.

Game-Loops!!! - By Eli Delventhal
deWiTTERS Game Loop - By Keon Witters

These are the two articles that helped me jump into programming actual games.

Your BufferedImage is of a RGB type because quite simply, that’s what you want to render. There will be no alpha channel, so no transparency basically.

In simplest terms,

  private void drawToScreen() {
      Graphics g2 = getGraphics();
      g2.drawImage(image, 0, 0,
            WIDTH * SCALE, HEIGHT * SCALE,null);
      
      g.drawString("2014 Jay H.", 10, 230);
      g2.dispose();
   }

You create a Graphics object called “g2” that will basically give you a conduit for drawing to the screen and receiving information about the screen. You use this graphic object to draw an image proportionate to the size of the screen, with no observer.

You then render text, and dispose of the Graphics object since you are done with it.

BufferedImage.TYPE_INT_RGB creates a buffer of image data that will appear on the screen. The Graphics object does nothing by itself, it needs a BufferedImage to actually write image data to.

Also, as an above poster said, Thread.sleep() is really bad practice. I wonder who came up with using that.