Jump to content

Help...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
lowerlifeform

lowerlifeform

    Newbie

  • Members
  • Pip
  • 1 posts
How can i possibly remove the first GLabel that was displayed in the canvas. The label that was added wont go away. Here is my code. Help please....tnx

import acm.program.*;
import acm.util.*;

public class Guess extends ConsoleProgram {

public void run() {
initialize();

while(true) {
String str = new String(ch);
canvas.displayWord(str);
println(str);
askGuess();
}
}

private void initialize() {
for(int i = 0; i < word.length(); i++) {
ch[i] = '-';
}
}

private void askGuess() {
String guess = readLine("Enter guess: ");
for(int i = 0; i < word.length(); i++) {
if(guess.charAt(0) == word.charAt(i)) {
ch[i] = word.charAt(i);
}
}
}

public void init() {
canvas = new Display();
add(canvas);
}

private Display canvas;
private String word = "HELLO";
char[] ch = new char[word.length()];
}
------------------------------------------------------------------------------------------------------------

import acm.graphics.*;

public class Display extends GCanvas {

public void displayWord(String word) {
GLabel lab = new GLabel(word, 100, 25);
add(lab);
}

}

Attached Files



#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Never really heard of acm but here goes nothing ;)
Could you test the following?

public class Display extends GCanvas {

  private GLabel lab;
  
  public Display(){
    lab = new GLabel();
    add(lab);
  }

  public void displayWord(String word) {
    lab.setLabel(word);
  }
}


#3
GMVResources

GMVResources

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
I think what you need to do is delete all the code you used for the first label then make the 2nd one.