wood burning stoves 2.0
The moose likes Swing / AWT / SWT and the fly likes Painting from one class to another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Painting from one class to another" Watch "Painting from one class to another" New topic

Painting from one class to another

Tom Rodrigo

Joined: Dec 07, 2001
Posts: 26
Hello all,
I have two classes in the same package. Class MyDialog shall create a dialog in my application and request a drawn String for one of its Panels. The String shall be drawn by my second class, called PaintString. I am unable to connect the two classes.
Maybe it is easy, but as I am a newbie, I don't get it...
Any help is appreciated.
Here is the code [I think the problem is in my second class]:

By the way: Could you please give me a feedback concerning my code writing? Do I make severe mistakes??
What about my initialize-try? Do I need a System.exit(0) in every dialog or only in my main frame [MyDialog is part of another frame]?
Please help!
Thanks in advance!!!
Tom Rodrigo
[ July 24, 2002: Message edited by: Tom Rodrigo ]
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
hello Tom,
1.) Why don't you use a JLabel to paint the String on the Panel?
2.) if you extend JFrame than don't call the subclass PaintString. This name sounds more like a subclass of String if ever. And actually I dont understand why you subclass JFrame anyway. Object Orientated Programming doesn't mean to inherit anytime. use more composition, maybe even with MyDialog.
3.) intialize() is a fine thing. often you will see a method called initComponents() that will do similar things. if you have such a method, then use it to initialize your components! your just adding them to you container inside this method but you are initializing them in the local scope.
most or even all of these components could just reside inside initialize() you don't have to declare them local. if you do make them private. no one outside an instance of MyDialog needs to see what components MyDialog actually contains. that's an implementation detail.

4.) this is Java, not C#
make your method, variable and package names start with non-capital letters. this common java convention and everybody is used to it. only class, interface names and Constructors start with a capital letter.
5.) this is only my experience: I never have used JDialog, I am only using JFrame (or JDesktopPane/JInternalFrame) for the application and JOptionPane for creating dialogs for user interaction.
maybe someone can throw some arguments in for using JDialog?
have fun with java!
Tom Rodrigo

Joined: Dec 07, 2001
Posts: 26
Hello Chantal,
thank you for your suggestions!
As you and all the others will see, I have edited my recent post and corrected my coding. Also my problem has changed, I am getting a NullPointerException now...
1.) myDialog and myPainter (formerly PaintString) are part of a larger project. Actually I don't want to paint only the string, but also some arcs and polygons, so I can't use a JLabel.
2.) corrected.
3.) Sorry, I don't understand what you mean. Could you explain it a little more, please? [perhaps with a code-sequence?] I do not have initComponents() and I don't know what to declare private... I've read some java books about this, but I'm not quite sure if I really understood the things about initialization and private declarations...
4.) corrected.
5.) I am using JFrame only for the main window of my application and JDialog only for the smaller windows in this app.
Many thanks for your help, Chantal!
Any ideas about the Exception?
[ July 24, 2002: Message edited by: Tom Rodrigo ]
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
concerning the NullPointerException:
go to the line in the code where the NullPointerException is thrown. this is the first line given in the StackTrace. the line will contain some method invocation like:
the refToObject points to null thats why the methods call fails and a NullPointerException is thrown. the object you reference in this line is not initialized yet or has been set to null after initialization. look through your code, I am sure you will find out fast why this is so.

concering initialize():
(I just suggested to rename initialize() to initComponents() as this is a very conventional name for this kind of method, but initialize() will do fine, don't think to much about it)
what I mean by "initialize the components at least with private access in the local scope or even only inside initialize" is:
a) what I mean by initialize is basically to call a constructor and point a reference to it, like JPanel panel = new JPanel(). (there's certainly more to it, but it will do for now.) declaration is just the first part of this statement: "JPanel panel" (you declare that the variable panel holds a reference to a JPanel.) Don't be offended if I am telling you things you already now, I just want to be sure that you know what I mean with the terms I use.
b) if a method uses some object internally and this object is *never* used or needed outside this method you don't have to declare and even less initialize it outside of this method. this helps the garbage collection to collect unused objects as fast as possible, and - certainly this should be the more important point - adds to a more object oriented program as it encapsulates the implementation details inside the method.
c) here the sample code:

you see, this is basically your code, I just put the initialization into initialize(). the code you presented did the initialization outside initialize().
concerning the painting problem:
do you know this site?
I haven't done any painting on components yet. the tutorial will provide you better information and sample code.
have fun!
Tom Rodrigo

Joined: Dec 07, 2001
Posts: 26
Hello Chantal,
I got it working!
I've renamed initialize() to initComponents() and put the initialization into it, as you suggested.
I've also fixed the NullPointerException, so everything works fine now.
Here's the relevant code-sequence:

I think the problem was that I was expecting something like painterPanel.add(MyPainter.MyMethod()) [which should be explicitly called up and returns a graphic object and adds this to the panel], but I suppose, I just have to overwrite paintComponent() and add the whole class-object to painterPanel...
[Maybe I'm using the words not in the right context, but I'm sure you know, what I mean...]
So everything is fine now and this is how I feel:
Thanks to you, Chantal! You really helped me.
I agree. Here's the link: http://aspose.com/file-tools
subject: Painting from one class to another
It's not a secret anymore!