Results 1 to 9 of 9
  1. #1
    George R. is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default Using threads will solve my problem?

    Well, I have two jpanels, A and B. Whatever mouse movement I do in jpanel A, I want it to be done in jpanel B too. What I do now is to call the mouselistener of jpanel B whenever the mouselistener of jpanel A is called, using the same mouse event. Everything is fine, but the response is slow.

    So, I thought to use threads. You think that this will be the solution to my problem? If yes, where should I use the thread? I mean, I used it inside the mouse listener, but there was no difference, the response remained slow.

    Thanks

    PS: I'm sorry if I don't post in the right place.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by George R. View Post
    Well, I have two jpanels, A and B. Whatever mouse movement I do in jpanel A, I want it to be done in jpanel B too. What I do now is to call the mouselistener of jpanel B whenever the mouselistener of jpanel A is called, using the same mouse event. Everything is fine, but the response is slow.

    So, I thought to use threads. You think that this will be the solution to my problem? If yes, where should I use the thread? I mean, I used it inside the mouse listener, but there was no difference, the response remained slow.

    Thanks

    PS: I'm sorry if I don't post in the right place.
    It's the correct (sub) forum, no need to worry ;-) If your thread(s) don't block the EDT (Event Dispatch Thread) all is fine. Multiple threads won't help you then because Swing's EDT has to do all the painting in its single thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    If the response is slow, then you are probably bogging down the event thread by placing too much activity in your event handlers, or maybe doing too much painting. If that *is* what's going on, then it should be possible to offload whatever you're doing into other threads.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Whatever mouse movement I do in jpanel A, I want it to be done in jpanel B too.
    Huh?

    What I do now is to call the mouselistener of jpanel B whenever the mouselistener of jpanel A is called, using the same mouse event.
    Whatever it is you're doing, that's just plain wrong. A MouseEvent holds information regarding its source.

    Everything is fine
    Not really.

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    db

  5. #5
    George R. is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Thank you all for your replies.

    Actually, what I want to do is what I draw in jpanel A to be drawn in jpanel B as well. To do this I use the following code inside the mouseDragged method

    Java Code:
    public void mouseDragged(MouseEvent e) {
            Point p = e.getPoint();
    
            if (drawingEnabled)
                this.draw(start, p);       //I use this method to draw...
            else
                this.erase(start, p);      //...and this to erase
            
            start = p;
    
            //for jpanel B I use this:
            jpanelB.mouseDragged(e);
    }
    When jpanelB is visible the response is slow, but when it is not the response is ok.

    Is there any other way to do this? If I use threads where should I use them?
    Thanks again

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    what I want to do is what I draw in jpanel A to be drawn in jpanel B as well.
    If the panels have the same dimensions:
    -- ensure panel B can access a reference to panel A
    -- in panel B, override paintComponent
    Java Code:
    protected void paintComponent(Graphics g) {
      panelA.paintComponent(g);
    }
    -- after any drawing is changed in panel A, cal panelB.repaint();

    And where's that SSCCE?

    db

  7. #7
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Post

    What exactly do you mean by mouse movement?
    - drawing some shapes? or
    - just moving mouse over the panel (though this doesn't make much sense to me)

    so, probably you are drawing some shapes.

    For this you can maintain the list of shapes drawn on panel A. Now draw all these shapes on panel by passing on updated list (everytime whenever mouselistener receives the event) to panel B.

    If this makes sense to you and have some doubts then let me know. I can give some more pointers in this direction.

    And you don't need two threads for this. I don't think it will be slow.

  8. #8
    George R. is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    And where's that SSCCE?
    I've tried to make one, the main class is this

    Java Code:
    public static void main(String[] a) {
          JFrame myFrame = new JFrame();
          myFrame.setSize(500,500);
    
          //Create a split pane
          JSplitPane splitJSP = new JSplitPane();
          myFrame.setContentPane(splitJSP);
    
          //Set left component as the source and the right as the target
          PaintJP rightPJP = new PaintJP("Target", null);
          PaintJP leftPJP = new PaintJP("Source", rightPJP);
    
          splitJSP.setLeftComponent(leftPJP);
          splitJSP.setRightComponent(rightPJP);
          
          myFrame.setVisible(true);
       }
    and the PaintJP class is the following one:
    Java Code:
    public class PaintJP extends  JPanel implements MouseMotionListener, MouseListener{
    
        public PaintJP(String title, PaintJP paintJP){
            this.setSize(300,300);
            this.setBackground(Color.white);
            this.paintJP = paintJP;
            this.add(new JLabel(title));
            this.initImage();
            this.addMouseListener(this);
            this.addMouseMotionListener(this);
        }
    
        PaintJP paintJP;
    
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if(image == null)
                initImage();
            g.drawImage(image, 0, 0, this);
        }
    
        private void initImage(){
            int w = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
            int h = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
            image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = image.createGraphics();
            g2.setPaint(getBackground());
            g2.fillRect(0,0,w,h);
            g2.dispose();
        }
    
        public void draw(Point start, Point end){
            Graphics2D g2 = image.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(Color.red);
    
            g2.setStroke(new BasicStroke(5,
                                         BasicStroke.CAP_ROUND,
                                         BasicStroke.JOIN_ROUND));
    
            g2.draw(new java.awt.geom.Line2D.Double((start.getX()),
                                                    (start.getY()),
                                                    (end.getX()),
                                                    (end.getY())));
            g2.dispose();
            repaint();
    
    
    
        }
    
        public void mouseDragged(MouseEvent e) {
            Point p = e.getPoint();
    
            this.draw(start, p);
            
            start = p;
    
            try { paintJP.mouseDragged((e)); }
            catch(Exception ex){}
        }
    
        public void mouseMoved(MouseEvent e) {  }
    
        public void mouseClicked(MouseEvent e) { }
    
        public void mousePressed(MouseEvent e) {
            start = e.getPoint();
            
            try{ paintJP.mousePressed(e); }
            catch(Exception ex){}
        }
    
        public void mouseReleased(MouseEvent e) { }
    
        public void mouseEntered(MouseEvent e) { }
    
        public void mouseExited(MouseEvent e) { }
    
        private java.awt.Point start;
        private java.awt.image.BufferedImage image;
    }
    but, when I run the above code the response is ok.

  9. #9
    George R. is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by lovelesh View Post
    What exactly do you mean by mouse movement?
    - drawing some shapes? or
    - just moving mouse over the panel (though this doesn't make much sense to me)

    so, probably you are drawing some shapes.

    For this you can maintain the list of shapes drawn on panel A. Now draw all these shapes on panel by passing on updated list (everytime whenever mouselistener receives the event) to panel B.

    If this makes sense to you and have some doubts then let me know. I can give some more pointers in this direction.

    And you don't need two threads for this. I don't think it will be slow.
    I mean drawing, yes. You can see it in the code I posted before. When mouseDragged method is called in jpanelA, I want it to be called in jpanelB as well. When I jpanelB is visible the response is slow, but this doesn't happen when it's invisible, despite the fact that the mouseDragged of jpanelB is actually called, and when I turn it visible, I can see the drawing.

Similar Threads

  1. Need help to solve my problem
    By Copro in forum New To Java
    Replies: 4
    Last Post: 08-21-2009, 04:26 AM
  2. Could someone help me to solve this problem...
    By Vinny in forum Java Software
    Replies: 1
    Last Post: 07-08-2009, 06:59 PM
  3. Help me to solve problem
    By mansoorhacker in forum Forum Guides
    Replies: 8
    Last Post: 01-24-2009, 06:29 PM
  4. Solve my Problem
    By kyo in forum New To Java
    Replies: 1
    Last Post: 12-16-2008, 02:22 PM
  5. Help me to solve problem
    By mansoorhacker in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 08:15 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
  •