Problem in drawing rectangle with mouse drag!!

hii

i am creating an image editor in java.

I have created my main class by extending frame.

Problem is::

how do draw a rectangle as the mouse is being dregged, so that i may get the subimage co-ordinates?

Is it necessary that we may have this option in panel only?

plzz help...itz urgent!!

[336 byte] By [rav_ahja] at [2007-11-26 19:49:36]
# 1
Don't extend frames, it's a bad idea.Extend JPanel or JComponent for this sort of thing.Then just use an inner class which implements MouseListener and MouseInputListener.
itchyscratchya at 2007-7-9 22:38:18 > top of Java-index,Desktop,Core GUI APIs...
# 2
Then shall i extend JPanel and add frame to it?Then how can i view my images....do we have to add it to Panel?plz help..coz i know very little abt JPanel!
rav_ahja at 2007-7-9 22:38:18 > top of Java-index,Desktop,Core GUI APIs...
# 3

Check out my MouseDragOutliner. It should be perfect for what you are doing

You are welcome to use and modify this code, but please do not change the package or take credit for it as your own code

tjacobs.ui.MouseDragOutliner.java

===========================

package tjacobs.ui;

import java.awt.BasicStroke;

import java.awt.Color;

import java.awt.Component;

import java.awt.Container;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Point;

import java.awt.Stroke;

import java.awt.event.*;

import java.util.ArrayList;

import java.util.Iterator;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

/**

* See the public static method addAMouseDragOutliner

*/

public class MouseDragOutliner extends MouseAdapter implements MouseMotionListener {

public static final BasicStroke DASH_STROKE = new BasicStroke(1.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL, 10.0f, new float[] {8, 8}, 0);

public static final boolean RECTANGLE = true;

public static final boolean OVAL = false;

private boolean mUseMove = false;

private Point mStart;

private Point mEnd;

private boolean mShape = RECTANGLE;

private Component mComponent;

private MyRunnable mRunner= new MyRunnable();

private ArrayList mListeners = new ArrayList(1);

public MouseDragOutliner() {

super();

}

public MouseDragOutliner(boolean useMove) {

this();

mUseMove = useMove;

}

public void setShape(boolean b) {

mShape = b;

}

public void mouseDragged(MouseEvent me) {

doMouseDragged(me);

}

public void mousePressed(MouseEvent me) {

mStart = me.getPoint();

}

public void mouseEntered(MouseEvent me) {

mStart = me.getPoint();

}

public void mouseReleased(MouseEvent me) {

Iterator i = mListeners.iterator();

Point end = me.getPoint();

while (i.hasNext()) {

((OutlineListener)i.next()).mouseDragEnded(mStart, end);

}

//mStart = null;

}

public void mouseMoved(MouseEvent me) {

if (mUseMove) {

doMouseDragged(me);

}

}

publicvoid addOutlineListener(OutlineListener ol) {

mListeners.add(ol);

}

public void removeOutlineListener(OutlineListener ol) {

mListeners.remove(ol);

}

private class MyRunnable implements Runnable {

public void run() {

Graphics g = mComponent.getGraphics();

if (g == null) {

return;

}

Graphics2D g2 = (Graphics2D) g;

Stroke s = g2.getStroke();

g2.setStroke(DASH_STROKE);

int x = Math.min(mStart.x, mEnd.x);

int y = Math.min(mStart.y, mEnd.y);

int w = Math.abs(mEnd.x - mStart.x);

int h = Math.abs(mEnd.y - mStart.y);

g2.setXORMode(Color.WHITE);

if (mShape == RECTANGLE) g2.drawRect(x, y, w, h);

else g2.drawOval(x, y, w, h);

g2.setStroke(s);

}

}

public void doMouseDragged(MouseEvent me) {

mEnd = me.getPoint();

if (mStart != null) {

mComponent = me.getComponent();

mComponent.repaint();

SwingUtilities.invokeLater(mRunner);

}

}

public static MouseDragOutliner addAMouseDragOutliner(Component c) {

MouseDragOutliner mdo = new MouseDragOutliner();

c.addMouseListener(mdo);

c.addMouseMotionListener(mdo);

return mdo;

}

public static interface OutlineListener {

public void mouseDragEnded(Point start, Point finish);

}

public static void main(String[] args) {

JFrame f = new JFrame("MouseDragOutliner Test");

Container c = f.getContentPane();

JPanel p = new JPanel();

//p.setBackground(Color.BLACK);

c.add(p);

MouseDragOutliner outliner = addAMouseDragOutliner(p);

outliner.setShape(OVAL);

f.setBounds(200, 200, 400, 400);

f.setVisible(true);

}

}

tjacobs01a at 2007-7-9 22:38:18 > top of Java-index,Desktop,Core GUI APIs...
# 4
Then shall i extend JPanel and add frame to it?No, extend JPanel and add the panel to a frame.
itchyscratchya at 2007-7-9 22:38:18 > top of Java-index,Desktop,Core GUI APIs...