Results 1 to 2 of 2
  1. #1
    ApheoXAIO is offline Member
    Join Date
    Jul 2010
    Posts
    2
    Rep Power
    0

    Default OnClick Image for Robot arm interface

    Hi Everyone
    I have made a java program for a robot arm i am developing at uni with a team of guys and basically we have a web interface that sends an ASCI code 1-10 which the robot uses to perform specific functions. At the moment, i have a single image and using the "mouse capture" thing have managed to establish where the user is clicking and can use that to send the right signal.
    I would like it so that when the user clicks on a part of the image for the robot arm the arrow changes color for the same duration as the "ThreadSleep" function goes for, that way, when someone clicks on one of the red arrows, the color changes to green (to show that it is moving) and then dissapears.
    The Relevant code is here:

    -----------------------------------------------------------------------
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.net.*;

    import com.dalsemi.system.*;
    import java.net.*;
    import java.io.*;
    import com.taylec.tini.*;
    import com.taylec.tini.io.*;



    public class RobotInterface extends Applet implements MouseListener
    {
    Socket sock;
    OutputStreamWriter os;
    InputStreamReader is;
    //The string that is displayed each time the interface is redrawn
    String m="current direction: start";
    //The images that are displayed
    Image left,right,forward,backward,stop,instructions;
    //The thread that is run in the ReceiveThread class
    Thread t;


    // This function is called automatically when the java applet is opened
    public void init()
    {
    //add a mouse listener
    addMouseListener(this);
    //set the background colour
    setBackground(Color.white);
    //open the images

    instructions=getImage(getCodeBase(),"instructions. gif"); //added for background

    //call the local initNetwork function to open the socket connection
    initNetwork();
    //call the local openStream class to initialise the streams
    openStream();
    //create an instance of the ReceiveThread class. see the ReceiveThread.java file
    t= new ReceiveThread(this,is,m); // <<<--- AARON EDITED THIS OUT...
    //launch the thread
    t.start();

    }

    //This function is used to draw the interface
    public void paint(Graphics g)
    {
    Font font = new Font("Arial", Font.PLAIN, 28); //Added by Aaron


    g.drawImage(instructions,10,10,675,595,this);
    g.drawString(m,450,18);

    }

    //This function captures mouseclick events and performs the appropriate action
    public void mouseClicked(MouseEvent e)
    {
    int x=0; //The x co-ordinate of the mouse click
    int y=0; //The y co-ordinate of the mouse click

    x=e.getX(); //get the x co-ordinate
    y=e.getY(); //get the y-co-ordinate

    //-------------------------------------------------------------------------------------
    //if the user clicks on the stop image
    if(x>46 && x<116 && y>325 && y<385)
    {
    m="current direction: stop";
    try
    {
    os.write("0");
    os.flush();
    // Thread.sleep(1000);
    // pause(5000);
    }
    catch (Exception ex)
    {
    printError("Transmission Error");
    }

    }

    //-------------------------------------------------------------------------------------

    //if the user clicked on the rail left image
    if(x>494 && x<553 && y>287 && y<322)
    {
    m="current direction: railright";
    try
    {
    os.write("1"); //see protocol definition in report
    os.flush();
    Thread.sleep(3000);
    // pause(5000);
    os.write("0"); //see protocol definition in report
    os.flush();
    }
    catch (Exception ex)
    {
    printError("Transmission Error");
    }

    }
    //-------------------------------------------------------------------------------------
    //if the user clicked on the right image
    if(x>175 && x<225 && y>481 && y<527)
    {
    m="current direction: rail left";
    try
    {
    os.write("2"); //see protocol definition in report
    os.flush();
    Thread.sleep(3000);
    // pause(5000);
    os.write("0"); //see protocol definition in report
    os.flush();
    }
    catch (Exception ex)
    {
    printError("Transmission Error");
    }

    }
    //-------------------------------------------------------------------------------------
    //if the user clicks on the forwards image
    if(x>537 && x<6603 && y>489 && y<518)
    {
    m="current direction: rotate clockwise";
    try
    {
    os.write("3");
    os.flush();
    Thread.sleep(3000);
    // pause(5000);
    os.write("0"); //see protocol definition in report
    os.flush();
    }
    catch (Exception ex)
    {
    printError("Transmission Error");
    }

    }
    //-------------------------------------------------------------------------------------
    //if the user clicks on the backwards image
    if(x>454 && x<513 && y>491 && y<520)
    {
    m="current direction: rotate anti-clockwise";
    try
    {
    os.write("4");
    os.flush();
    Thread.sleep(3000);
    // pause(5000);
    os.write("0"); //see protocol definition in report
    os.flush();
    }
    catch (Exception ex)
    {
    printError("Transmission Error");
    }

    }
    //-------------------------------------------------------------------------------------

    //if the user clicks on the backwards image
    if(x>362 && x<410 && y>122 && y<162)
    {
    m="current direction: elbowup";
    try
    {
    os.write("5");
    os.flush();
    Thread.sleep(2000);
    // pause(5000);
    os.write("0"); //see protocol definition in report
    os.flush();
    }
    catch (Exception ex)
    {
    printError("Transmission Error");
    }

    }
    //-------------------------------------------------------------------------------------
    //if the user clicks on the backwards image
    if(x>283 && x<329 && y>184 && y<231)
    {
    m="current direction: elbowdown";
    try
    {
    os.write("6");
    os.flush();
    Thread.sleep(2000);
    // pause(5000);
    os.write("0"); //see protocol definition in report
    os.flush();
    }
    catch (Exception ex)
    {
    printError("Transmission Error");
    }

    }
    //-------------------------------------------------------------------------------------
    //if the user clicks on the backwards image
    if(x>176 && x<196 && y>42 && y<104)
    {
    m="current direction: wrist up";
    try
    {
    os.write("7");
    os.flush();
    Thread.sleep(1000);
    // pause(5000);
    os.write("0"); //see protocol definition in report
    os.flush();
    }
    catch (Exception ex)
    {
    printError("Transmission Error");
    }

    }
    //-------------------------------------------------------------------------------------
    //if the user clicks on the backwards image
    if(x>211 && x<236 && y>140 && y<200)
    {
    m="current direction: wrist down";
    try
    {
    os.write("8");
    os.flush();
    Thread.sleep(1000);
    // pause(5000);
    os.write("0"); //see protocol definition in report
    os.flush();
    }
    catch (Exception ex)
    {
    printError("Transmission Error");
    }

    }
    //-------------------------------------------------------------------------------------
    //if the user clicks on the backwards image
    if(x>5 && x<117 && y>134 && y<153)
    {
    m="current direction: water on";
    try
    {
    os.write("9");
    os.flush();
    }
    catch (Exception ex)
    {
    printError("Transmission Error");
    }

    }
    //-------------------------------------------------------------------------------------
    //if the user clicks on the backwards image
    if(x>5 && x<117 && y>160 && y<180)
    {
    m="current direction: water off";
    try
    {
    os.write("0");
    os.flush();
    }
    catch (Exception ex)
    {
    printError("Transmission Error");
    }

    }
    //-------------------------------------------------------------------------------------
    //if the user clicks on the backwards image
    if(x>5 && x<117 && y>185 && y<220)
    {
    m="current direction: water burst";
    try
    {
    os.write("9");
    os.flush();
    Thread.sleep(2000);
    // pause(5000);
    os.write("0"); //see protocol definition in report
    os.flush();
    }
    catch (Exception ex)
    {
    printError("Transmission Error");
    }

    }
    //-------------------------------------------------------------------------------------
    //if the user clicks on the backwards image
    if(x>8 && x<117 && y>221 && y<260)
    {
    m="current direction: Spray Left & Right";
    try
    {
    os.write("10");
    os.flush();
    Thread.sleep(2000);
    // pause(5000);
    os.write("11"); //see protocol definition in report
    os.flush();
    }
    catch (Exception ex)
    {
    printError("Transmission Error");
    }

    }
    //-------------------------------------------------------------------------------------

    repaint(); //redraw the interface to update status string
    }

    //ignore these functions. Blank stubs of these functions must be included when
    //using the mouse listener object even if they are not implemented
    public void mousePressed(MouseEvent e)
    {
    }
    public void mouseReleased(MouseEvent e)
    {
    }
    public void mouseEntered(MouseEvent e)
    {
    }
    public void mouseExited(MouseEvent e)
    {
    }

    //This function is used when displaying something
    public void printError(String s)
    {
    m=s;
    repaint();
    }


    public void initNetwork()
    {

    try
    {
    printError("Connecting to 192.168.0.57:1000");
    // printError("Connecting to 130.130.88.129:1000");
    //open a socket connection to the TINI board
    // sock=new Socket("130.130.88.129",1000);
    sock=new Socket("192.168.0.57",1000);
    }
    catch (Exception e)
    {
    printError("Can't open Sockets "+e);
    }
    }

    public void openStream()
    {
    try
    {
    //open an input and output stream for the socket connection
    os=new OutputStreamWriter(sock.getOutputStream());
    is=new InputStreamReader(sock.getInputStream());
    os.write("Start\r\n",0,7);
    os.flush();
    }
    catch (Exception e)
    {
    printError("Can't open stream "+e);
    }
    }

    public static void main(String[] args)
    {

    }

    }

    -----------------------------------------------------------------------------------

    So my question is this
    can i add a small image of a green arrow to go on top of the existing arrow when the user clicks the image, and then disapears after a 2 second delay?

    Thanks for any responses!
    Attached Thumbnails Attached Thumbnails OnClick Image for Robot arm interface-wwb_img1.jpg  

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    can i add a small image of a green arrow to go on top of the existing arrow when the user clicks the image, and then disapears after a 2 second delay?
    Look at using a timer. Have a boolean flag for the paint to test whether to draw the arrow or not. Set the flag and start a Timer to awaken in 2 seconds to clear the flag and call repaint().

    When posting code please enclose the code in code tags.

Similar Threads

  1. help with onclick
    By unplugged in forum Java Applets
    Replies: 4
    Last Post: 12-15-2009, 12:24 AM
  2. Globally disable onclick
    By karnesb in forum New To Java
    Replies: 4
    Last Post: 10-13-2008, 03:45 AM
  3. use of onclick function
    By m4tt in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:03 AM
  4. JButton onClick?
    By Joe2003 in forum AWT / Swing
    Replies: 2
    Last Post: 01-06-2008, 03:04 PM
  5. How to change TXT color Onclick
    By dave700800 in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 01:39 AM

Posting Permissions

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