Results 1 to 6 of 6
  1. #1
    ankitmcgill is offline Member
    Join Date
    Nov 2008
    Posts
    22
    Rep Power
    0

    Default n00b : Ghost image while using mouseDragged event to move a filled jPanel

    What i am trying to do is quite simple. Initially i have a transparent jPanel with white border.
    When i move the mouse over it it turns into solid green and back to transparent when the mouse moves out of it.

    now when the user clicks and drags the jPanel i want it to follow the mouse. The problem is when the user does that i see this ghost image of the original jPanel offset by some amount.

    i looked thru my code again and again but cant figure out with my limited java experience.

    would appreciate if someone can point out what im doing wrong ... below is my component class

    Java Code:
    public class transparent_page_box extends JPanel implements MouseMotionListener,MouseListener
    {
        private int border=0;
        private int width=0;
        private int height=0;
        static private int lastx=0;
        static private int lasty=0;
        private int current_x;
        private int current_y;
        static private int box_count=0;
        
        transparent_page_box(int w,int h,int b)
        {
            this.setOpaque(false);
            //this.setSize(w, h);
            if((box_count%10)==0)
            {
                this.setBounds(lastx+20,lasty+20, width, height);
                current_x=lastx+20;
                current_y=lasty+20;
                box_count++;
                lastx+=20;
                lasty+=20;
            }
            else
            {
                this.setBounds(lastx+20,lasty, width, height);
                current_x=lastx+20;
                box_count++;
                lastx+=20;
            }
            width=w;
            height=h;
            border=b;
            this.setSize(width, height);
            
            
            super.addMouseMotionListener(this);
            super.addMouseListener(this);
        }
        
     public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2d=(Graphics2D)g;
            g2d.setColor(Color.white);
            this.setLocation(current_x, current_y);
            this.setBorder(BorderFactory.createLineBorder(Color.white));
            g2d.drawRect(border, border, width-2*border, height-2*border);
    
        }
    public void mouseClicked(MouseEvent e){}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {this.setOpaque(false);
                                             this.repaint();}
    public void mouseEntered(MouseEvent e) {this.setOpaque(true);
                                            this.setBackground(Color.green);
                                            this.repaint();
                                            }
    public void mouseExited(MouseEvent e) {this.setOpaque(false);
                                           this.repaint();}
     
    public void mouseDragged(MouseEvent e)
    {
        this.setOpaque(true);
        this.setBackground(Color.green);
        current_x=e.getX();
        current_y=e.getY();
        //this.setBounds(currentx, currenty, width, height);
        //this.setLocation(currentx, currenty);
        this.repaint();
        
        
    }
    
    public void mouseMoved(MouseEvent e){}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One problem is with your current_x and current_y variables. The mouse will likely give you values that are relative to the JPanel itself when what you want are the values relative to the Container that holds your JPanel (correction: or relative to the screen). Perhaps you should use getXOnScreen here.

    Another problem I see is your doing program logic (setting the JPanel's location) from within your paintComponent method. This is a no-no. Do this from within the mouseDragged method (as you've commented out), then call repaint.
    Last edited by Fubarable; 11-02-2008 at 04:33 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    i.e.,
    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    
    public class TransparentPageBox extends JPanel
    {
      private int border = 0;
      private int width = 0;
      private int height = 0;
      static private int lastx = 0;
      static private int lasty = 0;
      private int current_x;
      private int current_y;
      static private int box_count = 0;
    
      TransparentPageBox(int w, int h, int b)
      {
        this.setOpaque(false);
        // this.setSize(w, h);
        if ((box_count % 10) == 0)
        {
          this.setBounds(lastx + 20, lasty + 20, width, height);
          current_x = lastx + 20;
          current_y = lasty + 20;
          box_count++;
          lastx += 20;
          lasty += 20;
        }
        else
        {
          this.setBounds(lastx + 20, lasty, width, height);
          current_x = lastx + 20;
          box_count++;
          lastx += 20;
        }
        width = w;
        height = h;
        border = b;
        this.setSize(width, height);
    
        MyMouseAdaptor mouseAdaptor = new MyMouseAdaptor();
        super.addMouseMotionListener(mouseAdaptor);
        super.addMouseListener(mouseAdaptor);
        setBorder(BorderFactory.createLineBorder(Color.white));
      }
    
      public void paintComponent(Graphics g)
      {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.white);
        g2d.drawRect(border, border, width - 2 * border, height - 2 * border);
      }
    
      private class MyMouseAdaptor extends MouseAdapter
      {
        private int oldX;
        private int oldY;
        private Point location;
        
        public void mousePressed(MouseEvent e)
        {
          oldX = e.getXOnScreen();
          oldY = e.getYOnScreen();
          location = getLocation();
        }
    
        public void mouseReleased(MouseEvent e)
        {
          TransparentPageBox.this.setOpaque(false);
          TransparentPageBox.this.repaint();
        }
    
        public void mouseEntered(MouseEvent e)
        {
          TransparentPageBox.this.setOpaque(true);
          TransparentPageBox.this.setBackground(Color.green);
          TransparentPageBox.this.repaint();
        }
    
        public void mouseExited(MouseEvent e)
        {
          TransparentPageBox.this.setOpaque(false);
          TransparentPageBox.this.repaint();
        }
    
        public void mouseDragged(MouseEvent e)
        {
          TransparentPageBox.this.setOpaque(true);
          TransparentPageBox.this.setBackground(Color.green);
          current_x = location.x + e.getXOnScreen() - oldX;
          current_y = location.y + e.getYOnScreen() - oldY;
          setLocation(current_x, current_y);
          TransparentPageBox.this.repaint();
        }
    
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("transparent_page_box");
        JPanel contentPane = (JPanel) frame.getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(Color.blue);
        contentPane.setPreferredSize(new Dimension(800, 600));
        int panelCount = 6;
        for (int i = 0; i < panelCount ; i++)
        {
          contentPane.add(new TransparentPageBox(400, 300, 5));     
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }
    Last edited by Fubarable; 11-02-2008 at 04:40 AM.

  4. #4
    ankitmcgill is offline Member
    Join Date
    Nov 2008
    Posts
    22
    Rep Power
    0

    Default

    wow ! solution + sample code .. thanks a bunch :D

    So i corrected the things u said .. it seems to work better but not perfect yet.

    1. the ghost image is no more more there
    2. something else is happening now. now sometimes when i click on the panel to right before dragging it , it seems to jump to a new location nearby randomly.
    3. in my code you replaced all 'this' with 'transparent_page_box.this'. why is that ?

    ps it just hit me that random jump has something to do with the cordinates .. any place i could find info about all the cordinate functions like getPoint(), getX(), getXonScreen() etc ?
    Last edited by ankitmcgill; 11-02-2008 at 06:22 AM.

  5. #5
    ankitmcgill is offline Member
    Join Date
    Nov 2008
    Posts
    22
    Rep Power
    0

    Default

    update .. got it ! i simplified ur logic a bit and now it works like a charm !
    thank you for pointing me in the right direction :D

    Java Code:
    public void mouseClicked(MouseEvent e){}
    public void mousePressed(MouseEvent e) {oldX=e.getX();
                                            oldY=e.getY();
                                            location=e.getPoint();}
    public void mouseReleased(MouseEvent e) {transparent_page_box.this.setOpaque(false);
                                             transparent_page_box.this.repaint();}
    public void mouseEntered(MouseEvent e) {this.setOpaque(true);
                                            this.setBackground(Color.green);
                                            this.repaint();
                                            }
    public void mouseExited(MouseEvent e) {this.setOpaque(false);
                                           this.repaint();}
     
    public void mouseDragged(MouseEvent e)
    {
        this.setOpaque(true);
        this.setBackground(Color.green);
        int deltax=e.getX()-oldX;
        int deltay=e.getY()-oldY;
        current_x+=deltax;
        current_y+=deltay;
        setLocation(current_x,current_y);
        transparent_page_box.this.repaint();

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. n00b help with java2D api
    By michcio in forum Java 2D
    Replies: 7
    Last Post: 10-30-2008, 06:18 PM
  2. yet another N00b
    By java_student_4now in forum New To Java
    Replies: 6
    Last Post: 09-16-2008, 03:56 PM
  3. n00b at JSP - Need help
    By sylo18 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-04-2008, 07:33 AM
  4. MouseDragged Event
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 03-04-2008, 05:09 AM
  5. Replies: 2
    Last Post: 12-01-2007, 07:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •