2 Replies - 185 Views - Last Post: 08 October 2011 - 10:17 PM Rate Topic: -----

#1 SashaThompson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-September 11

The words my code output get painted over the top of each other

Posted 08 October 2011 - 06:00 PM

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?
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


Thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: The words my code output get painted over the top of each other

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10442
  • View blog
  • Posts: 38,677
  • Joined: 27-December 08

Re: The words my code output get painted over the top of each other

Posted 08 October 2011 - 09:23 PM

A few things. First, please properly indent your code so it is easier to read. Second, avoid using AWT since it is a dead (and glitchy) technology. Use the more modern Swing JComponents like JApplet, JButton, JCheckBox, etc. Third, avoid null layout. Java has great LayoutManagers that are easy to use, so you should use those instead. Fourth, when overriding paint() (or paintComponent()), always invoke the super.paint() (or super.paintComponent() method) to handle the key superclass functionality. That also erases the previous paint. Lastly, why not just use a JLabel to display the answer and use the setText() method on it to update the text.
Was This Post Helpful? 0
  • +
  • -

#3 SashaThompson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 22-September 11

Re: The words my code output get painted over the top of each other

Posted 08 October 2011 - 10:17 PM

View Postmacosxnerd101, on 08 October 2011 - 09:23 PM, said:

A few things. First, please properly indent your code so it is easier to read. Second, avoid using AWT since it is a dead (and glitchy) technology. Use the more modern Swing JComponents like JApplet, JButton, JCheckBox, etc. Third, avoid null layout. Java has great LayoutManagers that are easy to use, so you should use those instead. Fourth, when overriding paint() (or paintComponent()), always invoke the super.paint() (or super.paintComponent() method) to handle the key superclass functionality. That also erases the previous paint. Lastly, why not just use a JLabel to display the answer and use the setText() method on it to update the text.

I really like your idea of using labels, how would I be able to do that?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1