Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (683)
Games in Android Showcase (196)
games submitted by our members
Games in WIP (750)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  moving images in a game loop  (Read 1190 times)
0 Members and 1 Guest are viewing this topic.
Offline ping

Innocent Bystander

Java games rock!

« Posted 2004-04-09 07:38:53 »

I'm trying to setup a basic gameloop.
it runs but animation isn't smooth ( lots of flicker ).
as a personalJava application it must be compatible  with jdk 1.1
what's wrong with this code?
can you help me? thanks.

import java.awt.*;
import java.awt.event.*;

public class Engine extends Frame implements ActionListener {
 public LevelOne levOne;
 public static void main (String[] args) {
   Engine engine = new Engine();
 public Engine() {
   levOne = new LevelOne();
   levOne.setSize(300, 300);
   add("Center", levOne);
   addWindowListener(new winExit());
   setLayout ( new FlowLayout());
   setSize( 300, 300);
 public void actionPerformed(ActionEvent e)
   dispatchEvent( new WindowEvent( this, WindowEvent.WINDOW_CLOSING));
class winExit extends WindowAdapter {
 public void windowClosing(WindowEvent e) {
class LevelOne extends Canvas implements MouseMotionListener, Runnable {
 Thread t;
 Image offscreenImage;
 Graphics offscreenGraphics;
 Color black = new Color(0, 0, 0);
 Image background1, sprite1;
 Toolkit toolkit = Toolkit.getDefaultToolkit();
 int posX = 50, posY = 50;
   background1 = toolkit.getImage("bacground.jpg");
   sprite1 = toolkit.getImage("sprite.gif");
   t = new Thread(this, "LevelOne");
public void run(){
   for ( ;; ) {
     try {
       if (posY > 300) posY = 0;
     } catch (InterruptedException e) {}
 public void paint(Graphics g) {
   if(offscreenImage==null) {
// set up a simple double buffer
     offscreenImage = createImage(this.size().width, this.size().height);
     offscreenGraphics = offscreenImage.getGraphics();
   offscreenGraphics.drawImage(background1, 0, 0, this);
   offscreenGraphics.drawImage(sprite1, posX, posY, this);
   g.drawImage(offscreenImage, 0, 0, this);
 public void mouseMoved(MouseEvent e){}
 public void mouseDragged(MouseEvent e){}
Online tom
« Reply #1 - Posted 2004-04-09 11:45:35 »

Instad of calling repaint, get the Graphics object with getGraphics() and use it directly. You must also override "public void update(Graphics g)". It will clear the background with the background color wich causes flickering.

Offline Jeff

JGO Coder

Got any cats?

« Reply #2 - Posted 2004-04-09 17:58:35 »

Absoultely on the second.

On the first you need to be very careful. AWT and Swing are NOT thread safe and expect to render from the AWT event thread not from your main thread.

If you want to try to render from your main loop you really should be using the "Full Screen Exclusive" mode API that was added in JDK1.4

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

orrenravid (175 views)
2016-07-16 03:57:23

theagentd (246 views)
2016-07-11 14:28:54

Hydroque (329 views)
2016-07-06 05:56:57

Hydroque (479 views)
2016-07-03 08:52:54

GrandCastle (363 views)
2016-07-01 09:13:47

GrandCastle (355 views)
2016-07-01 09:09:45

CopyableCougar4 (410 views)
2016-06-25 16:56:52

Hydroque (392 views)
2016-06-22 02:17:53

SwampChicken (354 views)
2016-06-20 13:22:57

SwampChicken (294 views)
2016-06-20 13:22:49
Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42

FPS Camera Tutorial
by Hydroque
2016-05-22 05:40:58

Website offering 3D Models specifically for games for free
by vusman
2016-05-18 17:23:09

Website offering 3D Models specifically for games for free
by vusman
2016-05-09 08:50:56

Website offering 3D Models specifically for games for free
by vusman
2016-05-06 11:10:21

Website offering 3D Models specifically for games for free
by vusman
2016-04-29 12:56:17 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!