[java] choosing the right canvas

Started by
7 comments, last by Smoo 23 years, 11 months ago
Hello folks. I have a quick problem that hopefully someone will be able to help me out with. If I have: UserDefCanvas can1; OtherUserDefCanvas can2; public void mouseEntered (MouseEvent evt) { if (evt.something == can1) else if (evt.something == can2) } what would I replace the something with or would I just plain not use evt and use something else? Thanks! Smoo
Advertisement
Well, I don''t really know what the purpose is for what you are doing, but I if its what I think.....

What I would do is use a CardLayout with the canvases with the layout. Don''t worry if you already have a layout setup, just put this within it.

You can use you current setup, just call the Cardlayout''s premade functions and you are good to go.

I hope this makes some sense. If you need more help with language issues, try javasoft.com
Smoo,
You need to use evt.getSource().

Well a card layout is out of the question. The reason I need this is because I''m working on a map editor for a game (that I''ve worked on a bit and will continue once the map editor is to a point that I want it to be).

Anyways, it''s basically I have a canvas that displays a selection of the map but instead of having buttons for the arrows to move the map I want to use canvases. Now for a few reasons I don''t/can''t use swing (for JButtons) because I''m using vj++ which is doesn''t natively support swing and other features, and it sucks at importing those things. I also bought vj so i''m not going to go buy/download something else.

As for evt.getSource() I''ve tried a lot of things but the best I''ve been able to get out of getSource is the actual class name like UserDefCanvas, not can1. I need can1, can2, etc.. because the arrows are going to be using UserDefCanvas whilst the mapcanvas is OtherUserDefCanvas.

So, if anyone else has some suggestions or a little more detail on how to get it, I''d appreciate it.


Thanks.
Smoo
I''m assuming that you are trying to create custom buttons using Canvases in which case the solution you have suggested is one I would recommend. This does free you from having to use the built-in AWT/SWING button components.
It also allows you to do some pretty neat visual effects with the buttons.

- iTCHY


You could also use two different event handlers, one for each canvas.

So you''d have two inner classes (so they can access the can1 and can2 fields, assuming they''re class members):

public class CanvasOneEventHandler implements MouseListener
{
public void mouseEntered(MouseEvent evt)
{
// do stuff with the can1
}
}

public class CanvasTwoEventHandler implements MouseListener
{
public void mouseEntered(MouseEvent evt)
{
// do stuff with can2
}
}

Another possibility is to do anonymous inner classes:

can1.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent evt)
{
// do stuff with can1:
// if it''s a local variable in this method, declare it
// as final so you can reference it here
}
});

and the same thing for can2.

But really, if you''re doing can1.addMouseListener() or something similar, then evt.getSource() WILL return can1 if the event occurred on can1, so the test

if (evt.getSource() == can1)

should work.

Now, if UserDefCanvas and OtherUserDefCanvas don''t extend Canvas, then what you can do is add a method to these classes to return the canvas that''s embedded in them (which I assume you''d have to have), so your test changes to something like:

if (evt.getSource() == can1.getCanvas())

This also means that unless you overrode addMouseListener in UserDefCanvas & OtherUserDefCanvas, you wouldn''t be able to add the MouseListener directly to a class instance, anyway.

So basically, if you''re able to add a MouseListener to the class instance itself, evt.getSource() will return the instance of the class on which the event occurred. You can cast it to the appropriate type if you want, too

if (evt.getSource() instanceof UserDefCanvas)
{
UserDefCanvas can = (UserDefCanvas) evt.getSource();
// do stuff with the canvas
}
else if (evt.getSource() instanceof OtherUserDefCanvas)
{
OtherUserDefCanvas can = (OtherUserDefCanvas) evt.getSource();
}

Anyway, hopefullly somewhere in here you''ll find a solution.

I am a Jedi, like my father before me
I think you want
if( evt.getSource().equals(can1) ) {
do something with can1;
}
else if (evt.getSource().equals(can2) ) {
do something with can2;
}

if that doesn't work, nor the getSource() == can1, then you need to stop using vj++.

//excerp of API docs
java.util.EventObject
method getSource
public Object getSource()
Returns:
The object on which the Event initially occurred.

Edited by - Jim_Ross on 4/26/00 11:00:25 AM
.equals may work, but that''s more for checking the CONTENTS of the object. In this case, you want to check against actual object instances, so == is the better choice.
Also, it''s possible[1] that .equals can be overridden, so that you can run into a case where

object.equals(another_object)

returns true, but

object == another_object

returns false. It all depends on what the developer of the object wanted .equals to signify.

In this case, the desire is to compare against a particular object instance, so to guarantee that that''s what''s being checked, I would still recommend using == instead of .equals.

[1] I don''t think that''s the case for Canvas, but I think it''s important to make the distinction of comparing object instances versus object values, when it''s the former that is important to this example.
I am a Jedi, like my father before me
Well folks thanks a lot for all the information. It definitively helped!

Using evt.getSource().equals didn''t work. however using evt.getSource() == can1 does work beautifully.

Well, I''m going to continue working on it now to hopefully improve the esthetics of my editor.

Thanks again!
Smoo

This topic is closed to new replies.

Advertisement