1

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

        }
    }
}
Zong
  • 6,160
  • 5
  • 32
  • 46
Gabo
  • 47
  • 1
  • 7
  • 1
    You should tag this with whatever programming language you're using here. – user24601 Jan 01 '14 at 19:42
  • 1
    There should NEVER be a need to synchronise the paintComponent method, as you should never be calling it directly. The paint process is controlled by the RepaintManager and Event Dispatching Thread, meaning the a paint cycle should only ever be controlled by the EDT... – MadProgrammer Jan 01 '14 at 20:22
  • 1
    You should never have a need to use getGraphics, this is not how custom painting is done in Swing, instead, issue a repaint request back to the RepaintManager (via the repaint method). In 15 years of Swing development, if never used Toolkit.sync. In your example, paintComponent will override anything that is painted by the render method... – MadProgrammer Jan 01 '14 at 20:24

1 Answers1

2
  • 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

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319