flicker problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: flicker problem

  1. #1
    Join Date
    May 2004
    Posts
    1

    flicker problem

    hi, im new to java, trying to learn!!

    ive been given an assignment which involves creating a java game so i decided to go for the classic duck shoot game.

    Ive created the background lake and the little ducks that swim accross the lake.

    my problem is the little ducks flicker as they travel accross the page

    ill post my code below if anyone has any ideas?

    i tried to use the buffer to stop this and it doesnt work, im probly doing somthing stupid.

    any ideas?
    CHEERS
    ASH

    //////////////////////////////////////
    // //
    // Ashley Kay's Java Applet //
    // Version 1 //
    // //
    // Duck Shoot Game //
    // //
    //////////////////////////////////////

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

    public class duckShoot extends Applet implements ActionListener
    {
    Image image, duckpic;

    private Graphics gBuffer;
    private Image Buffer;

    DUCK1 duck1=null;
    T2 t2=null;

    public void init()
    {
    //create graphics buffer, the size of the applet
    Buffer=createImage(600,400);
    gBuffer=Buffer.getGraphics();
    }





    public void start()
    {
    duckpic=getImage(getDocumentBase(), "duckfamily.gif");
    image=getImage(getDocumentBase(), "lake.jpg");
    duck1=new DUCK1(this);
    duck1.start();
    resize(640,480);
    repaint();

    t2=new T2(this);
    t2.start();
    }

    public void stop()
    {
    if(duck1!=null)
    duck1.stop();
    if(t2!=null)
    t2.stop();
    }

    class DUCK1 extends Thread
    {
    duckShoot app;
    int x;

    //pass reference to Applet
    DUCK1(duckShoot parent)
    {
    app=parent;
    }

    public void run()
    {
    //method loops forever if thread exsists, other processes can run aswell
    while(true) {
    if(x++>600)
    x=0;
    app.repaint();
    try{Thread.sleep(10);}catch(InterruptedException e){}
    }
    }
    }

    class T2 extends Thread
    {
    duckShoot app;
    int x;

    //pass reference to Applet
    T2(duckShoot parent)
    {
    app=parent;
    }

    public void run()
    {
    //method loops forever if thread exsists, other processes can run aswell
    while(true) {
    if(x++>600)
    x=0;
    app.repaint();
    try{Thread.sleep(1);}catch(InterruptedException e){}
    }
    }
    }

    public void actionPerformed(ActionEvent e)
    {


    }

    public void update(Graphics g)
    {
    paint(g);
    }

    public void paint(Graphics g)
    {
    g.drawImage(image, 0,0,this);
    g.drawString("Score : ", 5, 20);
    g.drawImage(duckpic,duck1.x, 300,this);
    g.drawImage(duckpic,t2.x, 200, this);
    }
    }

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    Class Names Like Duck and Duck1 Should Always Start With A Capital Letter

    variable names like Buffer (yours) should always start with a lowercase letter (e.g. buffer) and should be more descriptive of what they actually do (e.g. duckImageBuffer)

    ALL CAPITAL LETTERS IS A RESERVED CONVENTION FOR FINAL VARIABLES. DO NOT NAME CLASSES WITH ALL CAPS UNLESS IT IS AN ACRONYM E.G. "public class URL{"
    only every make a variable name all caps if if is declared as: " ... final int MY_INT"

    -
    this will make your code more readable and understandable to other programmers.. it is also what Sun say you should do

    --

    I dont think your buffering technique is correct.. you should merely setDoubleBuffered(true) on whatever component you are using to display the ducks
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Feb 2004
    Posts
    808
    by the way.. sleeping for 1 millisecond isnt going to do much, as your applet will be generating arounf a thousand repaints a second, but java wont bother updating the display as often as that,,

    youve sort-of got the right idea about creating a video game, but at the same time, the method youre using is unsuited to java because of the way graphics are handled.. you'd be better off using a java.util.Timer to stimulate an array of Duck sprites at a regular interval. at every interval, the sprites should move a little, and when you fire, the arrow should be made aware of the Duck array so it can query each one to see if it hit one of them (when it gets there)..

    writing a game is a big thing to do; be careful not to bite off more than you can chew
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center