excuse me,
im doing atomix game and there is something that i can do as an extra thing which is while the atom moves i see it moving no just disappearing and showing in the next position?
thx :)

Recommended Answers

All 9 Replies

Keep a separate thread or timer that updates the state of your objects and calls repaint() on the AWT event queue to refresh them.

This is referred to as a game loop or animation loop.

excuse me,
but can u explain more?

while (board[currentI - 1][currentJ].equals(".")) {
                board[currentI - 1][currentJ] = board[currentI][currentJ];
                board[currentI][currentJ] = ".";

                currentI--;

this is part of moving code!

Take a look at the following post: http://www.daniweb.com/forums/post1146173.html#post1146173

Your movement code can't be running on the event queue because that will block repaints until your code is done. You have to update in one thread and repaint in another.

sry but how can i update in one thread:)?

Did you look at the post that I linked?

well yes but i didnt understand things in it :S :(

Perhaps this small example of animating the movement of a board piece with a thread might help. The important concept is in the BoardGrid.move() method, which is called when you press the button.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;


public class AnimateDemo extends JFrame{

    JButton btnMove = new JButton("Move");
    BoardGrid board = new BoardGrid();

    public AnimateDemo(){
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(btnMove, BorderLayout.NORTH);
        add(board, BorderLayout.CENTER);

        btnMove.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                board.move();
            }
        });
    }

    class BoardGrid extends JComponent{
        Ellipse2D player = new Ellipse2D.Float(0, 0, 0, 0);
        int width=0;
        int height=0;

        // this method is just here to simplify the example
        // your player position update code could be anywhere
        public void move(){
            // create a thread to move the player
            Thread moveIt = new Thread(new Runnable() {
                public void run() {
                    for (int i=0; i<3; i++){
                        // update position
                        player.setFrame(i*width/3, height/3, width/3, height/3);
                        // repaint
                        repaint();
                        // pause before next update
                        try {
                            Thread.currentThread().sleep(300);
                        } catch (InterruptedException ex) {
                            Thread.currentThread().interrupt();
                        }
                    }
                }
            });
            moveIt.start();
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D)g;

            width=getWidth();
            height=getHeight();

            // draw board
            g2.setColor(Color.BLUE);
            for (int i=1; i<3; i++){
                g2.drawLine(i*width/3, 0, i*width/3, height);
                g2.drawLine(0, i*height/3, width, i*height/3);
            }

            // draw player
            if (player.getBounds().getWidth()>0){
                g2.setColor(Color.ORANGE);
                g2.fill(player);
            }
        }

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                AnimateDemo demo = new AnimateDemo();
                demo.setSize(300, 300);
                demo.setVisible(true);
            }
        });
    }
}
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.