Shared variable for other classes

Solved the initial question, however posted a new one in the fourth post if you would bother to scroll down and see if you know the answer! :) Thank you!
Direct link to new question:Shared variable for other classes

____________
Original post

In my effort of trying to make my program more tidy and clean I've encountered a problem. Previously I've only coded my programs in one big mess, so now I'm trying to split it up into separate classfiles.

I'm trying to make a simple paint program, where I'm trying to split it into classfiles as to what makes sense to me. I might have tried to do it in a very difficult way, and I appreciate any tips beyond my problem, the thing I can't quite figure out however is how I can efficiently share variables between my classes.

I've divided my program into the following java files:mainProgram
- Contains only one line of code generating the GUI which is in a seperate file.

gui
- Contains the GUI and actionlisteners, as well as the mouselistener which I want to get the coordinates from.

settings
- Contains information about what the user has chosen. Currently there is selection of shape, fixed sizes, fill or just the "edge" and color. This also contains the xStart, xEnd, yStart and yEnd variables which I want to pass from gui into the drawing file.

drawingPanel
- Contains the functions for drawing.

The problem is however, when I do make a new instance of settings in each file they do not share variables obviously.

So when I in the gui class after having instanced

Java Code:

settings s = new settings();

in both files, they do not share the same variables. This might be obvious, but I thought it was going to work :)

I then do in the gui file a

Java Code:

s.setxStart(event.getX())

for each of the variables. This ends up with the instance of settings in the gui file containing the right value but obviously not in the drawingPanel file.

How do I pass variables from one class file to another? Can I make like a public double xStart which all classes can access and edit?

Thank you for answers, and sorry if my english isn't that good. If anything is unclear please ask and I'll try to clarify.
- Cemi

I am trying to make it retain the items I have painted when I resize the window, however when I make a "componentListener" which listens to when the JFrame is resized, the for loop runs and is supposedly painting all the shapes which I store in an ArrayList, but there is nothing to be shown. If I do however put it in my paintComponent function it will paint all the shapes next time I paint a new shape after resizing the window. If I do this however, it paints all the shapes every time I paint a shape, which makes all shapes grow in thickness each time, and I asume it's unnecessarily taking up resources.

Is it because a resize listener will repaint before the window updates, thus the shapes not beeing shown at the JFrame? Where do I have to put it if so?

I recommend that you do your painting from within the paintComponent method (exceptions if you are using a BufferedImage for the stable portions of the image). Are you calling super.paintComponent(g) as the first method of your paintComponent override? If not, doing this will likely prevent the side effects you are seeing. Best of luck.

The problem with doing it in the paintComponent function, in addition to the lines getting thicker is the fact that I can't see the rectangles after resizing until I paint another shape. If I call it in the resize listener the same thing as calling the paintAll function at the moment happens, it writes out my test message which confirms that the for loop is running, yet nothing is displayed.

Which is what I find weird, the for loop is functioning since the shapes are all painted if I paint another figure and it runs the paintComponent once again. However the for loop is ran upon resizing, yet nothing is displayed until I paint another figure.

This is my code for the paintComponent and the function I intend to use to paint everything. I do in addition have a pain function which makes new objects based on input from the users mouse actions and other settings which is configureable.

Perhaps you will want to create a small compilable program that demonstrates your problem, an SSCCE. Please see my link on this below. Then you can post it here and we can play with working (and small) code.

I would be happy to, but sscce.org seems to be down, according to It's not just you! it's not just me.

I've noticed that if I add the componentListener which listens to when the resize happens to the panel instead of the JFrame, the circles are repainting while I move it. But when I release the mouse they aren't there. Just as if my listener is doing the action while it is being resized instead of after as I intend.

However I've attached the full Eclipse project, which should be quite easy to import as far as I've experienced. In the current version it runs the for loop when the window is resized in the paintAll() function and not in the paintComponent. So now it does not repaint all the squares when you paint another figure since the for luck does not run when you do.

I do know about other flaws and errors, like not being able to paint figures if you move your mouse in negative directions etc, as it is work in progress I'm just doing one thing at a time as I figure out the Java 2D painting stuff :)

I hope this can be as helpful for you to help me as an SSCCE even though I don't know what that is yet.

Your painting code is all wrong (sorry to be blunt) and this is what is forcing you to create strange kluges to fix your problems. For example, you're extracting the Graphics object from the JFrame via getGraphics and drawing directly on that in your paintAll method. This has two bad effects: the Graphics object obtained from getGraphics is not stable requiring you to recall this method every time you resize, and by drawing directly on the JFrame, you lose many advantages that the Swing libraries use for doing graphics. You would do well to check out the Sun tutorials on 2D Graphics to show you how to do this right.

Here's a simple example (which is kind of what an SSCCE is) of using paintComponent:

By my painting code I asume you mean the code in the class tegnePanel which contains paintComponent etc.

I do find that weird as I've been following the book "O'Reilly - Java 2D Graphics" when it comes to painting stuff. I'll try to start over again and try to simplify my code and see if I can make it work :)

By my painting code I asume you mean the code in the class tegnePanel which contains paintComponent etc.

Yes.

I do find that weird as I've been following the book "O'Reilly - Java 2D Graphics" when it comes to painting stuff.

But does that book have you extracting the Graphics object from a JFrame via getGraphics? Does the book have you override both paint and paintComponent methods in a JPanel and then call paint from within paintComponent?

If so, throw the book out, but I kind of doubt that it does this.

As another example, here's one way to do the same graphics I did previously, but using a BufferedImage:

But does that book have you extracting the Graphics object from a JFrame via getGraphics? Does the book have you override both paint and paintComponent methods in a JPanel and then call paint from within paintComponent?

I asume it does not then, if it really is that dumb. Haha :rolleyes:

I'll try to study you're examples and the examples in the book once again, I've been more concerned with making it work at all than making it work properly I guess.

I do however find it weird to never call paintComponent, but I guess it just updates constantly? :s

No, that method is called whenever the EDT (Event Dispatch Thread) finds it necessary that your component needs repainting. This is called 'passive painting', i.e. your component repaints itself (through the paintComponent( ... ) method) when it is asked to do so; you can only ask the EDT to repaint a component (once in the near future) by calling the repaint() method and even then the EDT may decide that it's not necessary to repaint that particular component.

The paintComponent( ... ) method is called ever so often (e.g. when the component is resized or exposed from behind another component or moved to another position on the screen) so it should be fast; it should just repaint the component and not perform long winding program logic or install things or open files or whatever; it should paint itself, that's it.

Also it is always good to know that you can only suggest that it be called via repaint(). The paint manager may decide (if for example the repaint calls are right next to each other), to ignore a repaint call. And further the OS may request a call to paint the app, and this is something that you have no control over. So in other words, your paintComponent method is usually called when you request it be called (via repaint), but not always, and can be called outside of your control. One reason that this is important is that you should never place program logic in the paint or paintComponent methods.

A very useful article that goes into greater detail on the paint manager and all the above can be found here: Painting in AWT and Swing

Thanks for the link, understand the actual painting of the graphics alot better now.

However I do have another question, I searched google but I couldn't find an answer which could help me. Hopefully this is just a small coding error or something, and not something requiring me to post my entire new code or rewriting the program :)

I am adding a toolbar to simplify actions instead of going through the menu bar each time the user wants to change something. I have made JTextFields for Width and Height along with a checkbox for Fixed Size if the user wants to create a fixed size Rectangle or Ellipse.

When I am trying to add an actionlistener to the textFields I get the error that
"Cannot refer to a non-final variable widthField inside an inner class defined in a different method"

The code is all within the constructor, the JTextField is initiated normally and the actionlistener looks as follows:

How can I fix this? I've checked the offical JTextField examples, and tried copying the intializing of the textField and actionListener and altering the variable names to fit mine, still I get this error.