Results 1 to 5 of 5
  1. #1
    Sarah24 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Exclamation outputting 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:

    import java.util.*;
    import java.applet.Applet;
    import java.awt.*;
    import java.awt.event.*;
    public class Harvey_L_groupE 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

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Just a word to the wise- saying things like "help asap" actually decreases your chances of getting help.

    But for starters, give this a read:Using Text Components (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Hint- An easy way to do this is to use HTML.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    Now how did I know you were going to crosspost this?

    Java Applet, display different letters in different colours
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Sarah24 is offline Member
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default

    I cant do this in HTML it has to be in the java code.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default

    It would be Java. Swing supports basic HTML tags.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Help With Outputting Data To A File
    By Prodigial Mouse in forum New To Java
    Replies: 14
    Last Post: 01-24-2011, 09:01 PM
  2. Outputting a * triangle
    By blackbeltsas in forum New To Java
    Replies: 1
    Last Post: 10-17-2010, 12:39 PM
  3. How to print words in different colours?
    By veen in forum New To Java
    Replies: 7
    Last Post: 06-10-2010, 05:54 PM
  4. Replies: 0
    Last Post: 01-27-2010, 04:52 PM
  5. PDFBOX change text colours and remove background
    By gstkein in forum Advanced Java
    Replies: 1
    Last Post: 11-20-2009, 12:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •