Link to home
Start Free TrialLog in
Avatar of omavideniz
omavideniz

asked on

Dragging Problem

hi,

i write down a component to provide dragging but java run-time cannot paint it on-time(although it has a small images) and mouse looses the components control. thus some unwanted behaviour occurs. how can i solve this problem? is there any way to pause the event system while the current event finishs its execution? i think pausing events would help. so i'm looking for your help.

best regards.
Avatar of rkenworthy
rkenworthy

Its hard to suggest much as the description is kind vague. What exactly is not getting painted properly? The container component or the component getting dragged or what?

Anyway, for the component which is not getting painted properly is it possible to put a statement in the paint method such as "if (dragging) return;" so that painting is suspended until the dragging is completed?
post YOUR code (small compilable example please)
Avatar of omavideniz

ASKER

sample code from PAQ...

/*
all you need is two images (one for background and one more)
inside the current directory.
compile this example and run it - that's all.
*/
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
public class demo2 extends Container
{
 private Image bgImage;
 private Image i, i1, i2, i3;
 
 public demo2()
 {
   loadImages();
   buildUI();
 }
 // load one background image
 // and one sample foreground that we will manipulate
 public void loadImages()
 {  
   setBackground(Color.lightGray);
   i = Toolkit.getDefaultToolkit().getImage("duke.gif");
   bgImage = Toolkit.getDefaultToolkit().getImage("aa.jpg");
   MediaTracker tracker = new MediaTracker(this);
   tracker.addImage(i, 11);
   tracker.addImage(bgImage, 11);
   try
   {
     tracker.waitForID(11);
   } catch (Exception x)
   {
     x.printStackTrace();
   }
   System.out.println("w = " + i.getWidth(null));
   System.out.println("w = " + bgImage.getWidth(null));
 }
 
 private void buildUI()
 {
   setLayout(new GridLayout(1, 1));
   ImageContainer main = new ImageContainer(bgImage);
   add(main);
   main.setLayout(new FlowLayout());
   ImageContainer i1 = new ImageContainer(i, 20);
   ImageContainer i2 = new ImageContainer(i, 20);
   i1.setOpaque(true);
   main.add(i1);
   main.add(i2);
 }
 
 
// some double buffering code  
 private Image offImage;
 private Graphics offGraphics;
 
 public void update (Graphics g)
 {
   paint(g);
 }
 public void paint (Graphics g)
 {
   Dimension d = getSize();
   if (offImage == null || offImage.getWidth(null) != d.width || offImage.getHeight(null) != d.height)
   {
     offImage = createImage(d.width, d.height);
     offGraphics = offImage.getGraphics();
   }
   
   offGraphics.setColor(getBackground());
   offGraphics.fillRect(0, 0, d.width, d.height);
   super.paint(offGraphics);
   g.drawImage(offImage, 0, 0, null);
 }
 
 // main method
 public static void main(String args[])
 {
   Frame f = new Frame("demo");
   f.add(new demo2());
   f.setSize(500, 500);
   f.setVisible(true);
   f.addWindowListener(new WindowAdapter()
   {
     public void windowClosing(WindowEvent e)
     {
       System.exit(0);
     }
   });
 }

 // helper class ImageContainer class :)
 class ImageContainer extends Container
  implements MouseListener, MouseMotionListener
 {
   private Image image;
   private int delta;
   private boolean isOpaque = false;
 
   private int nStartX;
   private int nStartY;
   private Point nMoveStartPos;
   
   public ImageContainer(Image i)
   {
     this(i, 0);
   }
   
   public ImageContainer(Image i, int d)
   {
     image = i;
     delta = d;
     addMouseListener( this );
     addMouseMotionListener( this );
   }
   public void setOpaque(boolean b)
   {
     isOpaque = b;
     repaint();
   }
   public boolean isOpaque()
   {
     return isOpaque;
   }
   public Dimension preferredSize()
   {
     return new Dimension(image.getWidth(null) + 2 * delta, image.getHeight(null) + 2 * delta);
   }
   public Dimension minimumSize()
   {
     return super.minimumSize();
   }
   public Dimension getMaximumSize()
   {
     return preferredSize();
   }
   public void update(Graphics g)
   {
     paint(g);
   }
   public void paint(Graphics g)
   {
     Dimension d = getSize();
     
     if(isOpaque)
     {
       g.setColor(getBackground());
       g.fillRect(0, 0, d.width, d.height);
     }
     
     int x = (d.width - (image.getWidth(null) + 2 * delta)) / 2;
     int y = (d.height - (image.getHeight(null) + 2 * delta)) / 2;
//      System.out.println("x = " + x + ", y = " + y + ", delta = " + delta);
     g.drawImage(image, x + delta, y + delta, null);
     g.setColor(Color.black);
     g.drawRect(0, 0, d.width - 1, d.height - 1);
     g.drawRect(1, 1, d.width - 2, d.height - 2);
     super.paint(g); // children
   }
   public void mouseDragged( MouseEvent e )
   {
     int x = e.getX();
     int y = e.getY();
     Point p = getLocation();
     
     if( x < 0 || y < 0 )
     {
       setLocation( nMoveStartPos );
       return;
     }
            
     int nX = p.x + x - nStartX;
     int nY = p.y + y - nStartY;

     setLocation( nX, nY );
   }
   
   public void mouseMoved( MouseEvent e )
   {
   }
      
   public void mousePressed( MouseEvent e )
   {            
     getParent().add( this, 0 );
     nStartX = e.getX();
     nStartY = e.getY();
     nMoveStartPos  = getLocation();
   }

   public void mouseReleased( MouseEvent e )
   {
     int x = e.getX();
     int y = e.getY();

     if( x < 0 || y < 0 )
     {
       setLocation( nMoveStartPos );
       return;
     }
   }

   public void mouseEntered( MouseEvent e )
   {
   }

   public void mouseExited( MouseEvent e )
   {
   }

   public void mouseClicked( MouseEvent e )
   {
   }
 }
}
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i tried your suggestions but can't get any valuable changes.
mouse still looses control. i think its not possible to overcome this problem..
I fixed the repainting problems only.
I suppose that when you drag very quickly, mouse pointer will exit the component region, so it won't receive any following mouseDrag messages.

if this is the case, you'll have to implement your own Framework (I've done this) - with ONLY ONE MAIN java.awt. component and many draggable 'objects' inside it.
but how implementing an Framework that you tell will be different than the sample code i have given previously.
could you please give an outline or short sample for it?
omavideniz:

You have many open questions:

https://www.experts-exchange.com/jsp/qShow.jsp?qid=20222327
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20261522
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20255833
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20248161
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20247596
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20247451
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20239442
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20235419
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20234355
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20230738
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20227630
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20210091
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20163508
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20163400
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20156064
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20151257
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20149551
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20107807
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20107283
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20096836
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20095000
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20094242
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20081432
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20068029
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12040399
https://www.experts-exchange.com/jsp/qShow.jsp?qid=12040320

To assist you in your cleanup, I'm providing the following guidelines:

1.  Stay active in your questions and provide feedback whenever possible. Likewise, when feedback has not been provided by the experts, commenting again makes them receive an email notification, and they may provide you with further information. Experts have no other method of searching for questions in which they have commented, except manually.

2.  Award points by hitting the Accept Comment As Answer button located above and to the left of that expert's comment.

3.  When grading, be sure to read:
https://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#3
to ensure that you understand the grading system here at EE. If you grade less than an A, you must explain why.

4.  Questions that were not helpful to you should be PAQ'd (stored in the database for their valuable content?even if not valuable to you) or deleted. To PAQ or delete a question, you must first post your intent in that question to make the experts aware. Then, if no experts object after three full days, you can post a zero-point question at community support to request deletion or PAQ. Please include the link(s) to the question(s).
CS:  https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
At that point, a moderator can refund your points and PAQ or delete the question for you. The delete button does not work.

5.  If you fail to respond to this cleanup request, I must report you to the Community Support Administrator for further action.

Our intent is to get the questions cleaned up, and not to embarrass or shame anyone. If you have any questions or need further assistance at all, feel free to ask me in this question or post a zero-point question at CS. We are very happy to help you in this task!


thanks!
amp
community support moderator

2/6
not fulfills my needs but acceptable.