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

    Default Moving string around

    Hello all.

    Basically i've had to make a java applet where you have a textfield, and when you press enter, it will display the text. Once the text is displayed, when you click somewhere on the applet it will move the text to that position.

    My code is

    Java Code:
    import java.util.*;
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    
    public class assignment2 extends Applet implements ActionListener, MouseListener
    {
    	
    	TextField mytextfield;
    	String strtext="";
    	char thearray[] = strtext.toCharArray();
    	int xcord;
    	int ycord;
    	int x = 50;
    	int y = 50;
    	
    	public void start()
    	{
    		addMouseListener(this);	
    		setSize(500,200);
    	}
    
    	public void init()
    	{
    		setBackground(Color.gray);
    		mytextfield = new TextField(20);
    		add(mytextfield);
    		mytextfield.addActionListener(this);
    		
    	}
    	
    	
    	public void actionPerformed (ActionEvent e)
    	{
    		
    		strtext = e.getActionCommand();
    		
    		repaint();
    		
    		}
    	public void paint (Graphics g)
    	{
    				
    		
    		
    		for (int i=0;i<strtext.length();i++)
    		{
    			char c = strtext.charAt(i);
    			String strtext = "" + c;
    			if ((c == 'R') || (c == 'r'))
    			{
    				g.setColor(Color.green);
    				
    			}
    			else if ((c == 'D') || (c == 'd'))
    			{
    				g.setColor(Color.red);
    			}
    			else 
    			{
    				g.setColor(Color.BLACK);
    			}
    			g.drawString(strtext,x,y);x+=10;
    		
    		}
    	}
    		
    		public void mouseClicked( MouseEvent e)
    		{
    			xcord=e.getX();
    			ycord=e.getY();
    			repaint();
    		}
    		
    		 public void mousePressed(MouseEvent e){}
    		 public void mouseEntered(MouseEvent e){}
    		 public void mouseReleased(MouseEvent e){}
    		 public void mouseExited(MouseEvent e){}
    }
    The problem i'm having is when it displays the string entered, and i click the mouse to move it, rather then the text going where i clicked it just goes right everytime i click it.

    Any help appreciated :)

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

    Default

    I see you update the xcord and ycord when you detect a mouse click, but when do you actually use those values?
    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
    rich123 is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    Ah yes, I realised I wasnt using them, ive just assigned 50 to them variables and it works :), thanks.

Similar Threads

  1. Images not moving Please help!!!
    By reborn567 in forum New To Java
    Replies: 11
    Last Post: 11-09-2010, 06:25 PM
  2. Replies: 9
    Last Post: 04-23-2010, 02:52 AM
  3. Moving objects
    By rsjava24 in forum AWT / Swing
    Replies: 1
    Last Post: 02-26-2010, 11:47 AM
  4. moving a file
    By swati.jyoti in forum New To Java
    Replies: 8
    Last Post: 11-23-2009, 09:44 AM
  5. Moving Box
    By anilanar in forum New To Java
    Replies: 2
    Last Post: 08-30-2009, 01:29 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
  •