Results 1 to 3 of 3
  1. #1
    ChaosEvaUnit is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default 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

    Java 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    ChaosEvaUnit is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    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:

    Java Code:
    char stringArray[]
    Java Code:
    for(int i = 0; i < stringArray; i++)
    Java 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.
    Last edited by ChaosEvaUnit; 02-26-2011 at 07:59 PM.

Similar Threads

  1. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  2. New to Applets
    By Psyclone in forum Java Applets
    Replies: 8
    Last Post: 02-28-2010, 04:00 AM
  3. I need help with Applets
    By ProgrammingPup in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2009, 08:07 PM
  4. Can't see any Applets
    By pyr0chem in forum Java Applets
    Replies: 9
    Last Post: 12-17-2008, 04:24 AM
  5. applets on mac what´s going on?
    By willemjav in forum Java Applets
    Replies: 6
    Last Post: 04-19-2008, 07:03 PM

Tags for this Thread

Posting Permissions

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