4 Replies - 2283 Views - Last Post: 22 October 2011 - 04:52 PM Rate Topic: -----

#1 mahi.aw  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 21-May 11

mouse dragging & jpanel flashing problem

Posted 22 October 2011 - 08:36 AM

hello all,,

need small help:

I have small GUI program: in which component are assembled in following order:

The main JFrame----contains JScrollPane-----Contains JPanel(1)-------Contains JPanel(2):

I have attached the mousemotionListner to JPanel(2): I have implemented paint method for JPanle 2 as:

@Override
    public void paint(Graphics g) {

        /*draw the image on this JPanel*/
        g.drawImage(img, 0, 0, display_imageSizeX, display_imageSizeY, this);

        /*set the x,y axis legend*/
        g.setFont(new Font("Serif", Font.BOLD, 20));
        g.setColor(Color.yellow);
        g.drawString("Y", 10, 20);
        g.drawString("X", display_imageSizeX - 20, display_imageSizeX - 10);

        /*paint the border for Jpanel*/
        this.paintBorder(g);
    }



Now i want to provide the functionality in which i can dragged the JPanel2 in JscrollPane:::i could able to implement the method but the problem is when i started dragging the JPanel2 1st time it flashes and it appears that JPanel2 is painted two times:although the final result is what i expected (proper dragging is happening, but my problem is with flashing of components)
here is my implementation of dragging

public void mousePressed(MouseEvent me) {

        this.setCursor(new Cursor(Cursor.HAND_CURSOR));
        this.jvpLocationPre = this.scrVP.getLocation();
        this.mouseX = me.getX();
        this.mouseY = me.getY();
    }

public void mouseDragged(MouseEvent me) {

        /*calculate the new position for viewPort*/
        int newPositionX = this.jvpLocationPre.x + (this.mouseX - me.getX());
        int newPositionY = this.jvpLocationPre.y + (this.mouseY - me.getY());

        //prevent going over the borders. maximum=picture width minus scroll pane view width
        int maxX = this.getWidth() - scrVP.getWidth();
        int maxY = this.getHeight()- scrVP.getHeight();

        if (newPositionX > maxX) {
            newPositionX = maxX;
        }
        if (newPositionY > maxY) {
            newPositionY = maxY;
        }

        if (newPositionX < 0) {
            newPositionX = 0;
        }
        if (newPositionY < 0) {
            newPositionY = 0;
        }

        /*set the new Position for viewPort*/
        scrVP.setViewPosition(new Point(newPositionX, newPositionY));

        /*store the new values as old*/
        this.jvpLocationPre = scrVP.getViewPosition();
    }




any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: mouse dragging & jpanel flashing problem

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,778
  • Joined: 27-December 08

Re: mouse dragging & jpanel flashing problem

Posted 22 October 2011 - 09:02 AM

Why is this in MySQL? Moved to Java.

If you want to drag and drop JComponents, you should use the Java drag and drop API.
http://www.dreaminco...cs-of-dragging/
http://www.dreaminco...art-2-dropping/
Was This Post Helpful? 0
  • +
  • -

#3 mahi.aw  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 114
  • Joined: 21-May 11

Re: mouse dragging & jpanel flashing problem

Posted 22 October 2011 - 09:33 AM

View Postmacosxnerd101, on 22 October 2011 - 09:02 AM, said:

Why is this in MySQL? Moved to Java.

If you want to drag and drop JComponents, you should use the Java drag and drop API.
http://www.dreaminco...cs-of-dragging/
http://www.dreaminco...art-2-dropping/

oh..i didn't noticed that it is mysql:sorry:
well maco thanks for information. i will check java drag and drop option:
but well do you have any idea what could be the reseaon for flashing:
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10693
  • View blog
  • Posts: 39,778
  • Joined: 27-December 08

Re: mouse dragging & jpanel flashing problem

Posted 22 October 2011 - 09:58 AM

Possibly because repaint() is being called too often. The DnD API is cleaner for a lot of reasons. One of which is b/c it handles the painting, and transfers Objects via serialization. You don't see any real flickering so to speak.
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: mouse dragging & jpanel flashing problem

Posted 22 October 2011 - 04:52 PM

first your paint() shoud call supoer.pant(g)
that will call paintBorder() for you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1