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();
}
}
4

2 回答 2

1

不要覆盖 JFrame(或任何其他顶级窗口)的 paint() 方法。

自定义绘画是通过覆盖 JPanel 的 paintComponent() 方法完成的,然后将面板添加到框架中。

并且不要忘记调用 super.paintComponent(...) 作为方法中的第一条语句,以确保背景被清除。

阅读自定义绘画的 Swing 教程以获取更多信息。

也许更简单的解决方案是只在 JTextArea 中显示文本,这样您就不必重新发明轮子。

于 2013-02-11T04:19:51.107 回答
1

你没有尊重油漆链。

Paint 做了很多重要的工作,尤其是对您而言,它为渲染准备图形。

Graphics是一个共享资源,它可以通过重绘管理器需要重绘的所有组件。绘制过程的一部分是清除图形,通常通过调用super.paint.

现在,说了这么多。您应该很少需要覆盖paint顶级容器的方法,如果没有别的,顶级容器不是双缓冲的。

相反,您应该从类似的东西创建一个自定义组件,JPanel并覆盖它的paintComponent方法。

图形环境中的文本具有基于当前字体的度量。简单地使用一个幻数来计算文本应该出现的下一行会在字体发生变化时产生很多令人讨厌的结果(而且它会)。

在此处输入图像描述

相反,你应该做类似...

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();
}

查看使用文本 API

于 2013-02-11T04:23:44.240 回答