I have been working on Java game development for a few years.Obviously questions are going to be raised during the development period.I have come to notice that simple programming questions do not get answered by simple programming answers which are emphasised in lowering the morale of the questionaire and it is becoming a concern.

Here is a question I asked in a Java forum only a few days ago of which I got nothing but insults back.

Q.In my Java awt Applet game (no swing)I have runnable images which when clicked move with the mouse and then are placed upon a second mouse click.Works great but leaves a trail when moving the images.Would anyone know how to solve this trail problem?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package res3;


import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Label;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.TextField;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



/**
 *
 * @author po
 */
public class resthree extends Applet implements Runnable, MouseListener, MouseMotionListener,ActionListener {

    Label counter1;
    Label counter2;

    int bgx = 0;
    int bgy = 0;

    //////////////////////////////////////////////
    //ghosting without change
    //int x1 = 300;  
    //int y1 = 0;

    int b1xo = 300;////////brick1x old
    int b1yo = 0;

    int b1xn;
    int b1yn;
    //////////////////////////////////////////////////////
    int x2 = 450;
    int y2 = 0;

    Thread thread1;
    boolean thread1bool = true;

    Thread thread2;
    boolean thread2bool = true;

    Image bg1Image;
    boolean bg1Imagebool = true;
    boolean repaintbgbool = false;

    Image[] res1Images = new Image[4];
    int res1int = 0;
    int res1count = 0;
    boolean res1Imagesbool = true;

    Image[] res2Images = new Image[4];
    int res2int = 0;
    int res2count = 0;

    private Image Buffer;
    private Graphics gBuffer;

    MediaTracker tr;

    Label mcoordsx;
    Label mcoordsy;

    Integer mx;
    Integer my;

    String mouseover = "No Object";
    boolean  brick1bool;
    String brickbool = "brick 1 boolean = true";

    TextField textfield1;
    Button button1;
    ///////////////////////////////////////////////////////
    public void init() {

    setLayout(null);
    setSize(960,661);
    setForeground(Color.BLACK);

    //test no flicker black
    //setBackground(Color.BLACK);
    //tests confirm animation with mt and db runs smooth as silk with no bg image/s

    tr = new MediaTracker(this);

    // Buffer=createImage(size().width,size().height);
    Buffer = createImage (this.getSize ().width, this.getSize ().height);

    gBuffer=Buffer.getGraphics();

    bg1Image = getImage(getDocumentBase(), "tile1.gif");
    //animation images  
    res1Images[0] = getImage(getDocumentBase(), "reso1.gif");
    res1Images[1] = getImage(getDocumentBase(), "reso2.gif");
    res1Images[2] = getImage(getDocumentBase(), "reso3.gif");
    res1Images[3] = getImage(getDocumentBase(), "reso4.gif");


    res2Images[0] = getImage(getDocumentBase(), "reso1.gif"); 
    res2Images[1] = getImage(getDocumentBase(), "reso2.gif"); 
    res2Images[2] = getImage(getDocumentBase(), "reso3.gif"); 
    res2Images[3] = getImage(getDocumentBase(), "reso4.gif");

    //background image
    //add images to mediatracker
    tr.addImage(bg1Image,0); //id = 0,media tracker

    tr.addImage(res1Images[0], 1);// res1Images id =,1 mediatracker
    tr.addImage(res1Images[1], 1);
    tr.addImage(res1Images[2], 1);
    tr.addImage(res1Images[3], 1);

    tr.addImage(res2Images[0], 2);// res1Images id =,1 mediatracker
    tr.addImage(res2Images[1], 2);
    tr.addImage(res2Images[2], 2);
    tr.addImage(res2Images[3], 2);
    ///////////////////////////////////////////////////////

    counter1 = new Label("res1");
    counter1.setBounds(800,30,75,20);
    counter1.setForeground(Color.blue);
    counter1.setBackground(Color.LIGHT_GRAY);
    add(counter1);

    counter2 = new Label("res2");
    counter2.setBounds(800,70,75,20);
    counter2.setForeground(Color.blue);
    counter2.setBackground(Color.LIGHT_GRAY);
    add(counter2);

    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    mcoordsx = new Label("");
    mcoordsx.setBounds(700,510,35,20);
    mcoordsx.setForeground(Color.blue);
    mcoordsx.setBackground(Color.black);
    add(mcoordsx);

    mcoordsy = new Label("");
    mcoordsy.setBounds(735,510,35,20);
    mcoordsy.setForeground(Color.blue);
    mcoordsy.setBackground(Color.black);
    add(mcoordsy);

    addMouseListener( this );
    addMouseMotionListener( this );

    textfield1= new TextField("byebye");
    textfield1.setBounds(400, 150, 50, 20);   
    add(textfield1);
    textfield1.setVisible(false);


     button1 = new Button("Move brick");
     button1.setBounds(b1xo,b1yo+110,75,20);
     add(button1);
     button1.setVisible(false);
     button1.addActionListener(this);

    }

    public void start(){
     if(thread1bool == true){    
    (thread1 = new Thread(this)).start();
    }  
     if(thread2bool == true){    
    (thread2 = new Thread(this)).start();
    }   

    }
    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    public void stop(){
    thread1 = null;   
    thread2 = null;
    }

    public void run(){
        try{
     tr.waitForID(0);  
       tr.waitForID(1); 
         tr.waitForID(2);  

       } catch (InterruptedException e) {
        return;
        }     
     //background goes here  ///////////////////////
     if(bg1Imagebool==true){
     gBuffer.drawImage(bg1Image,0,0,this);
     }

     //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    //         animations frames 
    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    if(thread1bool == true){    
    try {
    while (thread1 == Thread.currentThread()) {
    res1int = (res1int+1)%res1Images.length;
    //gBuffer.drawImage(bg1Image[bg1int],0,0,this);//causes no flicker res 1,res 
    gBuffer.drawImage(res1Images[res1int],b1xo,b1yo,this);
    repaint();
    Thread.sleep(500);
    res1count = res1count + 1;
    counter1.setText("res1 "+res1count);
    }
    } catch (Exception e) {
    }
    }

  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    if(thread2bool == true){    
    try {
    while (thread2 == Thread.currentThread()) {
    res2int = (res2int+1)%res2Images.length;
    gBuffer.drawImage(res2Images[res2int],x2,y2,this);
    repaint();
    Thread.sleep(500);
    res2count = res2count + 1;
    counter2.setText("res2 "+res2count);
    }
    } catch (Exception e) {}
    } 
  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  
  }//end runnables 
  //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx  
   public void mouseEntered( MouseEvent e ){}
   public void mouseExited( MouseEvent e ){}
   public void mouseClicked( MouseEvent e ){

   Integer mx = e.getX();
   Integer my = e.getY();

   if(mx>b1xo && mx<b1xo+100 && my>b1yo && my<b1yo+100){  
   button1.setVisible(true);
   }
   else {
   button1.setBounds(b1xo,b1yo+110,75,20);   
   button1.setVisible(false);
   }

   if(brick1bool==true){  //selected

   b1xo = e.getX()-50;//on button click image attatch to mouse xy - 50 centre
   b1yo = e.getY()-50;


   brick1bool = false;//attach button1 to brick1
  }
 }
   /////////////////////////////////////////////////////
   public void mousePressed( MouseEvent e ){}
   //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
   public void mouseReleased( MouseEvent e ){}
    //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
   public void mouseMoved(MouseEvent e ){

     // get the latest mouse position   
   Integer mx = e.getX();
   Integer my = e.getY();

   mcoordsx.setText(""+mx.intValue());
   mcoordsy.setText(""+my.intValue());  

   if(mx>b1xo && mx<b1xo+100 && my>b1yo && my<b1yo+100){ //here coords change
   mouseover = "brick1";
   button1.setBounds(b1xo,b1yo+110,75,20);

   }
   else{    
   mouseover = "nothing";
   }
   /////////////////////////////////////////////
   if(brick1bool==true){
   b1xo = mx - 50; 
   b1yo = my - 50;
   button1.setVisible(false);
   }
   ////////////////////////////////////////////////

  }//end mouse MOVEMENT
   public void mouseDragged(MouseEvent e){}
   //xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
   ////////////////////////////////////////////////

   public void actionPerformed(ActionEvent evt){


    if(evt.getSource()==button1){
    brick1bool = true;
    }


   //xxxxxxxxxxxxxxxxxxxxxxxxxxxxend action event   
   }



 ////////////////////////////////////////////////////////  

    public void paint(Graphics g){ 

    if(tr.checkAll()==false){

    g.drawString("Loading...", 460, 330);
    }

    else{  
    g.drawImage (Buffer, 0, 0, this);


    g.drawString(mouseover, 800, 525);

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

Recommended Answers

All 3 Replies

At a quick look it seems you may not be clearing the Graphics before drawing in the new position, so the old drawing in still there.
ps If possible you should update your AWT components to the Swing equivalents, if only to get double-buffering as standard so you can simplify it out of your own code.
pps Welcome to DaniWeb. Our rules do include no double-posting between sites, but in this case you have given a reasonable explanation, so that's OK. Please note that any future double-postings will be handled according to our site rules.

not be clearing the Graphics before drawing in the new position,

Hi and thanks,I have tried repaint(); in all the positions after attatching the object to the mouse to clear the old object from the background which does not seem to do the trick.
Theoretically it makes sense though.

repaint just tells Java that it neeeds to schedule a call to (your) paint method. It doesn't clear anything.
In the code where you draw the image at the new position you need to clear the old contents from the Graphics first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.