File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Novice graphics question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Novice graphics question" Watch "Novice graphics question" New topic

Novice graphics question

Dennis Noren

Joined: Jan 07, 2005
Posts: 15
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.

Java novice, J2SE 1.4.2<br />Some C, some OOA/OOD
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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 ]

[Jess in Action][AskingGoodQuestions]
Simon Hunt

Joined: Jul 17, 2006
Posts: 11
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:



"I was born in a water moon..."
The Algebraist by Iain M. Banks
I agree. Here's the link:
subject: Novice graphics question
It's not a secret anymore!