• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a way to globally change a variable for many classes?  RSS feed

 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I mean is lets say I have a variable called zoom and it's at 100. If I have a class that calls the other classes of information(rectangles, circles, etc) of shapes, and then when you change the zoom in the main class it would change it for all the other classes.

I figure something like this would suffice



Note I just made this code up, but hopefully this makes sense... I figured their might be an easier way. Someone I spoke to who uses VB said that they have a "Global" for VB's "Classes" so I was curious if there was something of the same or anything?


I would appreciate any help, thanks,

~JO
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to say, but this is an incredibly bad idea. Your classes become very tightly coupled, and they're not really cohesive.

Instead, when you're painting your shapes, multiply their values by the zoom value that's located in whatever component that's responsible for painting them. This way, you only have to change one variable, it doesn't need to be global, and you can even have different views of the same model with different zooms.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Sorry to say, but this is an incredibly bad idea. Your classes become very tightly coupled, and they're not really cohesive.

Instead, when you're painting your shapes, multiply their values by the zoom value that's located in whatever component that's responsible for painting them. This way, you only have to change one variable, it doesn't need to be global, and you can even have different views of the same model with different zooms.


Well the paint routine is in each shape's class, so I wanted to do the zoom there and do like drawRect(x*zoom,y*zoom,w*zoom,h*zoom); within the class itself, instead of the main class.....
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Orsaw wrote:
Stephan van Hulst wrote:Sorry to say, but this is an incredibly bad idea. Your classes become very tightly coupled, and they're not really cohesive.

Instead, when you're painting your shapes, multiply their values by the zoom value that's located in whatever component that's responsible for painting them. This way, you only have to change one variable, it doesn't need to be global, and you can even have different views of the same model with different zooms.


Well the paint routine is in each shape's class, so I wanted to do the zoom there and do like drawRect(x*zoom,y*zoom,w*zoom,h*zoom); within the class itself, instead of the main class.....


Are you using Inheritance kind of thing???
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Orsaw wrote:Well the paint routine is in each shape's class, so I wanted to do the zoom there and do like drawRect(x*zoom,y*zoom,w*zoom,h*zoom); within the class itself, instead of the main class.....


So how are you painting the shapes exactly? Do you pass your graphical component's Graphics object to these shapes and let them paint themselves using that Graphics instance?

In this case, I recommend passing the zoom value as an argument to the shape's paint method, or have the component transform() the Graphics2D object before passing it to your shapes. In the latter case, you can "shrink" the Graphics object first, so drawing takes up more space, and then when the shapes are done painting, you restore the old AffineTransform of the Graphics object.

However, a better option may be to use AWT's predefined Shape classes. java.awt.Shape provides implementations such as rectangles, ellipses, round rectangles, and more complex polygons. You can then use AffineTransform to scale these shapes before you paint them on your component.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you clone your Graphics2D object with its create() method before using AffineTransforms. Scale and translate and not too bad, but rotate and shear can introduce changes into the Graphics object which can never quite be undone.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
R. Jain wrote:
Jay Orsaw wrote:
Stephan van Hulst wrote:Sorry to say, but this is an incredibly bad idea. Your classes become very tightly coupled, and they're not really cohesive.

Instead, when you're painting your shapes, multiply their values by the zoom value that's located in whatever component that's responsible for painting them. This way, you only have to change one variable, it doesn't need to be global, and you can even have different views of the same model with different zooms.


Well the paint routine is in each shape's class, so I wanted to do the zoom there and do like drawRect(x*zoom,y*zoom,w*zoom,h*zoom); within the class itself, instead of the main class.....


Are you using Inheritance kind of thing???


I was thinking of possibly using an interface full of methods that each would inherit and could use differently, but I'm not 100% sure on my path ATM....

Stephan van Hulst wrote:
Jay Orsaw wrote:Well the paint routine is in each shape's class, so I wanted to do the zoom there and do like drawRect(x*zoom,y*zoom,w*zoom,h*zoom); within the class itself, instead of the main class.....


So how are you painting the shapes exactly? Do you pass your graphical component's Graphics object to these shapes and let them paint themselves using that Graphics instance?

In this case, I recommend passing the zoom value as an argument to the shape's paint method, or have the component transform() the Graphics2D object before passing it to your shapes. In the latter case, you can "shrink" the Graphics object first, so drawing takes up more space, and then when the shapes are done painting, you restore the old AffineTransform of the Graphics object.

However, a better option may be to use AWT's predefined Shape classes. java.awt.Shape provides implementations such as rectangles, ellipses, round rectangles, and more complex polygons. You can then use AffineTransform to scale these shapes before you paint them on your component.


Basically I was going to create my own class and my first class extends Rectangle2D.Float and most likely the same with the other classes..... Each of those classes would have x,y,w,h and then another variable called zoom. when painting the parameters are x*zoom,y*zoom, etc. The zoom will stay at the current zoom until I decide to zoom in farther, or zoom out.

I'll have to check out the AffineTransform, but I think it might be too much from what I'm trying to do... At least I figured what I was doing should be simple....


EDIT: Yeah looks like it's a lot more than I need, but Idk...


http://docs.oracle.com/javase/tutorial/uiswing/painting/refining.html My class follows this example at the simplistic level, just wanted to show how my paint and such is set up like the "RedSquare" class with the paint and such....
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but you'd have a lot of variables that all essentially store the same value.

Instead, if you don't want to use the AffineTransform class, I suggest you just add a zoom parameter to your equivalent of the paintSquare() method. You can then multiply the bounds with the parameter, instead of a member variable (which is much more brittle).

Whatever component you use to paint all the shapes can then just pass its own zoom value to all the paintShape() methods. You then only have to change this one variable and it will be reflected in all the shapes when they are painted.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Yes, but you'd have a lot of variables that all essentially store the same value.

Instead, if you don't want to use the AffineTransform class, I suggest you just add a zoom parameter to your equivalent of the paintSquare() method. You can then multiply the bounds with the parameter, instead of a member variable (which is much more brittle).

Whatever component you use to paint all the shapes can then just pass its own zoom value to all the paintShape() methods. You then only have to change this one variable and it will be reflected in all the shapes when they are painted.


Ah so like

I don't mind using it, but it seems like it might cause issues according to Campbell? I like the idea of the extra parameter tho...
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, Campbell said no such thing. He was referring to transforming a Graphics object with an AffineTransform. He meant that you need to restore the original Graphics object after your component is done with it, which is easiest by not changing at all and transforming a copy of it instead. This has nothing to do with passing a zoom argument around though.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:No, Campbell said no such thing. He was referring to transforming a Graphics object with an AffineTransform. He meant that you need to restore the original Graphics object after your component is done with it, which is easiest by not changing at all and transforming a copy of it instead. This has nothing to do with passing a zoom argument around though.


I was talking about AffineTransform.....
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seem to be having an issue when I try to repaint....

My paint class is as follows



I have setRect commented out, because I have a question about it.... My class Extends the Rectangle2D.Float class, and for some reason whenever I had to paint I always had to setRect to whatever my x,y,w,h was, or else there was an issue painting on the screen... Why is that?

I am trying to drag the shapes, but when I do it either disapears or is very tiny... When it's tiny the code is or else it just goes away

Again using the code exactly like the one on the trial, except multiple instances, not 1.
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! Your post was a bit ambiguous.

What you can do is the following:
 
Stephan van Hulst
Saloon Keeper
Posts: 7932
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And in response to your second post; why don't you post an SSCCE that displays exactly the problems you're having?
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Oh! Your post was a bit ambiguous.

What you can do is the following:


Yeah I'm thinking the parameter thing is much easier, and I have to scale a lot of things, so this will work fine....

I'm trying to take my code and rework it, because there is a lot of extra stuff, so I will post that when it's done...
 
Pablo Abbate
Ranch Hand
Posts: 30
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have seen your question very quickly ....

I think that you should use the observer pattern. Every object (shape, circle,etc) subscribes to a (lets say) WindowManagerObject. And when you change the zoom (or what ever property you want) the other objects get notified and can do whatever they have to do.

Hope it helps.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pablo Abbate wrote:I have seen your question very quickly ....

I think that you should use the observer pattern. Every object (shape, circle,etc) subscribes to a (lets say) WindowManagerObject. And when you change the zoom (or what ever property you want) the other objects get notified and can do whatever they have to do.

Hope it helps.


Sounds interesting, is that a class?

I think the way I do it is simple, though I am having an issue like I did in this thread: http://www.coderanch.com/t/579456/GUI/java/Repainting-clipping-area


also I said I was going to post my code, so here:



The face class is just an upgrade of a Rectangle. I extend Rectangle2D.Float to be able to use Contains(For the next class I will post) and originally was used for painting faces before I was told/saw in the Oracle trail to paint in the "Face" class itself.




This is the class that draws the Face class rectangles onto the screen... PlanHead just reads in the values, as you can see they are floats originally, but then I convert them so that i can use them for the repaint and such....(which might be where the error lies).... When I repaint using x1,y1,w1,h1 I get a tiny rectangle(the size without the zoom) that will delete the area once it's dragged over; however it's a gray square "color the background..." When I do it x1*zoom,etc I get a giant sized flicker and that's all... It should just be x1,etc because zoom is multiplied in the paint method(which i'm not sure is a good idea or not)... But it stays normal sized... When I do x1*zoom, etc, then it becomes giant as if it's x1*zoom and then later that *zoom again in the face repaint, so I'm not sure what's going on....

So 2 things... how do I make it "normal sized" with the rest of the painted rectangle Faces, and 2 how to get my color to Blue/White like normal when dragging? Why does it go gray like the background?

http://docs.oracle.com/javase/tutorial/uiswing/painting/refining.html

This tutorial shows basically the same as I have done, but why am I having issues? Thoughts

Thanks,

~JO

Edit: Weird I noticed that in this case it doesn't even show a rectangle, it's just invisible... Weird........ Maybe it's because I changed from paint to paintComponent.... Maybe that was transparent while the Jframe is gray? It's obviously painting the background..... Just don't know why/how...
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it seems like *zoom is needed when repainting. It will black out, but if I draw it over the coordinates that are the normal x,y,w,h without * zoom it will appear in the current "zoomed" position... Really weird.... Why would adding zoom make it only appear over normal coordinates and without it stay zoomed but a smaller rectangle... WTF....
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

According to your original question , I guess you can delare a public static variable in a class. Then you can access that variable using <classname>.variable_name in any class within the same application.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!