Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,165 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,576 people online right now. Registration is fast and FREE... Join Now!




Using MouseListener to draw lines

 

Using MouseListener to draw lines

poomy

20 May, 2009 - 11:25 PM
Post #1

New D.I.C Head
*

Joined: 20 May, 2009
Posts: 3

I think I'm trying to run before I can walk and that the code might be "messy" but please help.
This is for a test game I'm making (2D click shooting).
How do I use the MouseListener method to get the X and Y coordinates of the location clicked and draw a line from another location to that point?

CODE

public boolean shot;
public boolean MouseClicked(MouseEvent e){
      shot=true;
      xmissile=e.getX();
      ymissile=e.getY();
      return true;
  }
  public void paint(Graphics g) {
if(shot==true){
//soldierpos[0] is X coordinate of soldier
//soldierpos[1] is Y coordinate of soldier
        g.drawLine(soldierpos[0],soldierpos[1],xmissile,ymissile);
    }


It doesn't seem to work. In the end though, I wish to make it fire a missile at an angle determined by mouse click in a straight line. How would I go about doing that?

Thanks for the help!

User is offlineProfile CardPM
+Quote Post



Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 06:18PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month