-
Arrays in Applets
Right first of all, I'd just like to say Im still new to Java, Im a total beginner, so Im still not too great at coping with jargon, so please excuse me if my explanation isn't great.
Basically I've made an applet with a textfield and whatever you type into the textfield is output underneath it.
Now I need to create an array within the applet that takes the input and puts each char of the string into an array, of which is used to change the colour of certain letters upon output.
Here's my applet so far
Code:
import java.util.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class finalApplet extends Applet implements ActionListener
{
String text;
TextField input;
Font f;
public void init()
{
setBackground(Color.gray);
f = new Font("Calibri", Font.BOLD,15);
input = new TextField (20);
add(input);
input.addActionListener(this);
}
public void start()
{
text = "";
}
public void actionPerformed (ActionEvent e)
{
text = e.getActionCommand();
repaint();
}
public void paint(Graphics g)
{
g.setFont(f);
g.setColor(Color.blue);
g.drawString(text, 25, 100);
}
}
So for example, upon output, if any chars in the String which was input are the letter 'c' I want them to be yellow and any chars in the string which are the letter 'k' to be black.
If you follow me.
So Im reading up on arrays as we speak but still not too sure on how to do it, especially within an applet.
Any chance you guys could help out a rookie?
Thanks
-
Does this have to be an AWT Applet? Could it be a Swing JApplet?
-
Yeah it does Im afraid.
It's for an assignment and we've only learnt to use awt so far, so if I use something else it will be evident I didn't do it.
I dont have the intention of copying word for word any code that people may offer me here, I just need guidance with this because Im pretty much at my wits end with it right now. =/
And I'll never learn if I do just copy stuff.
EDIT
So I've been reading around and Im thinking I need to add something along the lines of:
Code:
for(int i = 0; i < stringArray; i++)
Code:
if(stringArray[i] == 'c')
g.setColor(Color.yellow)
But thats just an educated guess, not sure if any of the above code is wrong because I'm not actually sure where in the Applet code I would stick these chunks, seeing as I would still get an error if the code was right but in the wrong place.
Im guessing the char variable would be in the init() section?
Please excuse my amateurish terminology and such.
Thanks.