permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes creating an application similar to ms paint in java... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "creating an application similar to ms paint in java... " Watch "creating an application similar to ms paint in java... " New topic
Author

creating an application similar to ms paint in java...

trish mo
Greenhorn

Joined: Feb 24, 2011
Posts: 14
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
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
is there some reason you're not allowed to use java.awt.Button? That's what we all used to do before there was Swing...

rc

Oh, I see -- image button, not just button.

Well, I don't see what add method you're calling. What class is your main method in?

rc
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Why are you using AWT rather than Swing?

I think this thread would sit better on our GUIs forum, so I shall move it.
trish mo
Greenhorn

Joined: Feb 24, 2011
Posts: 14
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.
trish mo
Greenhorn

Joined: Feb 24, 2011
Posts: 14
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 ?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4640
    
    5

First, a few points about your code.

  • 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 Dimension size 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.


    luck, db
    There are no new questions, but there may be new answers.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: creating an application similar to ms paint in java...