Need help with repainting Graphics

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2007
Posts: 6
Reputation: thenava is an unknown quantity at this point 
Solved Threads: 1
thenava thenava is offline Offline
Newbie Poster

Need help with repainting Graphics

 
0
  #1
Sep 7th, 2007
Hi, I'm trying to write a graphing application, that takes an equation and graphs it, and thats no problem. However, I want to add a cursor, which moves along with the mouse. I managed to add one, however, the cursor causes everything else to refresh too. Is there anyway to move just the cursor, keep the other contents from getting refreshed?

I've included an example of my problem in the provided source code. In example, the line in the background refreshes with the cursor, because it is in one paint() method. How can I make the background independent from the cursor?

Thanks.
Last edited by thenava; Sep 7th, 2007 at 2:08 pm.
Attached Files
File Type: java Grapher.java (654 Bytes, 7 views)
File Type: java graphWriter.java (936 Bytes, 7 views)
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,510
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Need help with repainting Graphics

 
0
  #2
Sep 7th, 2007
You really don't need to. Just be sure that you are doing expensive calculations and such outside of the paint() method and only painting the results. Paint will be called any time the OS needs to update the screen (ie moving, resizing, etc) so try to keep it as lean as possible. If you want to play with buffered image painting, that is one way to keep a copy of what you have already drawn and then draw over it as needed, but for your small example I don't think you need to do that.

You may also want to consider overriding paintComponent() of your JPanel instead of paint(), since paint also paints the borders and child components. paintComponent() is more narrowed to the area you are wanting to render.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 6
Reputation: thenava is an unknown quantity at this point 
Solved Threads: 1
thenava thenava is offline Offline
Newbie Poster

Re: Need help with repainting Graphics

 
0
  #3
Sep 7th, 2007
So would creating a buffered image and painting the cursor on top of that be like creating a layer on top of an image in Photoshop, and drawing on the new layer?

Thanks.
Last edited by thenava; Sep 7th, 2007 at 2:52 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,510
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Need help with repainting Graphics

 
0
  #4
Sep 7th, 2007
Originally Posted by thenava View Post
So would creating a buffered image and painting the cursor on top of that be like creating a layer on top of an image in Photoshop, and drawing on the new layer?

Thanks.
Yes, somewhat like that. It allows you to draw to an offscreen image and then render that image to the screen. In your case the line would be the image and your paint code would drawImage() and then draw your lines.
http://java.sun.com/docs/books/tutor...ges/index.html

In your case though, I don't think you will really gain anything with the extra trouble. Keeping a BufferedImage offscreen is helpful if you have a very complex image that you don't want to have the expense of regenerating with each paint. Your line drawing doesn't really have that issue here.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,510
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Need help with repainting Graphics

 
0
  #5
Sep 7th, 2007
This would be your code with an offscreen image for the line
  1. public class graphWriter extends JPanel {
  2. private int
  3. markerX = 0,
  4. markerY = 0,
  5. width = 0,
  6. height = 0;
  7.  
  8. Image bufImage = null;
  9.  
  10. public graphWriter() {
  11. addMouseMotionListener(
  12. new MouseMotionListener() {
  13. public void mouseDragged(MouseEvent e) {
  14. }
  15.  
  16. public void mouseMoved(MouseEvent e) {
  17. //Get coordinates of the mouse
  18. markerX = e.getX();
  19. markerY = e.getY();
  20. repaint();
  21. }
  22. }
  23. );
  24. }
  25.  
  26. public void paintComponent(Graphics g) {
  27. super.paintComponent(g);
  28. if (bufImage==null){
  29. bufImage = createImage(getWidth(), getHeight());
  30. Graphics gBuf = bufImage.getGraphics();
  31. //Draw line
  32. gBuf.drawLine(0, 0, getWidth(), getHeight());
  33. }
  34.  
  35. //Get width and height of current clip
  36. width = (int)g.getClipBounds().getWidth();
  37. height = (int)(g.getClipBounds().getHeight());
  38.  
  39. //Draw line
  40. g.drawImage(bufImage,0,0,this);
  41.  
  42. //Draw two lines, one horizontal, and one vertical to create the cursor
  43. g.drawLine(markerX, 0, markerX, height);
  44. g.drawLine(0, markerY, width, markerY);
  45. }
  46. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 6
Reputation: thenava is an unknown quantity at this point 
Solved Threads: 1
thenava thenava is offline Offline
Newbie Poster

Re: Need help with repainting Graphics

 
0
  #6
Sep 7th, 2007
Well, im hoping to apply that logic to another program (picture), and i think it would be much more efficient if only the cursor were redrawn, and the rest were left alone. As it is right now, each stock price is an object, and they all have to be redrawn along with the bands, and the volume, and everything else.
Attached Thumbnails
actual.png   concept.png  
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 6
Reputation: thenava is an unknown quantity at this point 
Solved Threads: 1
thenava thenava is offline Offline
Newbie Poster

Re: Need help with repainting Graphics

 
0
  #7
Sep 7th, 2007
Thank you very much Ezzaral.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,510
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Need help with repainting Graphics

 
0
  #8
Sep 7th, 2007
Originally Posted by thenava View Post
Well, im hoping to apply that logic to another program (picture), and i think it would be much more efficient if only the cursor were redrawn, and the rest were left alone. As it is right now, each stock price is an object, and they all have to be redrawn along with the bands, and the volume, and everything else.
Ah, ok, yes that is a bit more to render. The offscreen image may work pretty well for you then. For complex lines, you may also want to look at using drawPolyline() from prebuilt x[] and y[] coord arrays. That made a big difference for us on a few of our many-lined graphs here.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 6
Reputation: thenava is an unknown quantity at this point 
Solved Threads: 1
thenava thenava is offline Offline
Newbie Poster

Re: Need help with repainting Graphics

 
0
  #9
Sep 7th, 2007
Thank you once again!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 2272 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC