2013-02-11 21 views
0

我想顯示一個數組,但我有問題。如何以圖形方式重新繪製字符串數組而不重疊文本?

在我填滿數組之前,鞋子「null」,但填充數組後,它會繪製「null」,這使得它很難閱讀。

我怎樣才能讓畫布在我填滿陣列後清理並重繪。

這是到目前爲止我的代碼:

public class wordManager extends JFrame 
{ 
String[] array = new String[15]; 

private BufferedImage buffered; 
public wordManager() 
{ 
    super("Word Managery"); 


    setSize(300,600); 
    setVisible(true); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 

public void paint(Graphics window) 
{ 
    if(buffered==null) 
      buffered = (BufferedImage)(createImage(getWidth(),getHeight())); 

    Graphics windowTemp = buffered.createGraphics(); 

    int y = 50; 
    for(int i = 0; i<array.length; i++) 
    { 
     windowTemp.drawString(array[i] + "", 10,y); 
     y+=10; 

    } 

    window.drawImage(buffered, 0, 0, null); 
} 


public void read(String filename) throws IOException 
{ 

    String word; 
    int i = 0; 
    Scanner file = new Scanner(new File(filename+".txt")); 
    while(file.hasNext()) 
    { 
     word = file.next(); 
     array[i] = word; 

     i++; 
    } 
    repaint(); 
} 


public void scramble() 
{ 
    for(int i=0;i<array.length;i++) 
    { 
     int a = (int) (Math.random()*array.length); 
     String b = array[i]; 
     array[i] = array[a]; 
     array[a] = b; 
    } 
    repaint(); 
} 

public void sort() 
{ 
    for (int i = 1; i < array.length; i++) 
    { 
     int s = i-1; 
     for (int j = i; j < array.length; j++) 
     { 
      if (array[j].compareTo(array[s]) < 0) 
      { 
       s = j; 
      } 
     } 
     String temp = array[i-1]; 
     array[i-1] = array[s]; 
     array[s] = temp; 
    } 
    repaint(); 
} 


public void write() throws IOException 
{ 
    PrintWriter fileOut = new PrintWriter(new FileWriter("out.txt")); 

    for(int i = 0; i<array.length; i++) 
    { 
     fileOut.println(array[i]); 
    } 

    fileOut.close(); 

} 

public void printArray() 
{ 
    for(String term : array) 
    { 
     System.out.println(term); 
    } 
} 

} 

public class runner 
{ 
public static void main(String args[]) throws IOException 
{ 
    wordManager run = new wordManager(); 

    Scanner keyboard = new Scanner(System.in); 
    System.out.println("In put file name"); 
    String filename = keyboard.next(); 

    run.read(filename); 

    System.out.println(""); 
    run.printArray(); 
    System.out.println(""); 

    System.out.println("Enter 1 if you want to sort\n"); 
    System.out.println("Enter 2 if you want to scramble"); 


    int selection = keyboard.nextInt(); 


     if(selection == 1) 
     { 
      run.sort(); 
     } 

     if(selection == 2) 
     { 
      run.scramble(); 
     } 

    run.printArray(); 
    System.out.println(""); 

    run.write(); 
} 
} 

回答

1

您未能兌現油漆鏈。

油漆做了很多重要的工作,最重要的是爲您準備了用於渲染的圖形。

Graphics是一個共享資源,它可以通過重繪管理器需要重繪的所有組件。部分繪畫過程是清除圖形,通常通過調用super.paint

現在,已經說了。您應該很少需要重寫頂層容器的paint方法,否則頂層容器不是雙緩衝區。

相反,您應該創建一個自定義組件,如JPanel,並重寫它的paintComponent方法。

圖形環境中的文本具有基於當前字體的指標。簡單地使用一個幻數來計算文本應該出現的下一行會在字體改變時(它會)產生許多令人討厭的結果。

enter image description here

相反,你應該這樣做......

FontMetrics fm = windowTemp.getFontMetrics(); 
int y = 50; 
for(int i = 0; i<array.length; i++) 
{ 
    windowTemp.drawString(array[i] + "", 10,y + fm.getAscent()); 
    y+=fm.getHeight(); 
} 

看看Working with Text APIs

+0

我一直使用paintComponent(),但是我的老師給我們提供了這個新的代碼 現在我該如何克服我的問題 我是初學者 – user1972355 2013-02-11 04:32:29

+1

如果你不能改變代碼,調用'super.paint(window)'作爲'paint'方法的第一行,然後找一個新老師; – MadProgrammer 2013-02-11 04:34:13

+0

+ 1爲線高度計算。我打算編輯我​​的帖子,但你打敗了我:) – camickr 2013-02-11 04:43:18

1

不要忽略一個JFrame(或其他任何頂級窗口)的paint()方法。

自定義繪畫是通過覆蓋JPanel的paintComponent()方法,然後將面板添加到框架來完成的。

不要忘記調用super.paintComponent(...)作爲方法中的第一條語句,以確保清除背景。

閱讀關於Custom Painting的Swing教程以獲取更多信息。

也許更簡單的解決方案是隻顯示JTextArea中的文本,因此您不必重新創建3輪。

+0

+1的一切,但尤其是'JTextArea' – MadProgrammer 2013-02-11 04:24:40

+0

我會怎樣處理重疊問題? – user1972355 2013-02-11 04:39:02

+0

我的答案告訴你如何處理這個問題。嘗試一下。如果您遇到問題,請重新發布您的SSCCE。 – camickr 2013-02-11 04:41:47

相關問題