Results 1 to 4 of 4
  1. #1
    nathancz is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default Cant print character array in applet

    Hey, this is my first post here.

    I am making an applet that allows a user to input a string and my system returns it with any character "n" in red or any character "r" in green.

    However, when I try to use:

    Java Code:
    g.drawChars(stringArray[index], 50, 100, 0, 0);
    it wont compile. I also tried changing the array value to string and using drawString but it didnt work.

    Please can somone have a look to see if I have written my code wrong

    Thanks in advance

    NathanCz





    Here is my full code:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class test1 extends Applet implements ActionListener {
    	static TextField textInput;
    	private static String stringInput;
    	
    	public void init() {
    		textInput = new TextField("Enter text here", 20);
    		add(textInput);
    		textInput.addActionListener(this);
    	}//init
    	
    	public void actionPerformed(ActionEvent event) {
    		stringInput = textInput.getText();
    	}//actionPerformed
    	
    	public void paint(Graphics g) {
    		char[] stringArray = stringInput.toCharArray();
    		
    		for(int index=0; index < stringArray.length; index++) {
    			if (stringArray[index]=='n' || stringArray[index]=='N') {
    				g.setColor(Color.red);
    				g.drawChars(stringArray[index], 50, 100, 0, 0);
    			}
    			else if (stringArray[index]=='r' || stringArray[index]=='R') {
    				g.setColor(Color.green);
    				g.drawChars(stringArray[index], 50, 100, 0, 0);
    			}
    			else {
    				g.drawChars(stringArray[index], 50, 100, 0, 0);
    			}
    		}//for-loop
    		
    	}//paint
    }

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Out of curiosity, why do so many posters here try to print Strings and Images using the Graphics object, instead of using Label components? It boggles the mind.

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

    Default

    Im having the same problem, having tried various things

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

    Default

    Quote Originally Posted by Sarah24 View Post
    Im having the same problem, having tried various things
    Please don't hijack another's thread. I suggest you start a new thread and actually show us what you've tried and how it doesn't work.

Similar Threads

  1. AWT Applet for print
    By mamarget in forum AWT / Swing
    Replies: 0
    Last Post: 01-21-2011, 05:14 AM
  2. Using a character array with html
    By ishdevil in forum New To Java
    Replies: 0
    Last Post: 02-10-2010, 01:01 PM
  3. Replies: 2
    Last Post: 02-24-2009, 04:32 PM
  4. comparing array using character
    By Anseki in forum New To Java
    Replies: 7
    Last Post: 10-03-2008, 07:28 PM
  5. print webpage through applet
    By shakti singh in forum Java Applets
    Replies: 1
    Last Post: 07-22-2008, 07:17 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
  •