• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
trish mo
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic