Output in applet keeps painting over the top of itself
This code is for a java applet, and you enter a word, hit the enter key, then click a check box and then the answer will output just below the check box, if you enter a word, hit the enter key, then choose a checkbox, the word will paint, if you then enter a different word it will the output will go over that first word, how can I fix that so that the first answer disappears?
Code :
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MiniCode extends Applet implements KeyListener, ItemListener {
TextField OriginalText;
String EnteredVerb;
boolean PoliteForm, PoliteForm2, PolitePast, PolitePast2;
int len;
Checkbox checkbox1, checkbox2, checkbox3 ,checkbox4, checkbox5;
public void init() {
setLayout (null);
OriginalText = new TextField();
OriginalText.setBounds(200, 200, 100,35);
add(OriginalText);
OriginalText.addKeyListener(this);
checkbox1 = new Checkbox("polite form present");
checkbox1.setBounds(350,180,130,35);
checkbox1.addItemListener(this);
add(checkbox1);
checkbox2 = new Checkbox("polite form past");
checkbox2.setBounds(350,230,130,35);
checkbox2.addItemListener(this);
add(checkbox2);
}
public void itemStateChanged(ItemEvent e) {
len = EnteredVerb.length();
if (e.getSource() == checkbox1) {
if (EnteredVerb.endsWith("iru") || EnteredVerb.endsWith("eru")){
PoliteForm = true;
repaint();
}
else if (EnteredVerb.endsWith("u")){
PoliteForm2 = true;
repaint();
}
}
if (e.getSource() == checkbox2){
if (EnteredVerb.endsWith("iru") || EnteredVerb.endsWith("eru")){
PolitePast = true;
repaint();
}
else if (EnteredVerb.endsWith("u")){
PolitePast2 = true;
repaint();
}
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
EnteredVerb = OriginalText.getText();
}
this.repaint(); //request a repaint
}//end KeyPressed()
public void paint(Graphics g)
{
if (PoliteForm)
{
g.drawString(EnteredVerb.substring(0… -2)) + "masu", 250,300);
}
if (PoliteForm2)
{
g.drawString(EnteredVerb.substring(0… -1)) + "imasu", 250, 300);
}
if (PolitePast)
{
g.drawString(EnteredVerb.substring(0… -2)) + "mashita", 250,300);
}
if (PolitePast2)
{
g.drawString(EnteredVerb.substring(0… -1)) + "imashita", 250, 300);
}
}//end paint()
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
}//end class
Re: Output in applet keeps painting over the top of itself
Calls super.paint(g) to have the parent class clear the component. And for what its worth, I encourage you to use Swing rather than AWT (Swing components start with 'J', so JApplet, JPanel, etc...and should you do so override the paintComponent method as opposed to paint).
Edit: And do not post a question multiple times to the forums. I have deleted your other post.