now im gonna post the code that makes me going back to 0,
import javax.swing.*; import java.awt.event.*; import java.util.*; import javax.swing.Timer; /** * * @author */ public final class ScoreWindow extends JFrame { private Timer timer; public ScoreWindow(int finalScore) { initWindowComponents(finalScore); } private class ScoreWindowAnimationPanel extends JPanel implements ActionListener { private ArrayList fScoreNumberList; private AnimatedNumbers fScoreNumber; private int numImgIdx; public ScoreWindowAnimationPanel(int finalScore) { setFocusable(true); setDoubleBuffered(true); timer = new Timer(1, this); timer.start(); numImgIdx = 0; fScoreNumberList = new ArrayList(); createAnimatedNumberObjects(finalScore); } private void createAnimatedNumberObjects(Integer finalScore) { // creates a specific size for this array list object (fScoreNumberList) for (int x = 0; x < finalScore.toString().length() + 1; x++) { fScoreNumberList.add(new Object()); //fScoreNumberList.add(null); } // replace the elements on the indexes on this array list object for (int x = 0; x < finalScore.toString().length() + 1; x++) { fScoreNumberList.set(x, new AnimatedNumbers(x, x)); } // check all the instances on this array list object if they are // instance of the class (AnimatedNumbers) for (int x = 0; x < fScoreNumberList.size(); x++) { // here!, elements of this ArrayList Object are instances of this class!! System.out.println(fScoreNumberList.get(x) instanceof AnimatedNumbers); } } @Override public void actionPerformed(ActionEvent e) { if (numImgIdx < fScoreNumberList.size()) { // the problem is HERE!, elements are still object of Object.class fScoreNumber = (AnimatedNumbers) fScoreNumberList.get(numImgIdx); fScoreNumber.updateDelay(); } } } public void initWindowComponents(int finalScore) { add(new ScoreWindowAnimationPanel(finalScore)); setSize(505, 317); setVisible(true); } public static void showWindow(int finalScore) { new ScoreWindow(finalScore); } public static void main(String[] args) { ScoreWindow.showWindow(3642); } }
this is actually a simple animation that show a user's score on my game, to evaluate the problem,
i had a post in the same forum that asks whether is there a way to set an initial size of an ArrayList object, because i need to insert a certain instance on an index(i removed the insert-in-the-middle thing), then i was given a clarified answer that its not possible, so i made a way to make an initial size, as you can see on the code above,
1.) creating a for - loop with a certain loop
2.) adding elements(new Object()) or a null object to make the arraylist have its definite size
now i created another loop wherein i will replace the elements on it
1.) creating a for - loop with the same loop count
2.) replacing the elements using the set() method
PROBLEM:
the program is throwing me that the elements/element cannot be casted into an AnimatedNumbers objects(class will be posted below)
BUT I INCLUDED a for - loop that checks if the elements in that arraylist is an instance of AnimatedNumbers.class
it makes me confuse that in a certain part of the program the elements are instance of AnimatedNumbers.class but in another part of the program, the elements are STILL instance of Object.class? what is going on?
i will include the class that will support the issue here: AnimatedNumbers.class
public class AnimatedNumbers { private ArrayList numberImageList; private int numberImageIdx; private int x; private int delay; public AnimatedNumbers(int numberImageIdx, int x) { numberImageList = new ArrayList(); this.numberImageIdx = numberImageIdx; this.x = x; delay = 0; } public AnimatedNumbers(int x) { this.x = x; delay = 0; } }
i dont have any problems with this AnimatedNumbers.class, the only concern is , why does the elements on that ArrayList object doesnt change or not REPLACED in a certain part of the program