Drag Flicker ...

Hey,

I am trying to create components that can be dragged by the user. I make my class implement MouseMotionListener and MouseListener.

Here are my functions (I got these from another thread on this site):

Component dragComponent;

int xAdjustment;

int yAdjustment;

String q="";

publicvoid mouseDragged(MouseEvent me)

{

if (dragComponent ==null)return;

dragComponent.setLocation(me.getX() + xAdjustment, dragComponent.getLocation().y);

repaint();

}

publicvoid mouseMoved(MouseEvent me){

}

publicvoid mousePressed(MouseEvent me){

dragComponent =null;

Component c = (Component)me.getSource();

if (cinstanceof JLayeredPane){

q="prob";

repaint();

return;

}

dragComponent = c;

xAdjustment = dragComponent.getLocation().x - me.getX();

yAdjustment = dragComponent.getLocation().y - me.getY();

dragComponent.setLocation(me.getX() + xAdjustment, dragComponent.getLocation().y);

contentPane.moveToFront(dragComponent);

q=dragComponent.getName();

repaint();

}

publicvoid mouseReleased(MouseEvent me){

dragComponent =null;

}

publicvoid mouseClicked(MouseEvent me){

}

publicvoid mouseEntered(MouseEvent me){

}

publicvoid mouseExited(MouseEvent me){

}

When I drag something, it seems to flicker. There are two images seen at once, side by side.

Is there anyway to solve this?

Thanks ;)

[2815 byte] By [oni25a] at [2007-10-2 4:37:14]
# 1

Yeah I have seen this before. It happens because your component is bouncing back and forth between two locations. At least that's what was happening in my case.

Anyway here is my WindowUtilities class. It contains two static inner classes: Draggable and Resizeable. All you need to do to make your component draggable is do new Draggable(component) If you're just interested in figuring out what's wrong with your dragging, you could just compare your code to mine...

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

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

WindowUtilities.java

package tjacobs.ui;

import java.awt.*;

import java.awt.event.*;

import java.io.File;

import javax.swing.JFrame;

public abstract class WindowUtilities {

public static final int RESIZE_MARGIN_SIZE = 4;

public static final String VISUALIZE_FILE = "locations.dat";

private static File sVisFile;

private static class Entry {

Class key;

Point size;

}

static {

sVisFile = new File(VISUALIZE_FILE);

}

public static Point getBottomRightOfScreen(Component c) {

Dimension scr_size = Toolkit.getDefaultToolkit().getScreenSize();

Dimension c_size = c.getSize();

return new Point(scr_size.width - c_size.width, scr_size.height - c_size.height);

}

public static Window visualize(Component c) {

return visualize(c, true);

}

public static Window visualize (Component c, int width, int height) {

return visualize(c, true, width, height);

}

public static Window visualize(Component c, boolean exit, int width, int height) {

JFrame f;

Window w;

if (c instanceof Window) {

w = (Window) c;

}

else {

f = new JFrame();

f.getContentPane().add(c);

w = f;

}

w.setSize(width, height);

w.setLocation(100, 100);

if (exit) {

w.addWindowListener(new WindowClosingActions.Exit());

}

w.setVisible(true);

return w;

}

public static Window visualize(Component c, boolean exit) {

JFrame f;

Window w;

if (c instanceof Window) {

w = (Window) c;

}

else {

f = new JFrame();

f.getContentPane().add(c);

w = f;

}

w.pack();

w.setLocation(100, 100);

if (exit) {

w.addWindowListener(new WindowClosingActions.Exit());

}

w.setVisible(true);

return w;

}

public static class Draggable extends MouseAdapter implements MouseMotionListener {

Point mLastPoint;

Component mDraggable;

public Draggable(Component w) {

w.addMouseMotionListener(this);

w.addMouseListener(this);

mDraggable = w;

}

public void mousePressed(MouseEvent me) {

if (mDraggable.getCursor().equals(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR))) {

mLastPoint = me.getPoint();

}

else {

mLastPoint = null;

}

}

private void setCursorType(Point p) {

Point loc = mDraggable.getLocation();

Dimension size = mDraggable.getSize();

if ((p.y + RESIZE_MARGIN_SIZE < loc.y + size.height) && (p.x + RESIZE_MARGIN_SIZE < p.x + size.width)) {

mDraggable.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

}

}

public void mouseReleased(MouseEvent me) {

mLastPoint = null;

}

public void mouseMoved(MouseEvent me) {

setCursorType(me.getPoint());

}

public void mouseDragged(MouseEvent me) {

int x, y;

if (mLastPoint != null) {

x = mDraggable.getX() + (me.getX() - (int)mLastPoint.getX());

y = mDraggable.getY() + (me.getY() - (int)mLastPoint.getY());

mDraggable.setLocation(x, y);

}

}

}

public static class Resizeable extends MouseAdapter implements MouseMotionListener {

int fix_pt_x = -1;

int fix_pt_y = -1;

Component mResizeable;

Cursor mOldcursor;

public Resizeable(Component c) {

mResizeable = c;

c.addMouseListener(this);

c.addMouseMotionListener(this);

}

public void mouseEntered(MouseEvent me) {

setCursorType(me.getPoint());

}

private void setCursorType(Point p) {

boolean n = p.y <= RESIZE_MARGIN_SIZE;

boolean s = p.y + RESIZE_MARGIN_SIZE >= mResizeable.getHeight();

boolean w = p.x <= RESIZE_MARGIN_SIZE;

boolean e = p.x + RESIZE_MARGIN_SIZE >= mResizeable.getWidth();

if (e) {

if (s) {

mResizeable.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));

return;

}

mResizeable.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));

return;

}

if(s) {

mResizeable.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));

return;

}

}

public void mouseExited(MouseEvent me) {

if (mOldcursor != null)

((Component)me.getSource()).setCursor(mOldcursor);

mOldcursor = null;

}

public void mousePressed(MouseEvent me) {

Cursor c = mResizeable.getCursor();

Point loc = mResizeable.getLocation();

if (c.equals(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR))) {

fix_pt_x = loc.x;

fix_pt_y = loc.y;

return;

}

if (c.equals(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR))) {

fix_pt_x = loc.x;

fix_pt_y = -1;

return;

}

if (c.equals(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR))) {

fix_pt_x = -1;

fix_pt_y = loc.y;

return;

}

}

public void mouseReleased(MouseEvent me) {

fix_pt_x = -1;

fix_pt_y = -1;

}

public void mouseMoved(MouseEvent me) {

setCursorType(me.getPoint());

}

public void mouseDragged(MouseEvent me) {

Point p = me.getPoint();

int width = fix_pt_x == -1 ? mResizeable.getWidth() : p.x;

int height = fix_pt_y == -1 ? mResizeable.getHeight() : p.y;

mResizeable.setSize(new Dimension(width > 1 ? width : 1, height > 1 ? height : 1));

}

}

}

tjacobs01a at 2007-7-16 0:09:54 > top of Java-index,Desktop,Core GUI APIs...
# 2
Hey thanks for the code,your method is sorta dif from mine, but I get wut u did. :) thanks again ;)
oni25a at 2007-7-16 0:09:54 > top of Java-index,Desktop,Core GUI APIs...