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

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

Join 306,814 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,669 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


ralph_komer

RE: Using MouseListener To Draw Lines

21 May, 2009 - 02:54 AM
Post #2

D.I.C Head
**

Joined: 12 May, 2009
Posts: 69



Thanked: 13 times
My Contributions
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.

CODE


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){}
}


User is offlineProfile CardPM
+Quote Post

poomy

RE: Using MouseListener To Draw Lines

21 May, 2009 - 08:13 PM
Post #3

New D.I.C Head
*

Joined: 20 May, 2009
Posts: 3

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
User is offlineProfile CardPM
+Quote Post

ralph_komer

RE: Using MouseListener To Draw Lines

22 May, 2009 - 01:25 AM
Post #4

D.I.C Head
**

Joined: 12 May, 2009
Posts: 69



Thanked: 13 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

poomy

RE: Using MouseListener To Draw Lines

29 May, 2009 - 12:27 AM
Post #5

New D.I.C Head
*

Joined: 20 May, 2009
Posts: 3

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.

CODE
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
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 10:04PM

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