Zolf
asked on
get the dimensions on the image
hello there,
i have an image which has many objects(icons) on it.i want to create hotspots for each of them how do i do that.before that i need to get the dimensions of those objects how can i get those too
You could use an image map like the one your were discussing earlier. Getting dimensions of embedded parts of an image would be tricky and imprecise to do programmatically
ASKER
yes,but how do i get the object size.i mean the dimension of a particular object on the image.
If you need to do it programmatically, try a search on
java edge detection
java edge detection
ASKER
ok,can you tell me how can i implement a feature that when i click my mouse on anywhere on the image and drag, it should give me the dimension
if you add a transparent component to the component displaying your image you can add a MouseListener to it to get when its clicked on.
using a null layout allows you to explicitly specify the position and location of the area to be clicked on.
and you can use the getSize() method to determine the size of that component
using a null layout allows you to explicitly specify the position and location of the area to be clicked on.
and you can use the getSize() method to determine the size of that component
ASKER
that was good example but instead of drawing lines with the mouse i want to get the where on the image i am clicking and give me the location
You can just add a MouseListener and it will give you the click coordinates
ASKER
can you help me to get where on the image i am clicking and return the location.this is my code below.please help
import javax.swing.*;
import java.awt.*;
public class ImageFrame extends JFrame
{
public ImageFrame()
{
super("My Frame");
Container c = getContentPane();
c.setLayout(new BorderLayout());
c.add(new JLabel(new ImageIcon("images/7224.jpg
pack();
setLocation(100, 100);
setVisible(true);
}
public static void main(String [] args)
{
new ImageFrame();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks guys that is sort of thing i am trying to achieve,but i shouls also be able to drag the mouse to get the bounds.i mean when i clcik and drag the mouse it should give me the x,y,height and width
The RubberBand code i posted earlier does that
ASKER
it only draws figures line and rectange.but does not get the coordinates x,y,height and width
It only takes a small alteration to make it do what you want:
/*
<applet codebase="." code="RubberBand.class" width="300" height="300">
</applet>
*/
/*
In this applet, the user can draw straight lines by clicking and dragging
and can draw filled rectangles by right-clicking and dragging. Shift-
clicking will clear the applet. The picture is stored in an off-screen
image, so that the screen can be redrawn as necessary. A "rubber-band
cursor" is implemented by drawing the shape the user is sketching
on top of the stored image. Since Swing already implements double buffering,
this applet is, in effect, using triple buffering.
It is assumed that this applet will not change size after it is created.
The off-screen image is created in the applet's init() method and
will not be resized if the applet's size is changed.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RubberBand extends JApplet {
Shape lastShape;
Image offScreenImage; // An in-memory image that is used to hold a
// copy of the picture that is displayed
// on the applet (not including the figure
// that the user is currently drawing, if any).
public void init() {
// The applet's init() method creates a drawing panel to be
// used as the drawing surface of the applet. It also
// creates the off-screen image and fills it with a white
// rectangle with a black border.
setContentPane( new Display() );
offScreenImage = createImage(getSize().widt h, getSize().height);
Graphics g = offScreenImage.getGraphics ();
g.setColor(Color.white);
g.fillRect(0,0,getSize().w idth,getSi ze().heigh t);
g.setColor(Color.black);
g.drawRect(0,0,getSize().w idth-1,get Size().hei ght-1);
g.dispose();
}
class Display extends JPanel
implements MouseListener, MouseMotionListener{
// This nested class represents the drawing surface of the applet.
// It implements all the programming necessary to let the user
// draw on the applet.
boolean heardMouse = false; // Set to true the first time the user clicks on
// the applet. Until that time, some instructions
// are drawn by the paintComponent() method.
/* Some variables used during dragging. */
boolean dragging; // This is set to true when a drag begins, and to false
// when it ends. The value is checked by mouseReleased()
// and mouseDragged().
boolean drawingLine; // The user can draw lines or rects. This variable is true if
// the user is drawing a line, and false if the user is drawing
// a rectangle.
int startx, starty; // The location of the mouse when the dragging started.
int mousex, mousey; // The location of the mouse during dragging.
Display() {
// Constructor sets the background color to white and
// set up this panel to listen for mouse events on itself.
setBackground(Color.white) ;
addMouseListener(this);
addMouseMotionListener(thi s);
}
public void paintComponent(Graphics g) {
// Copy the off-screen image to the screen. If there
// have not yet been any user actions, show some instructions.
// If the user is dragging, draw the figure that the user
// is sketching on top of the permanent picture from
// the off-screen image. (Note: There is no need to
// call super.paintComponent(g) since this paintComponent
// method fills in the entire component.)
g.drawImage(offScreenImage ,0,0,null) ;
if (heardMouse == false) {
g.setColor(Color.red);
g.drawString("Click and drag to draw lines.", 10, 15);
g.drawString("Right-click and drag to draw rects.", 10, 30);
g.drawString("Shift-click to clear.", 10, 45);
}
else if (dragging) {
drawFigure(g);
}
}
void drawFigure(Graphics g) {
// This is called during dragging to draw the line or rect
// between the starting position of the mouse and the current
// position. The figure is drawn in the graphics context g.
// The variables startx, starty, mousex, mousey, and drawingLine
// contain the information needed to draw the figure.
if (drawingLine) {
// Draw a red line, but only if the two points are different.
if (startx != mousex || starty != mousey) {
g.setColor(Color.red);
g.drawLine(startx, starty, mousex, mousey);
}
}
else {
// Draw a blue rect, but only if it is non-empty.
if (startx != mousex && starty != mousey) {
int x, y; // Top left corner of the rectangle.
int w, h; // Width and height of the rectangle.
// x,y,w,h must be computed from the coordinates
// of the two corner points.
if (mousex > startx) {
x = startx;
w = mousex - startx;
}
else {
x = mousex;
w = startx - mousex;
}
if (mousey > starty) {
y = starty;
h = mousey - starty;
}
else {
y = mousey;
h = starty - mousey;
}
g.setColor(Color.blue);
g.fillRect(x, y, w, h);
g.setColor(Color.black);
g.drawRect(x, y, w-1, h-1);
lastShape= new Rectangle(x, y, w-1, h-1);
}
}
} // end drawFigure()
public void mousePressed(MouseEvent evt) {
// Respond when the user presses a mouse button.
if (dragging) // If already dragging, don't do anything.
return; // (This can happen if the user presses two mouse buttons.
if (heardMouse == false) {
// Erase the instructions, if this is the first mause click.
heardMouse = true;
repaint();
}
if (evt.isShiftDown()) {
// If the shift key is down, just erase the applet.
Graphics g = offScreenImage.getGraphics ();
g.setColor(Color.white);
g.fillRect(0,0,getSize().w idth,getSi ze().heigh t);
g.setColor(Color.black);
g.drawRect(0,0,getSize().w idth-1,get Size().hei ght-1);
g.dispose();
repaint();
return;
}
startx = mousex = evt.getX(); // Save coords of mouse position.
starty = mousey = evt.getY();
drawingLine = (evt.isMetaDown() == false); // Check if it's a right-click.
dragging = true; // Start dragging.
} // end mousePressed()
public void mouseReleased(MouseEvent evt) {
// End the dragging operation, if one is in progress. Draw
// the final figure, if any onto the off-screen canvas, so
// it becomes a permanent part of the image.
if (dragging) {
Graphics g = offScreenImage.getGraphics ();
drawFigure(g);
System.out.println(lastSha pe);
g.dispose();
dragging = false;
repaint();
}
}
public void mouseDragged(MouseEvent evt) {
// If a dragging operation is in progress, get the new
// values for mousex and mousey, and repaint.
if (dragging) {
mousex = evt.getX();
mousey = evt.getY();
repaint();
}
}
public void mouseClicked(MouseEvent evt) { } // Empty methods, required by
public void mouseEntered(MouseEvent evt) { } // the MouseEvent and
public void mouseExited(MouseEvent evt) { } // MouseListener intefaces.
public void mouseMoved(MouseEvent evt) { }
} // end nested class Display
} // end class RubberBand
/*
<applet codebase="." code="RubberBand.class" width="300" height="300">
</applet>
*/
/*
In this applet, the user can draw straight lines by clicking and dragging
and can draw filled rectangles by right-clicking and dragging. Shift-
clicking will clear the applet. The picture is stored in an off-screen
image, so that the screen can be redrawn as necessary. A "rubber-band
cursor" is implemented by drawing the shape the user is sketching
on top of the stored image. Since Swing already implements double buffering,
this applet is, in effect, using triple buffering.
It is assumed that this applet will not change size after it is created.
The off-screen image is created in the applet's init() method and
will not be resized if the applet's size is changed.
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RubberBand extends JApplet {
Shape lastShape;
Image offScreenImage; // An in-memory image that is used to hold a
// copy of the picture that is displayed
// on the applet (not including the figure
// that the user is currently drawing, if any).
public void init() {
// The applet's init() method creates a drawing panel to be
// used as the drawing surface of the applet. It also
// creates the off-screen image and fills it with a white
// rectangle with a black border.
setContentPane( new Display() );
offScreenImage = createImage(getSize().widt
Graphics g = offScreenImage.getGraphics
g.setColor(Color.white);
g.fillRect(0,0,getSize().w
g.setColor(Color.black);
g.drawRect(0,0,getSize().w
g.dispose();
}
class Display extends JPanel
implements MouseListener, MouseMotionListener{
// This nested class represents the drawing surface of the applet.
// It implements all the programming necessary to let the user
// draw on the applet.
boolean heardMouse = false; // Set to true the first time the user clicks on
// the applet. Until that time, some instructions
// are drawn by the paintComponent() method.
/* Some variables used during dragging. */
boolean dragging; // This is set to true when a drag begins, and to false
// when it ends. The value is checked by mouseReleased()
// and mouseDragged().
boolean drawingLine; // The user can draw lines or rects. This variable is true if
// the user is drawing a line, and false if the user is drawing
// a rectangle.
int startx, starty; // The location of the mouse when the dragging started.
int mousex, mousey; // The location of the mouse during dragging.
Display() {
// Constructor sets the background color to white and
// set up this panel to listen for mouse events on itself.
setBackground(Color.white)
addMouseListener(this);
addMouseMotionListener(thi
}
public void paintComponent(Graphics g) {
// Copy the off-screen image to the screen. If there
// have not yet been any user actions, show some instructions.
// If the user is dragging, draw the figure that the user
// is sketching on top of the permanent picture from
// the off-screen image. (Note: There is no need to
// call super.paintComponent(g) since this paintComponent
// method fills in the entire component.)
g.drawImage(offScreenImage
if (heardMouse == false) {
g.setColor(Color.red);
g.drawString("Click and drag to draw lines.", 10, 15);
g.drawString("Right-click and drag to draw rects.", 10, 30);
g.drawString("Shift-click to clear.", 10, 45);
}
else if (dragging) {
drawFigure(g);
}
}
void drawFigure(Graphics g) {
// This is called during dragging to draw the line or rect
// between the starting position of the mouse and the current
// position. The figure is drawn in the graphics context g.
// The variables startx, starty, mousex, mousey, and drawingLine
// contain the information needed to draw the figure.
if (drawingLine) {
// Draw a red line, but only if the two points are different.
if (startx != mousex || starty != mousey) {
g.setColor(Color.red);
g.drawLine(startx, starty, mousex, mousey);
}
}
else {
// Draw a blue rect, but only if it is non-empty.
if (startx != mousex && starty != mousey) {
int x, y; // Top left corner of the rectangle.
int w, h; // Width and height of the rectangle.
// x,y,w,h must be computed from the coordinates
// of the two corner points.
if (mousex > startx) {
x = startx;
w = mousex - startx;
}
else {
x = mousex;
w = startx - mousex;
}
if (mousey > starty) {
y = starty;
h = mousey - starty;
}
else {
y = mousey;
h = starty - mousey;
}
g.setColor(Color.blue);
g.fillRect(x, y, w, h);
g.setColor(Color.black);
g.drawRect(x, y, w-1, h-1);
lastShape= new Rectangle(x, y, w-1, h-1);
}
}
} // end drawFigure()
public void mousePressed(MouseEvent evt) {
// Respond when the user presses a mouse button.
if (dragging) // If already dragging, don't do anything.
return; // (This can happen if the user presses two mouse buttons.
if (heardMouse == false) {
// Erase the instructions, if this is the first mause click.
heardMouse = true;
repaint();
}
if (evt.isShiftDown()) {
// If the shift key is down, just erase the applet.
Graphics g = offScreenImage.getGraphics
g.setColor(Color.white);
g.fillRect(0,0,getSize().w
g.setColor(Color.black);
g.drawRect(0,0,getSize().w
g.dispose();
repaint();
return;
}
startx = mousex = evt.getX(); // Save coords of mouse position.
starty = mousey = evt.getY();
drawingLine = (evt.isMetaDown() == false); // Check if it's a right-click.
dragging = true; // Start dragging.
} // end mousePressed()
public void mouseReleased(MouseEvent evt) {
// End the dragging operation, if one is in progress. Draw
// the final figure, if any onto the off-screen canvas, so
// it becomes a permanent part of the image.
if (dragging) {
Graphics g = offScreenImage.getGraphics
drawFigure(g);
System.out.println(lastSha
g.dispose();
dragging = false;
repaint();
}
}
public void mouseDragged(MouseEvent evt) {
// If a dragging operation is in progress, get the new
// values for mousex and mousey, and repaint.
if (dragging) {
mousex = evt.getX();
mousey = evt.getY();
repaint();
}
}
public void mouseClicked(MouseEvent evt) { } // Empty methods, required by
public void mouseEntered(MouseEvent evt) { } // the MouseEvent and
public void mouseExited(MouseEvent evt) { } // MouseListener intefaces.
public void mouseMoved(MouseEvent evt) { }
} // end nested class Display
} // end class RubberBand
ASKER
did you make any changes to the code above,because when i run the above code i dont see any changes to what it was before.
Look at the command line
ASKER
i am running the applet from a html file
ASKER
how do i run from commond line.
by the way i managed to get the coordinates for dragging,like this.but it would be nice to show a outline showing the mouse is being dragged.can you guide me how to get that thing
addMouseMotionListener(new
public void mouseDragged(MouseEvent e) {
System.out.println("Dragge
}
});
Run it with appletviewer
appletviewer RubberBand.java
or look at the applet console
appletviewer RubberBand.java
or look at the applet console
ASKER
how can i draw a outline to show the mouse dragging
Change
>>
g.setColor(Color.blue);
g.fillRect(x, y, w, h);
g.setColor(Color.black);
g.drawRect(x, y, w-1, h-1);
>>
to
//g.setColor(Color.blue);
//g.fillRect(x, y, w, h);
g.setColor(Color.black);
g.drawRect(x, y, w-1, h-1);
>>
g.setColor(Color.blue);
g.fillRect(x, y, w, h);
g.setColor(Color.black);
g.drawRect(x, y, w-1, h-1);
>>
to
//g.setColor(Color.blue);
//g.fillRect(x, y, w, h);
g.setColor(Color.black);
g.drawRect(x, y, w-1, h-1);
ASKER
no i mean in my code above which i am working on
Before we go any further, i'd be grateful for answer to the question i asked in your other question
ASKER
please help
I had already suggested adding a MouseListener, CEHJ was just copying my suggestion.
Plus adding it to the frame will not work as it will give the coordinates of the click on the frame, and *not* on the image which is what u r interested in.
Plus adding it to the frame will not work as it will give the coordinates of the click on the frame, and *not* on the image which is what u r interested in.
:-)
:-)