I am trying to display an array graphically but I am having problems.
It shoes “null” before I fill up the array which is fine, but after I fill up the array it draws over the “null” which makes it hard to read.
How can I make it so the canvas clears up and redraws after I fill up the array.
Here is my code so far:
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();
}
}
You’ve failed to honor the paint chain.
Paint does a lot of important work, most notably for you, it prepares the graphics for rendering.
Graphicsis a shared resource, it can get past to all the components that the repaint manager needs to repaint. Part of the paint process is to clear the graphics, typically by callingsuper.paint.Now, having said that. You should, very rarely, need to override the
paintmethod of top level container, if for nothing else, top level containers aren’t double buffered.Instead, you should be creating a custom component, from something like
JPanel, and overriding it’spaintComponentmethod.Text in a graphic environment have metrics, based on the current font. Simply using a magic number to calculate the next line that the text should appear on is going to produce lots of nasty results when the font changes (and it will).
Instead, you should be do something like…
Take a look at Working with Text APIs