Question

I'm trying to make a graphic move through the panel but I have others in a moving wire from side to side the first problem is not synchronized and the second does not move anyone knows how to make it sincronize if I do threads separate and make it move thanks

public class caminos extends JPanel implements Runnable {
    int x1 = 0;
    int y1 = 50;
    int x2 = 400;
    int y2 = 150;
    int x3 = 0;
    int y3 = 250;
    int x = 200;
    int y = 350;
    int velX = 3;
    int velXX = -3;
    public boolean corren = true;
    Thread threadprincipal;

    caminos() {
        setPreferredSize(new Dimension(420, 420));
        addKeyListener(new personaje(this));

        threadprincipal = new Thread(this);
        threadprincipal.start();

    }

    public void up() {
        y = y - 3;
    }

    public synchronized void paintComponent(Graphics g) {
        g.setColor(Color.black);
        g.fillRect(0, 0, 460, 450);

        g.setColor(Color.red);
        g.fillRect(x1, y1, 40, 40);

        g.setColor(Color.blue);
        g.fillRect(x2, y2, 40, 40);

        g.setColor(Color.green);
        g.fillRect(x3, y3, 40, 40);

    }// this is the problem is not synchronized and does not move

    private synchronized void render() {

        Graphics g;
        g = this.getGraphics();

        if (g != null) {
            g.setColor(Color.orange);
            g.fillRect(x, y, 30, 30);

            Toolkit.getDefaultToolkit().sync();
        }
    }

    public void run() {

        while (corren) {
            render();

            x1 = x1 + velX;
            x3 = x3 + velX;
            x2 = x2 + velXX;

            if (x1 < 0 || x1 > 400) {
                velX = -velX;
            }

            if (x2 <= 0 || x2 > 400) {
                velXX = -velXX;
            }

            try {
                Thread.sleep(10);
            } catch (Exception e) {
            }
            //
            repaint();
        }
        // x1=x1+velX;
        // x3=x3+velX;
        // x2=x2-3;
    }

    public static void main(String[] args) {

        JFrame ven = new JFrame();
        ven.setSize(460, 430);
        ven.setTitle("game");
        ven.add(new caminos());
        ven.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ven.setVisible(true);
    }

    // another class to move
    class personaje implements KeyListener {

        caminos game;

        personaje(caminos passjuego) {
            game = passjuego;
        }

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                game.up();
            }
        }

        public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub

        }

        public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub

        }
    }
}
Was it helpful?

Solution

  • use Swing Timer instead of Runnable#Thread stopped by Thread.sleep(int)

  • read about Java Naming Convention

  • override getPreferredSize for JPanel instead of setPreferredSize(new Dimension(420, 420));

  • all coordinates inside paintComponent are accesible by getHeight/Wieght

  • use KeyBindings instead of KeyListener

  • otherwise you (enable KeyEvents for Container) have to setFocusable() for JPanel,

  • 1st. code line should be super.paintComponent inside public synchronized void paintComponent(Graphics g) {

  • you can't to code g = this.getGraphics();, everything could be done paintComponent

  • see Initial Thread

  • use JFrame.pack(); instead of ven.setSize(460, 430); in the case that you overrode getPreferredSize for JPanel

  • Thread.sleep(10);

    a) very short delay overloading latency in Native OS

    b) block Event Dispatch Thread, I'd suggest again to use Swing Timer instead

  • by put everything (exluding Swing Timer) together - for example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top