Solved

Applet to get bounding box / coordinates when given an image

Posted on 2004-08-01
3
371 Views
Last Modified: 2010-03-31
I am a HORRIBLE java programmer.  I have an app in php that will place some text on an image.   I want to add a user friendly applet that will allow me to load an image into the applet (jpg format, largest res 700x700) and click and drag to create a bounding box.   I would like the java applet to return (how? I'm totally lost in java... maybe by populating html form fields elsewhere on the page?) the four points returned by the bounding box.   I've seen a few of these apps out there in the world, but nothing that I would like to tackle as a first real java app :)    

Code, Advice a good tutorial (I'm a c/delphi/php guy) would be great :)

Thanks
0
Comment
Question by:hibbidiji
3 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 500 total points
ID: 11691766
With this (rubbish) HTML page:

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

<html>
  <head>
    <title>HTML Test Page</title>
  </head>
  <body>
    <applet codebase="." code="Test.class" id="applet" width="700" height="700" hspace="0" vspace="0" align="middle" mayscript>
    </applet><br/>
    Background Image (MUST be on the same server as the applet) : <input type="text" id="img" size="80" value="http://www.google.com/images/logo.gif"><input type="button" value="Set" onClick="document.getElementById( 'applet' ).setImage( document.getElementById( 'img' ).value )">
    <form id="data" action="whatever.jsp">
      X: <input type="text" id="x"/><br/>
      Y: <input type="text" id="y"/><br/>
      W: <input type="text" id="w"/><br/>
      H: <input type="text" id="h"/><br/>
      <input type="submit" value="POST"/>
    </form>
  </body>
</html>

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

And this java applet:

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

import java.applet.* ;
import javax.swing.* ;
import java.awt.* ;
import java.awt.event.* ;
import java.lang.reflect.* ;

public class Test extends JApplet
{
  class Mouse extends MouseAdapter
  {
    public void mousePressed( MouseEvent e )
    {
      bounding = new Rectangle() ;
      bounding.x = e.getX() ;
      bounding.y = e.getY() ;
      bounding.width = 0 ;
      bounding.height = 0 ;
    }
  }

  class MouseMove extends MouseMotionAdapter
  {
    public void mouseDragged( MouseEvent e )
    {
      if( bounding != null )
      {
        bounding.width = e.getX() - bounding.x ;
        bounding.height = e.getY() - bounding.y ;
        updateForm() ;
        repaint() ;
      }
    }
  }

  Rectangle bounding = null ;
  ImageIcon image = null ;

  public Test()
  {
    this.addMouseListener( new Mouse() );
    this.addMouseMotionListener( new MouseMove() );
  }

  public void setImage( String name )
  {
    image = new ImageIcon( name ) ;
  }

  public void update( Graphics g )
  {
    paint( g ) ;
  }

  public void paint( Graphics g )
  {
    if( image != null )
      g.drawImage( image.getImage(), 0, 0, this ) ;
    else
    {
      g.setColor( Color.yellow ) ;
      g.fillRect( 0, 0, getWidth(), getHeight() );
    }
    if( bounding != null )
    {
      g.setXORMode( Color.white );
      g.drawRect( bounding.x, bounding.y, bounding.width, bounding.height );
    }
  }

  // Cool reflection based javascript calling method
  // Taken from http://www.rgagnon.com/javadetails/java-0172.html
  private String callBrowser( String jsCmd )
  {
    String jsResult = null ;
    boolean success = false ;
    try
    {
      Method getw = null, eval = null ;
      Object jswin = null ;

      /* does it in IE too */
      Class c = Class.forName( "netscape.javascript.JSObject" ) ;

      Method ms[] = c.getMethods() ;
      for( int i = 0 ; i < ms.length ; i++ )
      {
        if( ms[ i ].getName().equals( "getWindow" ) )
          getw = ms[ i ] ;
        else if( ms[ i ].getName().equals( "eval" ) )
          eval = ms[ i ] ;
      }

      Object a[] = new Object[ 1 ] ;

      a[ 0 ] = this ; /* this is the applet */
      jswin = getw.invoke( c, a ) ; /* this yields the JSObject */

      a[ 0 ] = jsCmd ;
      Object result = eval.invoke( jswin, a ) ;

      if( result instanceof String )
        jsResult = (String)result ;
      else
        jsResult = result.toString() ;

      success = true ;
    }
    catch( InvocationTargetException ite )
    {
      jsResult = "" + ite.getTargetException() ;
    }
    catch( Exception e )
    {
      jsResult = "" + e ;
    }

    if(success)
      return jsResult ;
    else
    {
      System.out.println( "eval failed with error " + jsResult ) ;
      return null ;
    }
  }

  private void updateForm()
  {
    if( bounding != null )
    {
      callBrowser( "document.getElementById( 'x' ).value = '" + bounding.x + "' ;" ) ;
      callBrowser( "document.getElementById( 'y' ).value = '" + bounding.y + "' ;" ) ;
      callBrowser( "document.getElementById( 'w' ).value = '" + bounding.width + "' ;" ) ;
      callBrowser( "document.getElementById( 'h' ).value = '" + bounding.height + "' ;" ) ;
    }
  }
}

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

It sort of does what you want...  doesn't it?

Hope it helps you get off to a good start anyway :-)

Tim
0
 
LVL 3

Expert Comment

by:JohnnyAffa
ID: 11692018
hibbi,

"I want to add a user friendly applet that will allow me to load an image into the applet "

   Q) are you allowing the user to select an image from a file or will you be supplying the image/location (hardcoded)? either is easy to implement

"(jpg format, largest res 700x700) and click and drag to create a bounding box. "

    Q) so all you want is to display the coordinates and the size of the bounding box in the webpage?  it might (actually is ) easier to display it within the applet using a textfield object.  also, do you want the "bounding box" borders displayed so the user can see the dimensions while moving the mouse?
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 11711551
I made this code do just what I wanted. thanks!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now