Solved

Applet to get bounding box / coordinates when given an image

Posted on 2004-08-01
3
391 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
starter POM and spring-boot-starter,  spring-boot-web 2 53
eclipse java  build path 6 56
javap not working 8 43
Java exception bubble up 2 17
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …

816 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

8 Experts available now in Live!

Get 1:1 Help Now