943,865 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3698
  • Java RSS
Sep 7th, 2007
0

Need help with repainting Graphics

Expand Post »
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.
Attached Files
File Type: java Grapher.java (654 Bytes, 40 views)
File Type: java graphWriter.java (936 Bytes, 37 views)
Last edited by thenava; Sep 7th, 2007 at 2:08 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thenava is offline Offline
6 posts
since Sep 2007
Sep 7th, 2007
0

Re: Need help with repainting Graphics

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Sep 7th, 2007
0

Re: Need help with repainting Graphics

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thenava is offline Offline
6 posts
since Sep 2007
Sep 7th, 2007
0

Re: Need help with repainting Graphics

Click to Expand / Collapse  Quote originally posted by thenava ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Sep 7th, 2007
0

Re: Need help with repainting Graphics

This would be your code with an offscreen image for the line
Java Syntax (Toggle Plain Text)
  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. }
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Sep 7th, 2007
0

Re: Need help with repainting Graphics

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
Click image for larger version

Name:	actual.png
Views:	29
Size:	55.3 KB
ID:	4036   Click image for larger version

Name:	concept.png
Views:	27
Size:	31.2 KB
ID:	4037  
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thenava is offline Offline
6 posts
since Sep 2007
Sep 7th, 2007
0

Re: Need help with repainting Graphics

Thank you very much Ezzaral.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thenava is offline Offline
6 posts
since Sep 2007
Sep 7th, 2007
0

Re: Need help with repainting Graphics

Click to Expand / Collapse  Quote originally posted by thenava ...
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Sep 7th, 2007
0

Re: Need help with repainting Graphics

Thank you once again!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
thenava is offline Offline
6 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java graphics 2D and 3D
Next Thread in Java Forum Timeline: Jdbc...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC