2012-03-18 40 views
0

我試圖製作一個程序,在屏幕上球會自行移動。但問題是它不會重繪();JAVA移動球

任何建議如何解決它?

(主類)Main.java:

import javax.swing.*; 
import java.awt.*; 

public class Main extends JFrame{ 
    static int x = 10; 


    public static void main(String[] args){ 
     JFrame f = new JFrame("title"); 
     f.setVisible(true); 
     f.setSize(300,250); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     sekon m = new sekon(); 
     f.add(m); 

     antr t = new antr(); 
     Thread th = new Thread(t); 
     th.start(); 
    } 
} 

(第二類)sekon.java:

import javax.swing.*; 
import java.awt.*; 

public class sekon extends JPanel{ 
    int xiu = 10; 

    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     g.setColor(Color.RED); 
     g.fillOval(xiu, 10, 20, 20); 

    } 


    public void changeX(int b){ 
     this.xiu = b; 
    } 



} 

    class antr extends JPanel implements Runnable{ 
     int xi = 10; 
     sekon s = new sekon(); 
     public void run(){ 

      xi += 1; 
      s.changeX(xi); 
      JPanel p = new JPanel(); 
      p.repaint(); 

      try{ 
       Thread.sleep(5); 
       }catch(Exception e){} 
     } 
    } 
+1

1)'的Thread.sleep(5);'不要阻塞EDT。從Swing'Timer'調用該方法。 2)爲了更快地獲得更好的幫助,請發佈[SSCCE](http://sscce.org/)。 3)請爲代碼塊使用一致的邏輯縮進。 4)請學習通用的[Java命名約定](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307)(具體用於名稱的情況) ,方法和屬性名稱並一致使用。 – 2012-03-18 18:37:25

回答

1

sekonantr對象所擁有的實例是不同於所述一個加入GUI。

0

您的類antr和主類使用sekon的不同實例。您應該將在main方法中創建的實例傳遞給antr類。在你的代碼

2

1)repaint()阻止代碼行Thread.sleep(5);

2)你的代碼不起作用,因爲錯過了...,用於移動Oval throught屏幕

3)所有座標擺動容器,搖擺的JComponent只使用Swing Timer爲dealying,移動,重新粉刷,

肯定是可以通過使用Runnable#Thread,但不是這樣,

關於Swing Timer

import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 

    public class AnimationJPanel extends JPanel { 

     private static final long serialVersionUID = 1L; 
     private int cx = 0; 
     private int cy = 150; 
     private int cw = 20; 
     private int ch = 20; 
     private int xinc = 1; 
     private int yinc = 1; 

     public static void main(String[] args) { 
      EventQueue.invokeLater(new Runnable() { 

       @Override 
       public void run() { 
        AnimationJPanel panel = new AnimationJPanel(); 
        panel.setPreferredSize(new Dimension(400, 300)); 
        panel.animate(); 
        JFrame frame = new JFrame("Test"); 
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
        frame.getContentPane().add(panel); 
        frame.pack(); 
        frame.setLocationRelativeTo(null); 
        frame.setVisible(true); 
       } 
      }); 
     } 

     public AnimationJPanel() { 
      setLayout(new BorderLayout()); 
      JLabel label = new JLabel("This is an AnimationJPanel"); 
      label.setForeground(Color.RED); 
      label.setHorizontalAlignment(SwingConstants.CENTER); 
      add(label); 
      setBackground(Color.BLACK); 
      setForeground(Color.RED); 
      setOpaque(true); 
     } 

     public void animate() { 
      new Timer(15, new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        Rectangle oldCircle = new Rectangle(cx - 1, cy - 1, cw + 2, ch + 2); 
        cx += xinc; 
        cy += yinc; 
        if (cx >= getWidth() - cw || cx <= 0) { 
         xinc *= -1; 
        } 
        if (cy >= getHeight() - ch || cy <= 0) { 
         yinc *= -1; 
        } 
        repaint(oldCircle); 
        repaint(cx - 1, cy - 1, cw + 2, ch + 2); 
       } 
      }).start(); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.drawOval(cx, cy, cw, ch); 
     } 
} 
1

的示例研究以下示例以瞭解如何導致重繪動畫。

public static void main(String args[]) throws Exception { 
new JFrame("AnimationStudy") { 
    int x = 0; 
    JPanel j = new JPanel() { 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.setColor(Color.RED); 
     g.fillOval(x, 10, 20, 20); 
     g.setColor(Color.BLACK); 
     g.drawChars(("" + x).toCharArray(), 0, ("" + x).length(), x, 10); 
    } 
    }; 
    { 
    setSize(300, 100); 
    setLocation(300, 300); 
    setVisible(true); 
    setLayout(new BorderLayout()); 
    add(j); 
    new Thread(new Runnable() { 
     public void run() { 
     int t = 250; 
     for (x = 10; x < t; x += 1) { 
      j.repaint(); 
      try { 
      Thread.sleep((t - x)/4); 
      } catch (Exception e) { 
      e.printStackTrace(); 
      } 
     } 
     System.exit(0); 
     } 
    }).start(); 
    } 
}; 
} 
+1

也考慮'javax.swing.Timer';另請參閱[初始線程](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)。 – trashgod 2012-03-18 21:53:42