Sign in to follow this  
Concentrate

Simplifying these commands.

Recommended Posts

I wish java had function pointers. How would I simplify this code :

private class Listener extends java.awt.event.MouseAdapter{
public void mouseClicked(MouseEvent e) {
for(IShape s : shapeList){
if(s.contains(e.getX(),e.getY())){
s.getShapeMouseAdapter().mouseClicked(e);
break;
}
}
}
public void mouseDragged(MouseEvent e) {
System.out.print(e.getPoint() + "\n");
for(IShape s : shapeList){
if(s.contains(e.getX(),e.getY())){
s.getShapeMouseAdapter().mouseDragged(e);
break;
}
}
}
public void mouseEntered(MouseEvent e) {
for(IShape s : shapeList){
if(s.contains(e.getX(),e.getY())){
s.getShapeMouseAdapter().mouseEntered(e);
break;
}
}
}
public void mouseExited(MouseEvent e) {
for(IShape s : shapeList){
if(s.contains(e.getX(),e.getY())){
s.getShapeMouseAdapter().mouseExited(e);
break;
}
}
}
public void mouseMoved(MouseEvent e) {
for(IShape s : shapeList){
if(s.contains(e.getX(),e.getY())){
s.getShapeMouseAdapter().mouseMoved(e);
break;
}
}
}
public void mousePressed(MouseEvent e) {
for(IShape s : shapeList){
if(s.contains(e.getX(),e.getY())){
s.getShapeMouseAdapter().mousePressed(e);
break;
}
}
}
public void mouseReleased(MouseEvent e) {
for(IShape s : shapeList){
if(s.contains(e.getX(),e.getY())){
s.getShapeMouseAdapter().mouseReleased(e);
break;
}
}
}
public void mouseWheelMoved(MouseWheelEvent e) {
for(IShape s : shapeList){
if(s.contains(e.getX(),e.getY())){
s.getShapeMouseAdapter().mouseWheelMoved(e);
break;
}
}
}

}


Share this post


Link to post
Share on other sites
Assuming IShape extends Component:

private class Listener extends java.awt.event.MouseAdapter{
private void conditionalDispatch(MouseEvent e) {
for(IShape s : shapeList){
if(s.contains(e.getX(),e.getY())){
s.dispatchEvent(e);
return;
}
}

public void mouseClicked(MouseEvent e) {
conditionalDispatch(e);
}
....
// repeat for all other events

Share this post


Link to post
Share on other sites
I'm sorry, after googling dispatchEvents, I still don't get the big picture.
Mind explaining a little bit please. And IShape does not inherit from Components,
but maybe I can use the suggested idea somehow.

Share this post


Link to post
Share on other sites
Quote:
Original post by Concentrate
And IShape does not inherit from Components,
but maybe I can use the suggested idea somehow.


If IShape does not inherit from Component, then it must reinvent what AWT/Swing provides out of box.

MouseEvent contains the type of mouse action that occured.

So without dispatchEvent, it would look something like:

private void conditionalDispatch(MouseEvent e) {
for(IShape s : shapeList){
if(s.contains(e.getX(),e.getY())){
switch(e.getId()) {
case MouseEvent.MOUSE_CLICKED : s.mouseClicked(e); break;
case MouseEvent.MOUSE_DRAGGED : s.mouseDragged(e); break;
....
}
return;
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this