need help about dragging a rectangle on a image

hello

i need to draw a rectangle on image .this is my code does this code support my requirement .

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.MouseInputAdapter;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

public class RectangleDrag extends JPanel {

Rectangle tt=new Rectangle();

int sx=0,sy=0,dx=0,dy=0;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d=(Graphics2D)g;

g2d.draw(tt);

}

private MouseInputAdapter mia = new MouseInputAdapter() {

public void mousePressed(MouseEvent e) {

sx=e.getX(); //start x

sy=e.getY(); // start y

}

public void mouseDragged(MouseEvent e) {

dx=e.getX(); //end x

dy=e.getY(); // end y

int width=Math.abs(dx-sx);

int height=Math.abs(dy-sy);

int side=Math.max(width, height);

int side1=Math.min(width,height);

int x, y;

if(sx<=dx) {

x=sx;

} else {

x=sx-side;

}

if(sy<=dy) {

y=sy;

} else {

y=sy-side;

}

tt.setRect(x,y,side,side1);

repaint();

}

};

private JPanel getContent(BufferedImage image) {

JPanel panel = new JPanel(new GridLayout(1,0));

panel.add(new JLabel(new ImageIcon(image)));

panel.add(new JLabel(new ImageIcon(getImage(image))));

return panel;

public static void main(String[] args) {

RectangleDrag test = new RectangleDrag();

test.addMouseListener(test.mia);

test.addMouseMotionListener(test.mia);

String path = "C:/Documents and Settings/v.kirankumar/My Documents/jpg/32370.jpg";

BufferedImage image = ImageIO.read(new File(path));

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test.getContent(image));

//f.pack();

//f.getContentPane().add(test);

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

[2136 byte] By [mahesh.ba] at [2007-11-27 10:28:07]
# 1

then what is the problem?

student@sunDNa at 2007-7-28 17:49:51 > top of Java-index,Java Essentials,Java Programming...
# 2

compile this code getting error

mahesh.ba at 2007-7-28 17:49:51 > top of Java-index,Java Essentials,Java Programming...
# 3

Then what is the error and indicate what line it occurs on. Don't expect us to do it for you.

Also, when posting code, highlight it and click code button. This will retain formatting.

floundera at 2007-7-28 17:49:51 > top of Java-index,Java Essentials,Java Programming...
# 4

> ImageIcon(getImage(image))));

//where is the getImage method in the code?

student@sunDNa at 2007-7-28 17:49:51 > top of Java-index,Java Essentials,Java Programming...
# 5

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.MouseInputAdapter;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

public class RectangleDrag extends JPanel {

Rectangle tt=new Rectangle();

int sx=0,sy=0,dx=0,dy=0;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d=(Graphics2D)g;

g2d.draw(tt);

}

private MouseInputAdapter mia = new MouseInputAdapter() {

public void mousePressed(MouseEvent e) {

sx=e.getX(); //start x

sy=e.getY(); // start y

}

public void mouseDragged(MouseEvent e) {

dx=e.getX(); //end x

dy=e.getY(); // end y

int width=Math.abs(dx-sx);

int height=Math.abs(dy-sy);

int side=Math.max(width, height);

int side1=Math.min(width,height);

int x, y;

if(sx<=dx) {

x=sx;

} else {

x=sx-side;

}

if(sy<=dy) {

y=sy;

} else {

y=sy-side;

}

tt.setRect(x,y,side,side1);

repaint();

}

};

private JPanel getContent(BufferedImage image) {

JPanel panel = new JPanel(new GridLayout(1,0));

panel.add(new JLabel(new ImageIcon(image)));

panel.add(new JLabel(new ImageIcon(getImage(image))));

return panel;

public static void main(String[] args) {

RectangleDrag test = new RectangleDrag();

test.addMouseListener(test.mia);

test.addMouseMotionListener(test.mia);

String path = "C:/Documents and Settings/v.kirankumar/My Documents/jpg/32370.jpg";

BufferedImage image = ImageIO.read(new File(path));

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test.getContent(image));

// f.pack();

// f.getContentPane().add(test);

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

mahesh.ba at 2007-7-28 17:49:51 > top of Java-index,Java Essentials,Java Programming...
# 6

No error message and still the code is unformatted.

floundera at 2007-7-28 17:49:51 > top of Java-index,Java Essentials,Java Programming...
# 7

this is the modified code here amgetting an image but i couldnt draw rectangle exactly on the image.

getImage(image); isnt making any difference

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.MouseInputAdapter;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

public class RectangleDrag extends JPanel {

Rectangle tt=new Rectangle();

int sx=0,sy=0,dx=0,dy=0;

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d=(Graphics2D)g;

g2d.draw(tt);

}

private MouseInputAdapter mia = new MouseInputAdapter() {

public void mousePressed(MouseEvent e) {

sx=e.getX(); //start x

sy=e.getY(); // start y

}

public void mouseDragged(MouseEvent e) {

dx=e.getX(); //end x

dy=e.getY(); // end y

int width=Math.abs(dx-sx);

int height=Math.abs(dy-sy);

int side=Math.max(width, height);

int side1=Math.min(width,height);

int x, y;

if(sx<=dx) {

x=sx;

} else {

x=sx-side;

}

if(sy<=dy) {

y=sy;

} else {

y=sy-side;

}

tt.setRect(x,y,side,side1);

repaint();

}

};

private JPanel getContent(BufferedImage image) {

JPanel panel = new JPanel(new GridLayout(1,0));

panel.add(new JLabel(new ImageIcon(image)));

return panel;

public static void main(String[] args) {

RectangleDrag test = new RectangleDrag();

test.addMouseListener(test.mia);

test.addMouseMotionListener(test.mia);

String path = "C:/Documents and Settings/v.kirankumar/My Documents/jpg/32370.jpg";

BufferedImage image = ImageIO.read(new File(path));

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test.getContent(image));

f.pack();

f.getContentPane().add(test);

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

mahesh.ba at 2007-7-28 17:49:51 > top of Java-index,Java Essentials,Java Programming...
# 8

You obviously don't want any help do you?

floundera at 2007-7-28 17:49:51 > top of Java-index,Java Essentials,Java Programming...
# 9

this is the area where we are having some problem we could able to get an image on a panel but couldnt draw the rectangle on the image it is been drawn away from the image rather than on the image.

private JPanel getContent(BufferedImage image) {

JPanel panel = new JPanel(new GridLayout(1,0));

panel.add(new JLabel(new ImageIcon(image)));

return panel;

public static void main(String[] args) {

RectangleDrag test = new RectangleDrag();

test.addMouseListener(test.mia);

test.addMouseMotionListener(test.mia);

String path = "C:/Documents and Settings/v.kirankumar/My Documents/jpg/32370.jpg";

BufferedImage image = ImageIO.read(new File(path));

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test.getContent(image));

f.pack();

f.getContentPane().add(test);

mahesh.ba at 2007-7-28 17:49:51 > top of Java-index,Java Essentials,Java Programming...