Jump to content
  • Advertisement
Sign in to follow this  
Concentrate

Simplifying these commands.

This topic is 2805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!