Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Output in applet keeps painting over the top of itself

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default 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.

Similar Threads

  1. How to detect mouse event while painting in a loop
    By r9reen in forum AWT / Java Swing
    Replies: 6
    Last Post: March 1st, 2011, 12:58 AM
  2. Adjusting Size of JPanel to Boundaries of Painting
    By aussiemcgr in forum Java Theory & Questions
    Replies: 6
    Last Post: November 22nd, 2010, 12:17 PM
  3. painting Image on JPanel inside a JScrollPane
    By becca23 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 29th, 2010, 07:28 PM
  4. [SOLVED] Extends JPanel painting problem
    By Philip in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 28th, 2010, 03:16 PM
  5. Painting swing components to an arbitrary position?
    By ScummyChimp in forum AWT / Java Swing
    Replies: 1
    Last Post: September 1st, 2009, 11:06 PM