Glasspane Woes
To: comp.lang.java.gui
Trying to get a glasspane to intercept mouseDragged events and let the
rootpane's GUI still function. Tried doing something like setBounds on
the glasspane but no luck. Also tried doing a glasspane.setVisible on
the mouseEnter and mouseExit events and still no luck. Perhaps it
won't detect the mouse re-entry bc the glasspane is invisible? In
which case I'd need to make a listener on my JInternalFrame too? I'll
post some code in case its just a stupid mistake. And advice is
appreciated in direct ratio to how good it is ;-)
Here we go:
(It's a Digital Image Processor - DIP)
[START CODE]
package dip.listeners;
import dip.jcomponents.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
public class DIPMouseListener extends MouseAdapter implements
MouseMotionListener
{
DIPGlassPane glassPane;
Point currentLocation;
Point mouseDraggedFrom;
Point mouseDraggedTo;
Point mouseReleasedAt;
Point mouseClickedAt;
Point mousePressedAt;
public DIPMouseListener(DIPGlassPane glassPane)
{
this.glassPane=glassPane;
}
public void mousePressed(MouseEvent me)
{
mousePressedAt=new Point(me.getX(),me.getY());
//System.out.println("Mouse pressed at point "+me.getX()
+","+me.getY()+".");
glassPane.updateRect(me.getX(),me.getY(),0,0);
}
public void mouseEntered(MouseEvent me)
{
//System.out.println("Mouse entered at point "+me.getX()
+","+me.getY()+".");
glassPane.setVisible(true);
}
public void mouseExited(MouseEvent me)
{
//System.out.println("Mouse exited at point "+me.getX()+","+me.getY()
+".");
glassPane.setVisible(false);
}
public void mouseDragged(MouseEvent me)
{
//mouseDraggedFrom=new Point(me.getX(),me.getY());
//System.out.println("Mouse dragged starting at point "+me.getX()
+","+me.getY()+".");
glassPane.updateRect((int)mousePressedAt.getX(),
(int)mousePressedAt.getY(),me.getX()-
(int)mousePressedAt.getX(),me.getY()-(int)mousePressedAt.getY());
}
public void mouseReleased(MouseEvent me)
{
mouseReleasedAt=new Point(me.getX(),me.getY());
mousePressedAt=null;
//System.out.println("Mouse released at point "+me.getX()
+","+me.getY()+".");
}
public void mouseMoved(MouseEvent me)
{
currentLocation=new Point(me.getX(),me.getY());
//System.out.println("Mouse moved to point "+me.getX()+","+me.getY()
+".");
}
public Point getDraggedFromPoint()
{
return mouseDraggedFrom;
}
public Point getDraggedToPoint()
{
return mouseDraggedTo;
}
public Point getCurrentLocation()
{
return currentLocation;
}
}
package dip.jcomponents;
import dip.listeners.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DIPGlassPane extends JComponent
{
DIPMouseListener mouseListener=new DIPMouseListener(this);
Rectangle rectangle;
public DIPGlassPane()
{
this.addMouseMotionListener(mouseListener);
this.addMouseListener(mouseListener);
this.rectangle=new Rectangle();
}
public void clearRect()
{
this.rectangle=new Rectangle(this.getWidth(),this.getHeight(),0,0);
}
public void updateRect(Rectangle r)
{
this.rectangle=r;
System.out.println("updateRect(Rectangle r) says Rectangle's current
data: "+rectangle.toString());
repaint();
}
public void updateRect(int width, int height)
{
int x=(int)rectangle.getX();
int y=(int)rectangle.getY();
//Make the width and height positive, if necessary.
if (width < 0)
{
width=Math.abs(width);
x=x-width+1;
if (x < 0)
{
width += x;
x = 0;
}
}
if (height < 0)
{
height = 0 - height;
y = y - height + 1;
if (y < 0)
{
height += y;
y = 0;
}
}
rectangle.setBounds(x,y, width, height);
//System.out.println("updateRect(int width, int height) says
Rectangle's current data: "+rectangle.toString());
repaint();
}
public void updateRect(int x, int y, int width, int height)
{
//Make the width and height positive, if necessary.
if (width < 0) {
width = 0 - width;
x = x - width + 1;
if (x < 0) {
width += x;
x = 0;
}
}
if (height < 0) {
height = 0 - height;
y = y - height + 1;
if (y < 0) {
height += y;
y = 0;
}
}
//The rectangle shouldn't extend past the drawing area.
/*if ((x + width) > ) {
width = compWidth - x;
}
if ((y + height) > compHeight) {
height = compHeight - y;
}*/
rectangle.setBounds(x,y, width, height);
//System.out.println("updateRect(int x, int y, int width, int
height) says Rectangle's current data: "+rectangle.toString());
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.RED);
g.drawRect((int)rectangle.getX(),(int)rectangle.getY(),
(int)rectangle.getWidth()-1, (int)rectangle.getHeight()-1);
}
public Rectangle getRectangle()
{
return rectangle;
}
}
package dip.jcomponents;
import dip.actions.*;
import dip.*;
import dip.stats.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class ImageJInternalFrame extends JInternalFrame
{
private static final int TITLE_BAR_OFFSET=30;
private static final int MENU_BAR_OFFSET=26;
private static final int WIDTH_OFFSET=10;
BufferedImage image=null;
LightweightCanvas canvas=null;
DIPGlassPane glassPane=null;
JMenuBar menuBar=null;
JMenu file=null;
JMenu imageMenu=null;
JMenu scale=null;
JMenu filters=null;
JMenu sobel=null;
JMenuItem save=null;
JMenuItem exit=null;
JMenuItem duplicate=null;
JMenuItem invert=null;
JMenuItem desaturate=null;
JMenuItem scaleNoInterpolation=null;
JMenuItem scaleLinearInterpolation=null;
JMenuItem histogram=null;
JMenuItem average=null;
JMenuItem laplace=null;
JMenuItem sobelHorizontal=null;
JMenuItem sobelVertical=null;
JMenuItem sobelBidirectional=null;
JMenuItem sobelThreshold=null;
/* Future Impl
private JMenuItem compareDifference;
private JMenuItem compareAddition;
private JMenuItem compareBinaryAbsolute;
private JMenuItem compareBinaryThreshold;
*/
public ImageJInternalFrame(BufferedImage image, String title)
{
super(title, true, true, true, true);
setSize(image.getWidth()+WIDTH_OFFSET,image.getHeight()
+TITLE_BAR_OFFSET+MENU_BAR_OFFSET);
initJComponents(image);
}
private void initJComponents(BufferedImage image)
{
initCanvas(image);
initJMenuBar();
initGlassPane(image);
}
private void initGlassPane(BufferedImage image)
{
glassPane=new DIPGlassPane();
//glassPane.setBounds(0,50,image.getWidth(),image.getHeight());
setGlassPane(glassPane);
glassPane.setVisible(true);
}
private void initJMenuBar()
{
menuBar=new JMenuBar();
file=new JMenu("File");
imageMenu=new JMenu("Image");
scale=new JMenu("Scale");
filters=new JMenu("Filters");
sobel=new JMenu("Sobel");
save=new JMenuItem(new SaveAction("Save As..."));
exit=new JMenuItem(new DisposeJInternalFrameAction("Close"));
scaleNoInterpolation=new JMenuItem(new
ScaleNoInterpolationAction("No Interpolation",title));
scaleLinearInterpolation=new JMenuItem(new
ScaleLinearInterpolationAction("Bilinear Interpolation",title));
duplicate=new JMenuItem(new
DuplicateImageAction("Duplicate",title));
invert=new JMenuItem(new InvertImageAction("Invert",title));
desaturate=new JMenuItem(new
DesaturateImageAction("Desaturate",title));
histogram=new JMenuItem(new
CreateHistogramAction("Histogram",title));
average=new JMenuItem(new FilterAverageAction("Average"));
laplace=new JMenuItem(new FilterLaplaceAction("Laplace"));
sobelHorizontal=new JMenuItem(new
FilterSobelHorizontalAction("Horizontal"));
sobelVertical=new JMenuItem(new
FilterSobelVerticalAction("Vertical"));
sobelBidirectional=new JMenuItem(new
FilterSobelBidirectionalAction("Bidirectional"));
sobelThreshold=new JMenuItem(new
FilterSobelThresholdAction("Threshold"));
menuBar.add(file);
menuBar.add(imageMenu);
menuBar.add(filters);
file.add(save);
file.add(exit);
imageMenu.add(scale);
imageMenu.add(duplicate);
imageMenu.add(invert);
imageMenu.add(desaturate);
imageMenu.add(histogram);
scale.add(scaleNoInterpolation);
scale.add(scaleLinearInterpolation);
filters.add(average);
filters.add(laplace);
filters.add(sobel);
sobel.add(sobelHorizontal);
sobel.add(sobelVertical);
sobel.add(sobelBidirectional);
sobel.add(sobelThreshold);
setJMenuBar(menuBar);
}
private void initCanvas(BufferedImage image)
{
canvas=new LightweightCanvas(image);
getContentPane().add(canvas);
}
public void setImage(BufferedImage image) throws InterruptedException
{
canvas.setImage(image);
}
public BufferedImage getImage()
{
return canvas.getImage();
}
}
[END CODE]
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24