• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Novice graphics question

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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*
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic