Redrawing the contents of a AWT JFrame every second

36 Views Asked by At

When i use Timer and TimerTask to have it run the code every second it doesn't draw to the JFrame but still runs the code.

public void drawObject(Graphics g, int scale){
        g.setColor(colors[rand]);
        g.fillOval(xPos,yPos,scale*size,scale*size);
    }
    public void paint(Graphics g) {
        setBackground(Color.WHITE);

        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            boolean ting = false;
            public void run() {
                if(ting){
                    g.setColor(Color.white);
                    g.fillRect(0, 0, getWidth(), getHeight());
                }
                for(int i=0;i<jTP.physicsObjectArrayList.size();i++){
                    jTP.physicsObjectArrayList.get(i).drawObject(g,scale);
                    System.out.println("test");
                }
                for(int i=0;i<jTP.physicsObjectArrayList.size();i++) {
                    jTP.physicsObjectArrayList.get(i).setxPos(jTP.physicsObjectArrayList.get(i).getxPos()+getForceX(i)+getVelocityX(i));
                    jTP.physicsObjectArrayList.get(i).setyPos(jTP.physicsObjectArrayList.get(i).getyPos()+getForceY(i)+getVelocityY(i)+getGravity(i));
                }
                ting = true;
            }
        };

        timer.scheduleAtFixedRate(task, new Date(System.currentTimeMillis()), (long) 1000.0);

I am expecting it to draw the circle but it doesn't but it does print test to the command line. when i comment out the timer stuff it does draw the circle but then it doesn't have the delays

0

There are 0 best solutions below