DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Painting from a Graphics object?

  1. #1
    Join Date
    Nov 2005

    Painting from a Graphics object?

    I'm having trouble getting my head around Graphics objects and the paint method. Here's my predicament:

    I have a class called DrawSurface which extends JPanel. The only thing that I changed was the paint method which I will use to draw on the DrawSurface. I have a bunch of coordinates that I want to draw onto the DrawSurface. It's not all going to fit on the screen, so I want to use a scrollbar to pan along the drawing. Is there a way that I can draw the entire drawing onto an offscreen Graphics object and then display a portion of that Graphics object on the DrawSurface depending on where the scrollbar is? I know that it's possible to draw a portion of an Image object onto a Graphics object, but what about Graphics to Graphics. Or is there an easier way that I'm overlooking?

  2. #2
    Join Date
    Nov 2005
    Wow, why is it always that right after I ask a question I end up figuring it out?

    I created a BufferedImage object called img and created a Graphics object using img.getGraphics(). Whatever I draw on the Graphics object gets added to the BufferedImage and then I can use drawImage to paint a portion onto another Graphics object.

    Confusing, but I guess it makes sense.

  3. #3
    Join Date
    Nov 2005
    Another question, might as well post it in the same thread.
    Is it possible to specify exact width/height of characters that are being drawn to a graphics object? The only method I can see of altering size is the setFont method, but that only lets you do font size.

  4. #4
    Join Date
    Nov 2005
    Well I didn't find how to set the width of a font, but I found how to find the length of a string in pixels. I just used the FontMetrics class. I found out how to do it here:

  5. #5
    Join Date
    Feb 2006
    Cologne - Germany
    by the way: it's a nice approach to do make an big offscreen image and just show a clip.
    but why don't you use the JScrollPane? It just suports that. And has scrollbars included already

  6. #6
    Join Date
    Nov 2005
    I was thinking about doing it that way, but this worked out fine.

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2006, 12:34 PM
  2. Graphics object again
    By Michael Culley in forum .NET
    Replies: 5
    Last Post: 05-06-2002, 02:25 AM
  3. What is the graphics object
    By Michael Culley in forum .NET
    Replies: 1
    Last Post: 05-05-2002, 12:02 PM
  4. Validating XML
    By Jaco de Villiers in forum XML
    Replies: 1
    Last Post: 06-01-2001, 05:50 PM
  5. Re: COM object Password Security
    By Tom Shreve in forum Enterprise
    Replies: 0
    Last Post: 04-07-2000, 08:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center