File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Novice graphics question

 
Dennis Noren
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using paintComponent within a class that extends Jcomponent (using Java2 1.4.2). I'm using graphics2D by:

public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
...

I can access all the advertised methods from there. However, I need to build some compound objects with common structure, but vary the characteristics of elements within the objects, for example, different colors, linetypes, rotations, etc. I know how to use GeneralPath, Area, intersect, add, etc. to manipulate these elements, but I need to create many of these compound objects with the flexibility of making the separate elements within the objects look different as needed. The first thing I think of is to build methods with arguments for what I want to vary. However, within those methods I cannot use the methods that manipulate the graphics object, apparently, and I that isn't thinking the OO way, methinks.

Is the use of an inner class advised here? I know that gives me access to data and instances of the surrounding class. Or is it better to use an external class, and if so, can I pass a reference to be able to access the graphics object? I'm not quite sure how to go about this and haven't been able to find examples of it.

Yes, I have taken intro classes in OOA and OOD, and an intro Java class, but it doesn�t all stick at once. ;-)

Thanks for any help you can offer.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dennis,

Welcome to JavaRanch!

The general shape of the solution would look like this:



Note that instead of creating your own interface MyShape, you can actually have your shapes be JComponents themselves and extend JComponent!
[ January 07, 2005: Message edited by: Ernest Friedman-Hill ]
 
Simon Hunt
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Earnest

Just a quick "thank you". This post solved an embarassingly simple problem I was having with trying to output a group of objects to a Jpanel, whilst trying to keep their graphics behaviour inside the objects.

The problem: I was only able to see one object at a time (the last one). This post finally got me to realise that I needed to be painting them all on the _same_ panel. :roll:

Cheers!

Simon*
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic