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 5 of 5

Thread: Java Applet, display different letters in different colours

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

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


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Applet, display different letters in different colours

    Sigh.

    This thread has been cross posted here:

    http://www.java-forums.org/java-applets/39833-outputting-different-letters-different-colours.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Extracting letters as variables
    By TH1 in forum Java Theory & Questions
    Replies: 3
    Last Post: February 11th, 2011, 07:22 AM
  2. Replies: 6
    Last Post: January 28th, 2011, 01:13 AM
  3. Display prime numbers from 100 to 200 in Java
    By c.P.u1 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 25th, 2011, 03:14 PM
  4. How to display ppt slides through java????
    By jj_crazy_1 in forum Java Theory & Questions
    Replies: 0
    Last Post: April 5th, 2010, 10:52 PM
  5. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 10:36 PM