0

Whenever I tried to run the program, yes the text is moving but it leaves a trace of its last position. Like for example the text "test" is in first positioned at x=50, y=100 then in the next update, the x=50, y=120. The problem is, the last position which is the x=50, y=100 is still in display. I would like to remove the last position of the text every time it updates.

Can you guys help me?

(The j label is in the Action performed method)

This is my core class.

package GAME;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GameFrame extends JPanel implements ActionListener{

Image background = Toolkit.getDefaultToolkit().createImage("D:\\SHIPMATH TEXTURES\\simple-water-animation-597-x-800.gif");


Timer mainTimer;

shipPlayer player;
enemyShips enemy;


int level = 5;
int enemyCount = 5;
int numCount = 10;

int y=0;

// arraylists are arrays that has no capacity limit
static ArrayList<Numbers> number = new ArrayList<Numbers>();
static ArrayList<enemyShips> enemies = new ArrayList<enemyShips>();
static ArrayList<Cannons> cannon_balls = new ArrayList<Cannons>();

JLabel numbers;

Random rn = new Random();

public GameFrame() {
    // calls the actionPerformed method every 10 milliseconds   
    mainTimer = new Timer(10, this);

    mainTimer.start();
    setLayout(null);











    setFocusable(true);

    player = new shipPlayer(0, 500);
    addKeyListener(new KeyAdapt(player));



    startGame();

}


public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;

    g2d.drawImage(background, 0, 0, this);

    player.draw(g2d);






    repaint();


    for(int i=0; i<enemies.size(); i++) {

        // the value of 'i' is the location/index used to find the value stored in the ArrayList
        enemyShips enemy = enemies.get(i);
        enemy.draw(g2d);

    }

    for(int i=0; i<cannon_balls.size(); i++) {
        Cannons cannon = cannon_balls.get(i);
        cannon.draw(g2d);
    }







}



public void actionPerformed(ActionEvent e) {
    player.update();

    // where movement of the enemy happens
    for(int i=0; i<enemies.size(); i++) {
        enemyShips enemy = enemies.get(i);
        enemy.update();
    }


  //        Later...
    for(int i=0; i<cannon_balls.size(); i++) {
        Cannons cannon = cannon_balls.get(i);
        cannon.update();


    }

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

    y++;
    numbers = new JLabel("TEST");
    numbers.setBounds(200, y, 100, 100);

    add(numbers);

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

    repaint();


}


public static void addEnemy(enemyShips e) {

    enemies.add(e);
    // stores what the user puts in the enemyShips' object into the ArrayList "enemies"
}

public static void removeEnemy(enemyShips e) {
    enemies.remove(e);
    // removes what the user inputs from the ArrayList
}

public static ArrayList<enemyShips> getEnemyList() {
    return enemies;
}



public static void addNumbers(Numbers n) {
    number.add(n);
}


public static void addCannons(Cannons c) {

    cannon_balls.add(c);
    // stores what the user puts in the Cannons' object into the ArrayList "cannon_balls"
}

public static void removeCannons(Cannons c) {
    cannon_balls.remove(c);
    // removes what the user inputs from the ArrayList
}

public static ArrayList<Cannons> getCannonsList() {
    return cannon_balls;
}







public void startGame() {

//      enemyCount = level * 5;

    // runs 5 times
    for(int x=0; x<enemyCount; x++) {

        addEnemy(new enemyShips(rn.nextInt(500), -rn.nextInt(800)));

    }



}

}

This is the output of this program. Click here for the output of the program

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Feb 28 '20 at 22:57
  • When you're doing an animation, you have to completely redraw the image for each frame. Otherwise, you'll leave a trail or trace. – Gilbert Le Blanc Feb 29 '20 at 00:08

1 Answers1

0

you can use getContentPane().repaint() over and over in a loop or something else like some sort of timer.