使它们移动和同步



我正在尝试通过面板进行图形移动,但我有其他人在左右移动的电线中,第一个问题不同步,第二个问题不移动,任何人都知道如何使它旋转,如果我将线程分开并使其移动,谢谢

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
        }
    }
}
  • 使用摆动计时器而不是Runnable#ThreadThread.sleep(int)停止

  • 阅读有关 Java 命名约定的信息

  • 覆盖 JPanel 的getPreferredSize而不是setPreferredSize(new Dimension(420, 420));

  • paintComponent内的所有坐标均可通过getHeight/Wieght

  • 访问
  • 使用 KeyBindings 而不是 KeyListener

  • 否则,您(为Container启用KeyEvents)必须setFocusable() JPanel

  • 1st. 代码行应super.paintComponentpublic synchronized void paintComponent(Graphics g) {

  • 你不能g = this.getGraphics()编码;,一切都可以paintComponent

  • 请参阅初始线程

  • 使用 JFrame.pack(); 而不是 ven.setSize(460, 430) ; 如果您覆盖了 JPanel 的getPreferredSize

  • Thread.sleep(10);

    a) 本机操作系统中非常短的延迟过载延迟

    b)阻止事件调度线程,我建议再次使用摆动计时器代替

  • 通过将所有内容(不包括摆动计时器)放在一起 - 例如

相关内容

  • 没有找到相关文章

最新更新