draw line,oval,circle and arrow on the image dynamically
i need to draw line,oval,circle and arrow on the image dynamically.
any one can please help me.
it's urgent.
i need to draw line,oval,circle and arrow on the image dynamically.
any one can please help me.
it's urgent.
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
public class DrawOverImage extends JPanel {
BufferedImage image;
Rectangle rect = new Rectangle();
public DrawOverImage(BufferedImage image) {
this.image = image;
}
protected void setRect(Point p1, Point p2) {
rect.setFrameFromDiagonal(p1, p2);
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int x = (getWidth() - image.getWidth())/2;
int y = (getHeight() - image.getHeight())/2;
g2.drawImage(image, x, y, this);
g2.setPaint(Color.red);
g2.draw(rect);
}
public static void main(String[] args) throws IOException {
String path = "images/cougar.jpg";
BufferedImage image = ImageIO.read(new File(path));
DrawOverImage test = new DrawOverImage(image);
DrawCoordinator coordinator = new DrawCoordinator(test);
test.addMouseListener(coordinator);
test.addMouseMotionListener(coordinator);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
}
class DrawCoordinator extends MouseInputAdapter {
DrawOverImage component;
Point start;
boolean dragging = false;
public DrawCoordinator(DrawOverImage dow) {
component = dow;
}
public void mousePressed(MouseEvent e) {
start = e.getPoint();
dragging = true;
}
public void mouseReleased(MouseEvent e) {
dragging = false;
}
public void mouseDragged(MouseEvent e) {
if(dragging) {
component.setRect(start, e.getPoint());
}
}
}
thanks a lot.
but we need to draw line,arrow,oval and circle.
one more doubt,if suppose image changes like zoom in or zoom out at that time this code work or not?