java - 计时器无法正确触发

标签 java swing timer concurrency

我使用计时器来切换 boolean 值,但它并没有像预期那样每 250 毫秒触发一次,而是尽可能快地触发。这是我的代码:

package com.cgp.tetris;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.DataLine.Info;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class SinglePlayerMenu extends JPanel implements Runnable {
    private static final long serialVersionUID = 1L;
    private Thread thread;
    private AudioInputStream themestream, blipstream;
    private Clip clip, blipclip;
    private BufferedImage base, bi1, bi2, bi3, bi4, bi1s, bi2s, bi3s, bi4s;
    private boolean typeselected = false, b1 = true, b2 = false, b3 = true, b4 = false, b5 = false, b6 = false, b1a = true, b2a = false;

    public SinglePlayerMenu() {
        super();
        this.addComponentListener(new ComponentAdapter() {
            public void componentShown(ComponentEvent e) {
                SinglePlayerMenu.this.requestFocusInWindow();
            }
        });
    }

    public void addNotify() {
        super.addNotify();
        thread = new Thread(this);
        thread.start();
    }

    public void paint(Graphics g) {
        super.paint(g);

        g.drawImage(base, 0, 0, 640, 576, null);

        if (b1) {
            g.drawImage(bi1s, 80, 156, 240, 40, null);
        } else if (!b1) {
            g.drawImage(bi1, 80, 156, 240, 40, null);
        }
        if (b2) {
            g.drawImage(bi2s, 324, 156, 236, 40, null);
        } else if (!b2) {
            g.drawImage(bi2, 324, 156, 236, 40, null);
        }
        if (b3) {
            g.drawImage(bi1s, 80, 380, 240, 40, null);
        } else if (!b3) {
            g.drawImage(bi1, 80, 380, 240, 40, null);
        }
        if (b4) {
            g.drawImage(bi2s, 324, 380, 236, 40, null);
        } else if (!b4) {
            g.drawImage(bi2, 324, 380, 236, 40, null);
        }
        if (b5) {
            g.drawImage(bi3s, 80, 444, 240, 40, null);
        } else if (!b5) {
            g.drawImage(bi3, 80, 444, 240, 40, null);
        }
        if (b6) {
            g.drawImage(bi4s, 324, 444, 236, 40, null);
        } else if (!b6) {
            g.drawImage(bi4, 324, 444, 236, 40, null);
        }
    }

    public void run() {
        sound();
        loadImages();
        bind();
        while (true) {
            repaint();
            try {
                Thread.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            checks();
        }
    }

    private void checks() {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                System.out.println("Being run");
                if (b1a && !typeselected) {
                    b1 = !b1;
                }
            }
        }, java.util.Calendar.getInstance().getTime(), 250);
    }

    private void bind() {
        InputMap im = getInputMap();
        ActionMap am = getActionMap();

        im.put(KeyStroke.getKeyStroke("LEFT"), "left");
        am.put("left", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (!typeselected) {
                    if (!b1a) {
                        blipclip.start();
                        blipclip.setFramePosition(0);
                    }
                    b1 = true;
                    b2 = false;
                    b1a = true;
                    b2a = false;
                } else if (typeselected) {

                }
            }
        });

        im.put(KeyStroke.getKeyStroke("RIGHT"), "right");
        am.put("right", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (!typeselected) {
                    if (!b2a) {
                        blipclip.start();
                        blipclip.setFramePosition(0);
                    }
                    b2 = true;
                    b1 = false;
                    b1a = false;
                    b2a = true;
                } else if (typeselected) {
                }
            }
        });

        im.put(KeyStroke.getKeyStroke("SPACE"), "space");
        am.put("space", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (!typeselected) {
                    typeselected = true;
                    b1=b1a;
                    b2=b2a;
                } else if (typeselected) {

                }
            }
        });
    }

    private void loadImages() {
        try {
            base = ImageIO.read(new File("res/base.png"));
            bi1 = ImageIO.read(new File("res/1.png"));
            bi2 = ImageIO.read(new File("res/2.png"));
            bi3 = ImageIO.read(new File("res/3.png"));
            bi4 = ImageIO.read(new File("res/4.png"));
            bi1s = ImageIO.read(new File("res/1s.png"));
            bi2s = ImageIO.read(new File("res/2s.png"));
            bi3s = ImageIO.read(new File("res/3s.png"));
            bi4s = ImageIO.read(new File("res/4s.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sound() {
        File theme = new File("res/playtheme.wav");
        try {
            themestream = AudioSystem.getAudioInputStream(theme);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        DataLine.Info info = new DataLine.Info(Clip.class, themestream.getFormat());
        clip = null;
        try {
            clip = (Clip) AudioSystem.getLine(info);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
        try {
            clip.open(themestream);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        File blip = new File("res/blip.wav");
        try {
            blipstream = AudioSystem.getAudioInputStream(blip);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        DataLine.Info blipinfo = new DataLine.Info(Clip.class, blipstream.getFormat());
        blipclip = null;
        try {
            blipclip = (Clip) AudioSystem.getLine(blipinfo);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
        try {
            blipclip.open(blipstream);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void startClip() {
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        blipclip.start();
        blipclip.setFramePosition(0);
    }
}

遗憾的是,Eclipse 表现得很奇怪,因此 SSCCE 不可能成为 ATM。

最佳答案

你的基本代码对我来说看起来是错误的:

  1. 自定义绘制应该在paintComponent()方法中完成。

  2. 使用 Swing 时,您应该使用 Swing 计时器。

  3. 您有一个每 2 毫秒重新绘制一次的线程。这是你的问题吗?

  4. 您不需要带有 while () 循环的线程。这就是您使用计时器的目的。所以我猜想线程和计时器应该合并成一个 Swing 计时器。当时间触发时,您更新变量,然后调用 repaint()。

关于java - 计时器无法正确触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8287106/

相关文章:

java - 如何让 Java 程序暂停,直到按下特定按钮?

java - 如何在 Jframe 中显示 3d 模型

javascript - 如何在刷新页面时停止重置 JavaScript 计时器

java - 如何构建具有开始和结束字符验证的验证正则表达式?

java - 释放BufferedReader、InputStreamReader和InputStream获取的资源

java - 常量的命名;先输入还是先命名?

java - 显示 JList

Java:使用GSON进行JSON映射

C# - 如何在一段时间内阻止方法返回?

c# - 定时器内的日期比较