Java Applet, display different letters in different colours
I have made a textfield applet, and would like it to do the following:
If the string typed in contains the letter 'l', then i would like it to always be displayed in red, otherwise I would like the text to be displayed in green.
Here is the code I have so far:
Code Java:
import java.util.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Sarah extends Applet implements ActionListener{
//public static void main(String[] args)
// TODO Auto-generated method stub
String name;
TextField input;
Color col;
Font f;
Scanner sc=new Scanner (System.in);
char firstname;
public void init()
{
f = new Font("Helvetica", Font.BOLD,19);
setBackground (Color.white);
input = new TextField(20);
do
{
firstname = sc.next().charAt(0);
} while (firstname <'A' || firstname > 'Z');
if (firstname == 'l')
{
col=Color.red;
}
else
{
col=Color.green;
}
add(input);
input.addActionListener(this);
}
public void actionPerformed (ActionEvent e)
{
name = e.getActionCommand();
repaint();
}
public void paint (Graphics g)
{
g.setFont(f);
g.setColor(col);
g.drawString(name, 75, 75);
}
}
Any help asap would really be appreciated! Thank you
Re: Java Applet, display different letters in different colours
Re: Java Applet, display different letters in different colours
I was not aware this isn't allowed. I will put a link on the other website to this one. :)
Re: Java Applet, display different letters in different colours
It's not that it isn't allowed (in fact my post says that it is allowed), it can just be wasteful for people who aren't aware that you're getting help in multiple places, which many consider very rude and will actually decrease your chances of getting help in the future. Recommended reading: http://www.javaprogrammingforums.com...s-posting.html
Re: Java Applet, display different letters in different colours
Hello sarah24, welcome to the forums.
So what is the problem with the code you have posted? Does any of it work?
By the way, I have moved this thread to - Java Applets