2015-08-15 41 views
1

我已經搜索並試圖實現一個自己的計時器,並不能爲我的生活弄清楚。所以我想要做的就是每隔五秒鐘,我想讓隨機圓圈變成一個新的隨機位置。這似乎很簡單,但我不能讓它工作。給我的代碼添加一個定時器

public class BounceThread implements Runnable { 
    JFrame frame; 
    int myX = 500/2; 
    int myY = 500/2;; 

    int X; 
    int Y; 

    Canvas canvas; 
    BufferStrategy bufferStrategy; 
    BufferStrategy bufferStrategy2; 
    boolean running = true; 
    Random rand = new Random(); 
    Random rand2 = new Random(); 

    int n = rand.nextInt(450) + 1; 
    int n2 = rand.nextInt(450) + 1; 

    public BounceThread() { 
    frame = new JFrame("Basic Game"); 
    JPanel panel = (JPanel) frame.getContentPane(); 
    panel.setPreferredSize(new Dimension(500, 500)); 
    panel.setLayout(null); 
    canvas = new Canvas(); 
    canvas.setBounds(0, 0, 500, 500); 
    canvas.setIgnoreRepaint(true); 
    panel.add(canvas); 
    canvas.addKeyListener(new KeyAdapter() { 
     @Override 
     public void keyPressed(KeyEvent evt) { 
     moveIt(evt); 
     } 
    }); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.pack(); 
    frame.setResizable(false); 
    frame.setVisible(true); 
    canvas.createBufferStrategy(2); 
    bufferStrategy = canvas.getBufferStrategy(); 
    canvas.requestFocus(); 
    } 

    public void run() { 
    while (running = true) { 
     Paint(); 
     if ((myX == n || myX == n) && (myY == n2 || myY == n2)) { 

     n = rand.nextInt(450) + 1; 
     n2 = rand.nextInt(450) + 1; 
     n2 = 10 * (Math.round(n/10)); 
     n = 10 * (Math.round(n/10)); 
     Paint(); 

     } 
    } 
    } 

    public static void main(String[] args) { 
    BounceThread ex = new BounceThread(); 
    new Thread(ex).start(); 
    } 

    public void Paint() { 

    Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics(); 
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
     RenderingHints.VALUE_ANTIALIAS_ON); 
    g.clearRect(0, 0, 500, 500); 
    Graphics2D e = (Graphics2D) bufferStrategy.getDrawGraphics(); 
    e.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
     RenderingHints.VALUE_ANTIALIAS_ON); 
    e.clearRect(0, 0, 500, 500); 

    Paint(g, e); 

    bufferStrategy.show(); 
    } 

    protected void Paint(Graphics2D g, Graphics2D e) { 
    n2 = 10 * (Math.round(n/10)); 
    n = 10 * (Math.round(n/10)); 
    g.fillOval(myX, myY, 30, 30); 

    g.setColor(Color.red); 

    e.fillOval(n, n2, 30, 30); 

    e.setBackground(Color.white); 
    } 

    public void moveIt(KeyEvent evt) { 
    switch (evt.getKeyCode()) { 
    case KeyEvent.VK_DOWN: 
     if (myY >= 450) { 
     break; 
     } else { 
     myY += 10; 
     break; 
     } 
    case KeyEvent.VK_UP: 
     if (myY <= 30) { 
     break; 
     } else { 
     myY -= 10; 
     break; 
     } 
    case KeyEvent.VK_LEFT: 
     if (myX <= 30) { 
     break; 
     } else { 
     myX -= 10; 
     break; 
     } 
    case KeyEvent.VK_RIGHT: 
     if (myX >= 450) { 
     break; 
     } else { 
     myX += 10; 
     break; 
     } 
    } 
    } 
} 

回答

1

我將從一個組件(如JComponent)擴展並讓JFC/Swing儘可能多地提升。請不要撥打電話canvas.setIgnoreRepaint(true);,並記住在每次移動後都要撥打repaint();。接下來,您需要ActionListener作爲Timer事件。您的switch邏輯可以用邏輯if-else之外的break表示。像

public class BounceThread extends JComponent implements ActionListener { 
    JFrame frame; 
    int myX = 500/2; 
    int myY = 500/2;; 

    int X; 
    int Y; 

    Canvas canvas; 
    BufferStrategy bufferStrategy; 
    boolean running = true; 
    Random rand = new Random(); 

    int n = rand.nextInt(450) + 1; 
    int n2 = rand.nextInt(450) + 1; 

    public BounceThread() { 
     frame = new JFrame("Basic Game"); 
     JPanel panel = (JPanel) frame.getContentPane(); 
     panel.setPreferredSize(new Dimension(500, 500)); 
     panel.setLayout(null); 
     canvas = new Canvas(); 
     canvas.setBounds(0, 0, 500, 500); 
     // canvas.setIgnoreRepaint(true); 
     panel.add(canvas); 
     canvas.addKeyListener(new KeyAdapter() { 
      @Override 
      public void keyPressed(KeyEvent evt) { 
       moveIt(evt); 
       repaint(); 
      } 
     }); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setResizable(false); 
     frame.setVisible(true); 
     canvas.createBufferStrategy(2); 
     bufferStrategy = canvas.getBufferStrategy(); 
     canvas.requestFocus(); 
    } 

    public static void main(String[] args) { 
     BounceThread ex = new BounceThread(); 
     int delay = 5000; // milliseconds 
     new Timer(delay, ex).start(); 
    } 

    @Override 
    public void repaint() { 
     Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics(); 
     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
       RenderingHints.VALUE_ANTIALIAS_ON); 
     Graphics2D e = (Graphics2D) bufferStrategy.getDrawGraphics(); 
     e.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
       RenderingHints.VALUE_ANTIALIAS_ON); 
     g.clearRect(0, 0, 500, 500); 
     e.clearRect(0, 0, 500, 500); 
     Paint(g, e); 
     bufferStrategy.show(); 
     super.repaint(); 
    } 

    protected void Paint(Graphics2D g, Graphics2D e) { 
     g.fillOval(myX, myY, 30, 30); 
     g.setColor(Color.red); 
     e.fillOval(n, n2, 30, 30); 
     e.setBackground(Color.white); 
    } 

    public void moveIt(KeyEvent evt) { 
     switch (evt.getKeyCode()) { 
     case KeyEvent.VK_DOWN: 
      if (myY < 450) { 
       myY += 10; 
      } 
      break; 
     case KeyEvent.VK_UP: 
      if (myY > 30) { 
       myY -= 10; 
      } 
      break; 
     case KeyEvent.VK_LEFT: 
      if (myX > 30) { 
       myX -= 10; 
      } 
      break; 
     case KeyEvent.VK_RIGHT: 
      if (myX < 450) { 
       myX += 10; 
      } 
      break; 
     } 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     n = rand.nextInt(450) + 1; 
     n2 = rand.nextInt(450) + 1; 
     repaint(); 
    } 
} 
+0

爲什麼重寫'repaint()'而不是'paintComponent()'? – durron597

+0

@ durron597因爲這樣我可以重用OP的大部分代碼。 –

-1

添加以下到您的run()方法的開始,在while循環前的第一行可能做你要找的內容:

TimerTask task = new TimerTask() {@ 
     Override 
     public void run() { 
      n = rand.nextInt(450) + 1; 
      n = 10 * (Math.round(n/10)); 
     } 
    }; 
    Timer timer = new Timer(); 
    timer.scheduleAtFixedRate(task, 0, 5 * 1000); 

只給你,雖然一個想法,我認爲總體設計(代碼重用等)必須改進。

+1

對不起,但這不是一個好主意。您不應該推薦在Swing GUI中使用java.util.Timer,因爲這有可能會導致創建代碼,使其與Swing線程規則發生衝突。 –

+1

查看Elliott Frisch的答案,瞭解如何使用更合適的擺動計時器。 1+。 –