aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes AWT: Painting buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "AWT: Painting buttons" Watch "AWT: Painting buttons" New topic
Author

AWT: Painting buttons

Fred Wagner
Greenhorn

Joined: Jan 07, 2010
Posts: 5
Hi,

I am trying to find out how I can add buttons to my programme. It is a programme that calculates and paints coordinates. At the moment the variables are constants, but I would like in the end that one will be able to click on the button "m:15" and then the calculations are performed with m=15. Or even better with a textarea where personal numbers can be added.


This is my very first java programme and I have no clue about how to add the buttons. I have posed this question on the AWT/SWING subforum of the java-forums.org already - however nobody has answered yet (excuse the missing of a link to the thread, but the site seems to be offline right now).





Above you can see my attempt to create a seperate button class, and then give that out in the frame. It just won't work. (There are so many error messages that I won't post them, but if they are wished-for I will post them)

I really hope somebody will help me out. Thank you in advance.


chappa
R Johnson
Greenhorn

Joined: Jan 05, 2010
Posts: 28

Your compile problem is Frame2 takes a String Argument in the (). So you can't' just call Frame2().... you have to call Frame2("STRING"); Also I cannot get it to compile with



your Fram2() call on line 19 needs a String argument though
Fred Wagner
Greenhorn

Joined: Jan 07, 2010
Posts: 5
Hi Mr. Johnson and thank you for your reply,



I have cheated my way around it by using the code of somebody else who had 2 buttons implemented already. My problem now is to figure out how can I make it so that my buttons (and future textareas) will change the value of the variables in the programme?



If you run this you will see 2 buttons and something painting. My intention is now that I can change the variables via a button (and later also a textarea). How do I do that?


Any help is greatly appreciated.


Thanks.


Chippa


PS: this is the link which leads to the thread in the other forum, for those who are interested : Link to the other Thread (other forum/website!)
R Johnson
Greenhorn

Joined: Jan 05, 2010
Posts: 28

This is just some code to show an example of how a button works. You must add an action/selection listener to the button and then define what happens in that action. It can be anonymous which basically means it's declared when added and has no name value to reconstruct or can be defined as a class that implements the action and then the class can be called into your project like any other class and used with a defined variable. Here's an example simple and sweet.

Fred Wagner
Greenhorn

Joined: Jan 07, 2010
Posts: 5
Thank you again Mr. Johnson,


I have found the way to make the button print out "Hi" on pressing. However I still can't figure out how I can make it "erase" my old variable (f.e. m1) and use the one which the button is labelled with. (F.e. the button says m1 = 1500 and then the calculations are done with 1500 instead of the preset 6000)

Also is there a function that will just "clear" my current painting, so that it can be performed with the new variables instead of the default ones?

The current code looks like this:




Help is again greatly appreciated.


Thank you


chippa
R Johnson
Greenhorn

Joined: Jan 05, 2010
Posts: 28

If you want to change a variable like then you need to declare it as a class/instance variable. Basically outside the method of paintComponent. This should allow you to change m1 just by calling it.



Fred Wagner
Greenhorn

Joined: Jan 07, 2010
Posts: 5
I changed my code as you showed, but now I get this error message:


R Johnson
Greenhorn

Joined: Jan 05, 2010
Posts: 28

I was hoping to wait for someone with more experience to comment mainly because of your static keyword before the Circle class. Also I'm self taught and by no means a java expert. However I can "make" things work. So as my intentions might be pure my programming might not and I was hoping not to pass on any bad habits but here's how I'd make a quick fix. First I want to explain why it can't access the m1 variable. There is no reference to it. The class a() has no idea where m1 is coming from because it has not been declared. Because the Circle class is static I suspect, but have not tested, that if you do call the Circle class and change m1 you will change it or all Circles however you will be unable to repaint the one residing in your JFrame so you need to point to the Circle that your displaying in your JFrame. Currently your code Circle circ is Empty/Null so you will have to call new Circle() before you can reference the object circ. Then you need to make a parameter for myButtonListener(Circle) so that you can pass that exact Circle to the class and alter it's attributes. After that it compiles and works fine for me.



Fred Wagner
Greenhorn

Joined: Jan 07, 2010
Posts: 5
I can only repeat myself again: thank you so much Mr. Johnson.


I have found a strange issue to occur, on my brother's desktop (windows) my planets draw all at once, on my own box it paints step by step... is this intended?

is there a way to change it so that it is painted step by step instead of all at once?








Thanks again,



Fred
R Johnson
Greenhorn

Joined: Jan 05, 2010
Posts: 28

Computers take code and execute it as fast as it possibly can so on different computers will often have different results though often it's not noticable. Even different OS's will have different run times. So no it's not intended to be step by step. On my computer with the codes you've given the results are instantaneous. The only way that I would know how to ensure some lapse of time (again different machines different results) would be to create a thread that draws each planet. I'm not sure what we're calling planets I'm assuming the red line is one and the blue line is the other. In BASIC all you had to do to slow things down was create a large loop, not a good idea in Java. So the only way I could see is a rewrite of the program to seperate the lines perhaps create a class called planets that holds all a planets variables then in the run you could populate the variables and call repaint after each one. In paintComponent you would still paint the objects now part of this planet class but the variables set to 0 until populated. Here's an example of a thread though I'm pretty sure it doesn't use good programming practices and hopefully someone will comment on my errors and we can both learn!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: AWT: Painting buttons