2016-03-24 14 views
-1

Merhaba Basit bir arcanoid oyun yazmak istedim ama bir sebepten dolayı benim JPanel.repaint() hiç çağrılmadı ve topum hareket etmiyor. Yani ana sınıfımda JPanel'i JPanel'e genişletiyorum Kullanıcı bir oyun başlatmak istediğinde çağrılan bir dinleyici ekliyorum ve sonuç olarak bir iplik işlendiğinde bu iş parçacığı topun pozisyonunu değiştirir ve (ideal olarak) jpanel sınıfı. Topun değiştiğini kontrol ettim ama boyama metodu hiç çağrılmadı. Birisi lütfen yardım edebilir mi?neden JPanel asla bir reapint çağırmaz

public class test extends JPanel{ 
int x=250; 
int y=470; 
int width=100; 
int height=20; 
Ball b=new Ball(); 
public static void main(String[] args){ 
    test t=new test(); 
    t.draw(); 
} 
public void draw(){ 
    JFrame frame=new JFrame(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setSize(520, 520); 
    frame.setResizable(false); 
    this.setSize(500,500); 
    frame.add(this); 
    this.addMouseListener(new mouseL()); 
    frame.setVisible(true); 
} 
public void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    System.out.println("Yeah I am called"); 

    Graphics2D g2d=(Graphics2D) g; 
    g2d.setColor(Color.blue); 
    g2d.fillRect(x, y, width, height); 
    g2d.setColor(b.getColor()); 
    g2d.fillOval(b.x1, b.y1, b.width1, b.height1); 
} 
class Ball{ 
    Random rand=new Random(); 
    int x1=300; 
    int y1=450; 
    int height1=20; 
    int width1=20; 
    Color c; 
    public Color getColor(){ 
     return new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255)); 
    } 
} 
class mouseL implements MouseListener{ 
    Thread t=new Thread(new MyRun()); 

    @Override 
    public void mouseClicked(MouseEvent e) { 
     // TODO Auto-generated method stub 
     t.run(); 


    } 

    @Override 
    public void mousePressed(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseEntered(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseExited(MouseEvent e) { 
     // TODO Auto-generated method stub 

    } 

} 
class MyRun implements Runnable{ 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     try{ 
     while(true){ 
      //System.out.println("Yeah I work"); 
      test.this.b.x1=test.this.b.x1-1; 
      test.this.b.y1=test.this.b.y1-1; 
      System.out.println("the values are: "+test.this.b.x1+" and "+test.this.b.y1); 
      Thread.sleep(2000); 
      test.this.repaint(); 
     } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 

    } 

} 

} (sizin durumunuzda)

cevap

2

t.run(); aramalar Runnabe#run, böylece sonsuza kadar sizin UI dondurma, Olay Sevk Takip kapsamında yürütülür: Şimdiden teşekkürler benim kodudur.

Sen

Salıncak tek dişli olduğundan t.start() kullanmış gerekirdi, ırk koşulları ve kirli okuma riskiyle karşı karşıyadır/değişkenler arasında yazar. Kullanmadığınız neden daha güvenli ve daha basit bir çözüm de

gelen UI güncelleştirmek için güvenli hale o EDT içinden bildirimleri var ki programları, How to use Swing Timers bkz salıncak Timer kullanmak olacaktır, nedenlerle How can I set in the midst? bakmak JFrame#setSize ve bunun yerine, JPanel'un getPreferredSize yöntemini geçersiz kılın ve JFrame#pack yerine

+0

Tamam, bunu tamamen unuttuğum için teşekkürler! Çok teşekkürler!!! – user3428496