Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Trying to move airplane by clicking on it each time.

$
0
0
**I created two aircraft's for my first project.Then for my next project, I have to be able to make it move horizontally one step every time I click on the body, and move vertically every time I click on any of the wings. I started by creating first a class body that extended to RoundedRectangle but I got errors with the MouseClicked event and I was not able to move the object. Can anybody help me out with what I am doing wrong? Or what I need to do in order to make it move horizontally and vertically? **

    import wheels.users.*;
    import java.awt.Color; 

    public class Aircraft {
    private Ellipse _Window1, _Window2, _Window3, _Window4, _Window5, _Window6, _Window7, _Window8;
    private RoundedRectangle _body;
    private Rectangle _mouth;
    private RoundedRectangle _leftWing, _rightWing, _backWing;
    private ConversationBubble _bubble;
    
    public Aircraft() {
        _body = new RoundedRectangle (java.awt.Color.blue);
        _body.setSize(250, 65);
        _body.setLocation(100,100);
        
        _mouth = new Rectangle (java.awt.Color.black);
        _mouth.setSize(20,5);
        _mouth.setLocation(210,170);
        
        _Window1 = new Ellipse (java.awt.Color.green);
        _Window1.setSize(10,20);
        _Window1.setLocation(120,80);
        
        _Window2 = new Ellipse (java.awt.Color.green);
        _Window2.setSize(10,20);
        _Window2.setLocation(135,80);
        
        _Window3 = new Ellipse (java.awt.Color.green);
        _Window3.setSize(10,20);
        _Window3.setLocation(145,80);
        
        _Window4 = new Ellipse (java.awt.Color.green);
        _Window4.setSize(10,20);
        _Window4.setLocation(155,80);
        
        _Window5 = new Ellipse (java.awt.Color.green);
        _Window5.setSize(10,20);
        _Window5.setLocation(165,80);
        
        _Window6 = new Ellipse (java.awt.Color.green);
        _Window6.setSize(10,20);
        _Window6.setLocation(175,20);
        
        _Window7 = new Ellipse (java.awt.Color.green);
        _Window7.setSize(10,20);
        _Window7.setLocation(185,20);
        
        _Window8 = new Ellipse (java.awt.Color.green);
        _Window8.setSize(10,20);
        _Window8.setLocation(195,20);
        
        _leftWing = new RoundedRectangle (java.awt.Color.green);
        _leftWing.setSize(60,20);
        _leftWing.setLocation(200,50);
        _leftWing.setRotation(60);
        
        _rightWing = new RoundedRectangle (java.awt.Color.green);
        _rightWing.setSize(60,20);
        _rightWing.setLocation(200,50);
        _rightWing.setRotation(120);
        
        _backWing = new RoundedRectangle (java.awt.Color.green);
        _backWing.setSize(80,20);
        _backWing.setLocation(350,270);
        _backWing.setRotation(160);
         
    }
    
    public Aircraft (Color _bodyA, Color _windowA, Color _leftWingA, Color _rightWingA) { 
        _body = new RoundedRectangle (_bodyA);
        _body.setSize(250, 65);
        
        _mouth = new Rectangle (java.awt.Color.black);// 
        _mouth.setSize(20,5);
        
        _Window1 = new Ellipse (_windowA);
        _Window1.setSize(10,20);
        
        _Window2 = new Ellipse (_windowA);
        _Window2.setSize(10,20);
        
        _Window3 = new Ellipse (_windowA);
        _Window3.setSize(10,20);
        
        _Window4 = new Ellipse (_windowA);
        _Window4.setSize(10,20);
        
        _Window5 = new Ellipse (_windowA);
        _Window5.setSize(10,20);
        
        _Window6 = new Ellipse (_windowA);
        _Window6.setSize(10,20);
        
        _Window7 = new Ellipse (_windowA);
        _Window7.setSize(10,20);
        
        _Window8 = new Ellipse (_windowA);
        _Window8.setSize(10,20);
        
        _leftWing = new RoundedRectangle (_leftWingA);
        _leftWing.setSize(60,20);
        _leftWing.setRotation(60);
        
        _rightWing = new RoundedRectangle (_leftWingA);
        _rightWing.setSize(60,20);
        _rightWing.setRotation(120);
        
        _backWing = new RoundedRectangle (_leftWingA);
        _backWing.setSize(80,20);
        _backWing.setRotation(150);
    }
    
    public void setMessage(String _string, int x, int y) { 
        _bubble = new ConversationBubble(_string); 
        _bubble.setLocation(x,y);
    }

    public void setLocation(int x, int y) {
        _body.setLocation(x,y); 
        _mouth.setLocation(x+20,y+50);
        _Window1.setLocation(x+10,y+20);
        _Window2.setLocation(x+40,y+20);
        _Window3.setLocation(x+70,y+20);
        _Window4.setLocation(x+100,y+20);
        _Window5.setLocation(x+130,y+20);
        _Window6.setLocation(x+160,y+20);
        _Window7.setLocation(x+190,y+20);
        _Window8.setLocation(x+220,y+20);
        _leftWing.setLocation(x+90,y+75);
        _rightWing.setLocation(x+90,y-30);
        _backWing.setLocation(x+245,y+15);
    }
    
}



**AND this is the App class:**

    import wheels.users.*;
    import java.awt.Color;


    public class AirApp extends Frame {
    private Aircraft _airc1, _airc2;// created another aircraft here.

	public AirApp() {
		_airc1 = new Aircraft(Color.blue, Color.green, Color.green, Color.green);
		_airc1.setMessage("Hey look, I am way higher than you!", 130,10);
		_airc1.setLocation(300,50);
		
		_airc2 = new Aircraft(Color.red, Color.pink, Color.gray, Color.orange);
		_airc2.setMessage("Oh yeah? but I am flying faster than you!", 0,250);
		_airc2.setLocation(100,350);
	}

	public static void main(String [] args) {
	    AirApp app = new AirApp();
	}

}


**Then i created a Body class that extends Rounded Rectangle and here it is:**

    import wheels.users.*;
    import java.awt.Color; 
    import java.awt.event.MouseEvent;

    public class Body extends RoundedRectangle {
    
    private java.awt.Point _lastMousePosition, _currentPoint;
    
    private int _x, _y;
 
    public Body(java.awt.Color aColor, int x, int y, int l, int w) {
        super(aColor);
        this.setLocation(x,y);
        this.setSize(l,w);
        
        _lastMousePosition = new java.awt.Point();
    }    
        
    public Body () {
    }    
    public void mouseClicked (MouseEvent e) {
        _lastMousePosition = e.getPoint();
        
    }
    public void mouseReleased (MouseEvent e) {
        _currentPoint = e.getPoint();
        int diffX = _currentPoint.x - _lastMousePosition.x;
        this.setLocation(this.getXLocation());
        _lastMousePosition= _currentPoint;
    }    

}


I am stuck at how to move this airplane horizontally when i click on the body, and how to make it move vertically when I click on the wings. I know I use the mouseClicked event, but i think I am messing up inside the methods. Can someone please help me fix this so I am able to move because from few tries, when I click on the body, the plane disappears?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>