School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,162 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,038 people online right now. Registration is fast and FREE... Join Now!



Using MouseListener to draw lines

Using MouseListener to draw lines Rate Topic: -----

#1 poomy  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 3
  • Joined: 20-May 09


Dream Kudos: 0

Posted 20 May 2009 - 11:25 PM

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?

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!
Was This Post Helpful? 0
  • +
  • -


#2 ralph_komer  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 71
  • Joined: 12-May 09


Dream Kudos: 0

Posted 21 May 2009 - 02:54 AM

You need to store the old co-ordinates and the new co-ordinates everytime the mouse is clicked and then call paint method to draw the lines based on the co-ordinates.

Please feel free to refer to the below sample code and customize it.


import java.applet.*;
import java.awt.event.*;
import java.awt.*;

//<applet code = Test width = 400 height = 400></applet>
public class Test extends Applet implements MouseListener
{
	int x, y, oldx, oldy, count;

	public void init()
	{
		addMouseListener(this);
	}
	public void mouseEntered(MouseEvent me){}
	public void mouseExited(MouseEvent me){}
	public void mousePressed(MouseEvent me){}
	public void mouseClicked(MouseEvent me)
	{
		if(count == 0)
		{
			x = me.getX();
			y = me.getY();
			count++;
		}
		else
		{
			oldx = x;
			oldy = y;
			x = me.getX();
			y = me.getY();
			count++;
		}
		repaint();
	}
	public void paint(Graphics g)
	{
		g.drawLine(x, y, oldx, oldy);
	}
	public void mouseReleased(MouseEvent me){}
}



Was This Post Helpful? 1
  • +
  • -

#3 poomy  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 3
  • Joined: 20-May 09


Dream Kudos: 0

Posted 21 May 2009 - 08:13 PM

Thank you very much!

Do you know how I can make it so that the line only shows when the mouse is clicked?
I tried adding a boolean and changing to false in the MouseReleased method (as well as an if statement for drawing the line) but it did not work.

*And also, is there a way to "undraw" the line after a set amount of time?

This post has been edited by poomy: 21 May 2009 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#4 ralph_komer  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 71
  • Joined: 12-May 09


Dream Kudos: 0

Posted 22 May 2009 - 01:25 AM

The code above works only when the mouse is clicked if I am right. If you don't want the line to be drawn when the mouse is released, the simple solution would be to not execute any code in MouseRelease method.

For your second query, yes its possible to undraw/erase a line after a set amount of time. You need to store the start and end co-ordinates of the particular line and then redraw it using the same color as the background or simply call repaint to clear all teh contents from the applet.

Hope it helps.
Was This Post Helpful? 0
  • +
  • -

#5 poomy  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 3
  • Joined: 20-May 09


Dream Kudos: 0

Posted 29 May 2009 - 12:27 AM

Sorry for the late reply, was busy with exams.

I'm trying to get the g.drawLine to execute only when the mouse is clicked (not held down). In your code, when I ran it, the line still appears after releasing the mouse. I tried to use a boolean to find when it is held down but it does not work.

public class Test extends Applet implements Runnable, MouseListener, KeyListener{
boolean click=false;
int xrocket, yrocket;
int[] soldier = {250, 250, 4, 2};

public void mouseClicked(MouseEvent e) {
			 xrocket = e.getX();
			 yrocket = e.getY();
			 click=true;
			 repaint();
	}

public void mouseReleased(MouseEvent e) {
			click=false;
	}

public void paint(Graphics g) {
			if(click){
				 g.drawLine(soldier[0]+35, soldier[1]+15, xrocket, yrocket); //soldier array variables are controlled by user input
		}
}



Thanks for the help Komer

This post has been edited by poomy: 29 May 2009 - 12:30 AM

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

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

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month