2013-02-15 20 views
0

我遇到問題了,我想要繪製一個方形(飛)並重繪以顯示移動。當按下按鈕時,該代碼很好,蒼蠅會「移動」,但舊的方塊不會刪除。我嘗試了enviromentPanel.repaint()updateui()和removeall(),我無法使它工作,如果我使用它們中的任何一個,那麼沒有任何形狀出現,我得到一個空白屏幕。圖形不會正常清除

import java.util.Random; 


public class Fly implements Runnable{ 
private int xPosition; 
private int yPosition; 
private boolean eaten; 

public Fly(){ 
    Random randomGenerator = new Random(); 
    xPosition = randomGenerator.nextInt(690) + 10; 
    yPosition = randomGenerator.nextInt(690) + 10; 
    eaten = false; 
} 

public int getxPosition() { 
    return xPosition; 
} 

public void setxPosition(int xPosition) { 
    this.xPosition = xPosition; 
} 

public int getyPosition() { 
    return yPosition; 
} 

public void setyPosition(int yPosition) { 
    this.yPosition = yPosition; 
} 

public boolean isEaten() { 
    return eaten; 
} 

public void setEaten(boolean eaten) { 
    this.eaten = eaten; 
} 

public void move(){ 
     Random randomGenerator = new Random(); 

     int xChange = -10 + randomGenerator.nextInt(20); 
     int yChange = -10 + randomGenerator.nextInt(20); 
     xPosition = xPosition + xChange; 
     yPosition = yPosition + yChange; 
     try { 
      Thread.sleep(1000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     move(); 

} 

@Override 
public String toString() { 
    return "Fly [xPosition=" + xPosition + ", yPosition=" + yPosition 
      + ", eaten=" + eaten + "]"; 
} 

@Override 
public void run() { 
    move(); 
} 


} 

import java.awt.Color; 
import java.awt.FlowLayout; 
import java.awt.Graphics; 
import java.awt.Panel; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import javax.swing.*; 
import java.awt.*; 
import javax.imageio.ImageIO; 

public class Enviroment2 implements Runnable,ActionListener{ 
private JFrame frame; 
private JPanel enviromentPanel,totalGUI,enviromentButtonPanel; 
private JButton newFrogButton, resetButton, hungryButton; 
private JTextField enterName; 
private JLabel hungryLabel; 
private ArrayList<Frog> frogs; 
private ArrayList<Fly> flys; 



public Enviroment2(){ 
totalGUI = new JPanel(); 
flys = new ArrayList<Fly>(); 
frogs = new ArrayList<Frog>(); 
enviromentPanel = new JPanel(); 
enviromentButtonPanel = new JPanel(); 
newFrogButton = new JButton("New Frog"); 
enterName = new JTextField("Enter name"); 

hungryButton = new JButton("Hungry!"); 

resetButton = new JButton("Reset"); 
frame = new JFrame("[=] Hungry Cyber Pet [=]"); 
JFrame.setDefaultLookAndFeelDecorated(true); 

frame.setContentPane(runEnviroment()); 

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setSize(740, 800); 
frame.setVisible(true); 



} 



public JPanel runEnviroment(){ 

totalGUI.setLayout(null); 

enviromentPanel.setLayout(null); 
enviromentPanel.setLocation(10, 10); 
enviromentPanel.setSize(700, 700); 
enviromentPanel.setBackground(Color.WHITE); 
totalGUI.add(enviromentPanel); 


FlowLayout experimentLayout = new FlowLayout(); 
enviromentButtonPanel.setLayout(experimentLayout); 
enviromentButtonPanel.setLocation(10, 710); 
enviromentButtonPanel.setSize(700, 50); 
totalGUI.add(enviromentButtonPanel); 

newFrogButton.setLocation(0, 0); 
newFrogButton.setSize(120, 30); 
newFrogButton.addActionListener(this); 
enviromentButtonPanel.add(newFrogButton); 

enterName.setLocation(140,0); 
enterName.setSize(120,30); 
enviromentButtonPanel.add(enterName); 

hungryButton.setLocation(280, 0); 
hungryButton.setSize(120, 30); 
hungryButton.addActionListener(this); 
enviromentButtonPanel.add(hungryButton); 

resetButton.setLocation(420, 0); 
resetButton.setSize(120, 30); 
resetButton.addActionListener(this); 
enviromentButtonPanel.add(resetButton); 


totalGUI.setOpaque(true); 

return totalGUI; 
} 


public void draw(){  

Graphics paper = enviromentPanel.getGraphics(); 

for (int i = 0; i <= flys.size()-1; i++){ 
    System.out.println("hi"); 
    paper.setColor(Color.BLACK); 

    paper.fillRect(flys.get(i).getxPosition(), flys.get(i).getyPosition(), 10,  10); 




} 
try { 
    Thread.sleep(1000); 
} catch (InterruptedException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

draw(); 

} 

public void actionPerformed(ActionEvent e) { 
if(e.getSource() == newFrogButton){ 
    Frog frog = new Frog(enterName.getText()); 
    frogs.add(frog); 
    Fly fly = new Fly(); 
    Thread t = new Thread(fly); 
    t.start(); 
    flys.add(fly); 
    showFlys(); 

    } 
    else if(e.getSource() == hungryButton){ 
    } 
    else if(e.getSource() == resetButton){ 
     frogs.clear(); 
     flys.clear(); 
     System.out.println(frogs); 
     System.out.println(flys); 


    } 
} 


public void showFlys(){ 
for (int i = 0; i <= flys.size()-1; i++){ 
    System.out.println(flys.get(i)); 
} 

} 



@Override 
public void run() { 
draw(); 
} 



} 
+1

順便說一下,你將有一個由move()方法引起的stackoverflow錯誤,它會以任何條件遞歸地調用它自己。用'while(true)'--loop替換它。 'draw()'方法也是如此。 – 2013-02-15 10:38:36

回答

5

Graphics paper = enviromentPanel.getGraphics()是你的問題的開始,這不是多麼風俗畫完成。

getGraphics返回上次繪製週期中使用的圖形上下文,最好是快照,最糟糕的是它可以是null

您不應該保留對您未創建的任何Graphics上下文的引用。他們可以改變和反過來繪畫可以產生意想不到的結果。

取而代之,您應該重寫paintComponent方法(可能在environmentPanel中)並在其中執行所有自定義繪畫。

你的第二個問題是你違反搖擺的螺紋規 - 你永遠不應該創建或則其他任何線程修改任何UI組件的EDT

你可能想採取通過

+2

+1 ... @ user1817988而不是重寫'getPreferredSize()'/使用'JPanel'的適當的'LayoutManager'並返回適合圖紙的'Dimension',而不是我們可以在'JFrame'上調用'pack()' – 2013-02-15 12:21:36