0

I am creating a crossword puzzle program that displays the crossword through applet. I'm attempting to create a method that will allow the user to enter his/her desired word. I call this enterWord() My issue comes in when I try to implement applet into it. I want my enterWord() method to allow the user to put in their newWord, the x coordinate and the y coordinate.

How can I change this code:

import java.awt.Graphics;
import java.applet.Applet;


public class crosswordMain extends Applet {

    String word;
    int wordlen;    

    public crosswordMain(){        
    }

    public void enterWord(String newWord, int xCoordinate, int yCoordinate){
        word = newWord;
        public void paint(Graphics g){            
            g.drawString(newWord, xCoordinate, yCoordinate);
        }
    }    

}

To make it work? The issue comes at the public void paint(Graphics g){ part.

Any help would be great! Thanks!

ElGavilan
  • 6,610
  • 16
  • 27
  • 36
  • 2
    It looks like you are new to Java. You have the paint() method inside enterWord(). This is not valid Java. Perhaps you would be better off reviewing a Java tutorial and then an Applet tutorial. – Gary May 27 '14 at 20:59
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson May 28 '14 at 08:01

1 Answers1

4

You cannot define a method inside another method in Java.
To implement this,you could:
1. Create a TextField and store it in a global variable.
TextField inputLine = new TextField(15);
2. Next,simply add this input string inside your drawString method like this:
g.drawString(inputLine,x,y)

HackCode
  • 1,837
  • 6
  • 35
  • 66