Results 1 to 9 of 9
- 03-22-2011, 05:12 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 13
- Rep Power
- 0
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.
- 03-22-2011, 05:31 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-22-2011, 10:08 PM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.
- 03-23-2011, 02:22 AM #4
Huh?Whatever mouse movement I do in jpanel A, I want it to be done in jpanel B too.
Whatever it is you're doing, that's just plain wrong. A MouseEvent holds information regarding its source.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.
Not really.Everything is fine
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
db
- 03-23-2011, 08:31 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 13
- Rep Power
- 0
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
When jpanelB is visible the response is slow, but when it is not the response is ok.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); }
Is there any other way to do this? If I use threads where should I use them?
Thanks again
- 03-23-2011, 09:10 AM #6
If the panels have the same dimensions:what I want to do is what I draw in jpanel A to be drawn in jpanel B as well.
-- ensure panel B can access a reference to panel A
-- in panel B, override paintComponent-- after any drawing is changed in panel A, cal panelB.repaint();Java Code:protected void paintComponent(Graphics g) { panelA.paintComponent(g); }
And where's that SSCCE?
db
- 03-23-2011, 02:41 PM #7
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
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.
- 03-24-2011, 07:07 PM #8
Member
- Join Date
- Jan 2010
- Posts
- 13
- Rep Power
- 0
I've tried to make one, the main class is this
and the PaintJP class is the following one: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); }
but, when I run the above code the response is ok.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; }
- 03-24-2011, 07:13 PM #9
Member
- Join Date
- Jan 2010
- Posts
- 13
- Rep Power
- 0
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
-
Need help to solve my problem
By Copro in forum New To JavaReplies: 4Last Post: 08-21-2009, 04:26 AM -
Could someone help me to solve this problem...
By Vinny in forum Java SoftwareReplies: 1Last Post: 07-08-2009, 06:59 PM -
Help me to solve problem
By mansoorhacker in forum Forum GuidesReplies: 8Last Post: 01-24-2009, 06:29 PM -
Solve my Problem
By kyo in forum New To JavaReplies: 1Last Post: 12-16-2008, 02:22 PM -
Help me to solve problem
By mansoorhacker in forum New To JavaReplies: 3Last Post: 11-13-2008, 08:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks