This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Here's the deal...I am creating my own version of ms paint in java. I am expected to work in the constraints of awt (swing strictly prohibited). I need image buttons for functionalities such as drawing a rectangle etc just as it is in the actual application. It is noted that no such buttons are available in Java so I surfed a bit & found a solution :
I have used the object of this class in my code thus :
Both codes compile successfully but the button does not appear in my frame window when I run my application
I think this thread would sit better on our GUIs forum, so I shall move it.
Joined: Feb 24, 2011
Well, I don't see what add method you're calling. What class is your main method in?
The piece of code where I have invoked the constructor of ImageButton & added it to the frame is in the constructor of the class extending frame. The main method is in the same class that extends Frame.
Why are you using AWT rather than Swing?
This is a part of a project & like I mentioned, we are supposed to strictly work with AWT.
Joined: Feb 24, 2011
I did some simple debugging & noticed that the paint method is never called as a result of which button never gets painted.
The repaint call is in the processMouseEvent method which as far as I have understood generates action event based on whether the mouse is pressed over the button or not. However, the first time that the button should be painted must be done at the start up itself regardless of any events taking place. So, to test my assumption, I commented out the processMouseEvent method & simply made a call to repaint from within addActionListener method in ImageButton class.
But, this doesn't work either
What exactly am I missing ?
Moreover, I am not sure if I have understood the purpose of processMouseEvent correctly because if I had to use the original ImageButton class as it is, shouldn't I be invoking this method explicitly from my class that extends Frame ?
It is never needed to compare booleans to true (nor to false).
A painting method override usually invokes the super implementation as the first line in the overridden method, not the last, as that has the potential to overpaint any custom painting already done. The only reason you're getting away with that is because the default implementation of paint(g) in Component does nothing. You can remove the call to the super implementation entirely.
When posting code on a forum, in the interest of keeping it as short as possible (and thus easier to read) you should remove doc and other comments that are self-obvious.
Now to the actual problem. Toolkit#getImage(...) returns before the image is fully loaded. At the point in the ImageButton constructor that you assign the Dimensionsize from the image's getWidth and getHeight, in all probability those methods are returning -1 because the image isn't yet loaded. Sysout the return from preferredSize() to confirm whether that is indeed the problem.
Either use a MediaTracker to monitor the image loading (see the source of ImageIcon for example code) or load your image with ImageIO#read(...) which blocks till the image is loaded.
There are no new questions, but there may be new answers.