Google Answers Logo
View Question
 
Q: Make image follows the mouse movement ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Make image follows the mouse movement
Category: Computers > Programming
Asked by: open0102-ga
List Price: $10.00
Posted: 06 Jun 2003 23:37 PDT
Expires: 06 Jul 2003 23:37 PDT
Question ID: 214267
how to make a image follow the direction when mouse is moved 
here is the code I wrote can somebody check that for me Please 

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.util.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class Fighter_Plane extends Applet
             implements MouseListener, MouseMotionListener

{
	private Image imgFighter;

	public void init(){
		imgFighter = getImage(getDocumentBase(), " Fighter.gif");


		imgFighter.addMouseListener (this);
		imgFighter.addMouseMotionListener(this);
	}

	public void paint (Graphics g)
	{
		g.drawImage(imgFighter, 50,50, 50,50,this);
	}

	   public void mouseClicked( MouseEvent e ){}
	   public void mousePressed( MouseEvent e ){}
	   public void mouseReleased( MouseEvent e ){}
	   public void mouseEntered( MouseEvent e ){}
	   public void mouseExited( MouseEvent e ){}
	   public void mouseDragged( MouseEvent e ){}
	   public void mouseMoved( MouseEvent e ){}

}

Request for Question Clarification by answerguru-ga on 07 Jun 2003 00:21 PDT
Hi there,

Considering the special knowledge and time required to answer this
question, you might consider increasing your list price in order to
increase the chance of having it answered.

http://answers.google.com/answers/faq.html#pricing

answerguru-ga
Answer  
Subject: Re: Make image follows the mouse movement
Answered By: endo-ga on 07 Jun 2003 21:21 PDT
Rated:5 out of 5 stars
 
Hi,

Thanks for your question.
Here is the code I've gotten after adapting yours.
The loaded image will follow the mouse button within the applet.
If you have any questions or problems please don't hesitate to ask, I
hope you appreciate the work. Kind regards and thanks.
endo

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
import java.util.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class Fighter_Plane extends JApplet
{

  private Image imgFighter;

 public void init(){
   imgFighter = getImage(getDocumentBase(), "Fighter.gif");
   Fighter_Panel fp = new Fighter_Panel(imgFighter);  //create a panel
   getContentPane().add(fp);  //add panel to applet


 }

 public static void main(String s[]) {
      JFrame f = new JFrame("Fighter Plane");
      f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {System.exit(0);}
      });
      JApplet applet = new Fighter_Plane();
      f.getContentPane().add(applet, BorderLayout.CENTER);
      applet.init();
      f.pack();
      f.setSize(new Dimension(250,250));
      f.setVisible(true);
    }




}

class Fighter_Panel extends JPanel implements MouseListener,
MouseMotionListener{


  private int mouseX=0,mouseY=0;
  private Image imgFighter;


  public Fighter_Panel(Image imgFighter)
  {
    this.imgFighter=imgFighter;
    addMouseMotionListener(this);
    addMouseListener(this);

  }

    public void mouseClicked( MouseEvent e ){}
    public void mousePressed( MouseEvent e ){    }
    public void mouseReleased( MouseEvent e ){}
    public void mouseEntered( MouseEvent e ){}
    public void mouseExited( MouseEvent e ){}
    public void mouseDragged( MouseEvent e ){}

    public void mouseMoved( MouseEvent e ){
      repaint();
      mouseX=e.getX();
      mouseY=e.getY();
      repaint();
    }


    public void paintComponent (Graphics g)
     {
      super.paintComponent(g);
      g.drawImage(imgFighter, mouseX,mouseY,
imgFighter.getWidth(this),imgFighter.getHeight(this),this);
     }



}

Clarification of Answer by endo-ga on 07 Jun 2003 21:23 PDT
Hi,

I forgot to add my search strategy.
Here is a useful site with some applets and their source code:
http://java.sun.com/docs/books/tutorial/listofapplets.html

Search Strategy:
java applet
://www.google.com/search?q=java%20applet

Thanks.
endo

Request for Answer Clarification by open0102-ga on 08 Jun 2003 00:28 PDT
Hi Endo, Thank you very much that is very good for me 
after seeing your code I realize my Java knowledge is very very poor
because I thought I just only needed to add little code then the
program would
works.


Do you mind be kind to tell me that if there is a good web site or a
book for Java very beginner

Request for Answer Clarification by open0102-ga on 08 Jun 2003 00:33 PDT
Sorry 
this is first time I am using this so if I am satisfied 
with your answer How can I pay you

Request for Answer Clarification by open0102-ga on 08 Jun 2003 01:05 PDT
Hi Endo 
I just saw your message on another question which I posted earlier
"Create a simple game (Alien Spaceship"
as you can see I am trying now and I have taken a look the example on
the web site but it is still very difficult for me to create the game,
so as you are so enthusiastic how much will it cost me if I ask you to
create the game (but it must be easy to adopt for beginner if
possible)( my apology if I sould not ask you like that)
note: English is my second language, so if there is anything make you
confuse
I am sorry 

thank you 
Mark

Clarification of Answer by endo-ga on 08 Jun 2003 08:10 PDT
Hi,

You can always tip a researcher for a given question if you are
satisfied with their work.

Silly me, I hadn't noticed it was you who asked the other question, it
was quite late at night :).
What do you mean by "it must be easy to adopt for beginner if
possible". Does this mean that it should be easy for a beginner player
to play or that a beginner coder should be able to understand the
code?

As for how much it would cost to write the whole game, that is a
difficult question, because I really don't know how much time it would
take me, but it would probably take more than 5 hours and less than
20. You can try and use that to price your question appropriately.

Thanks, hope to help you again in the future.
Regards,
endo

Clarification of Answer by endo-ga on 08 Jun 2003 09:14 PDT
Hi,

Sorry I missed your first clarification request. Yes there are lots of
books and sites available, but most of them are in English, I'll give
you a few English ones and if you tell me your first language, I'll
try and find a few in that language.

Also don't worry if the code I wrote seems very different to yours,
it's all about knowing a bit about Java.

The Java API is one of your best allies:
http://java.sun.com/j2se/1.4.1/docs/api/index.html
It lists all the classes and what they do and the available functions.
For example if you look at the class Image, you'll see that you can't
add a MouseListener to it, this is generally reserver for Frames or
other containers.

Here are a few books and a site to start you off.

Thanks.
endo

Flanagan, D., "Java in a Nutshell (Third Edition)" O'Reilly UK, 1999
http://www.oreilly.com/catalog/javanut3/

Horstmann, C.S. and G. Cornell, "Core Java 2 Volume 1: Fundamentals
(5th edition)”, Prentice Hall, 2001
http://www.horstmann.com/corejava.html

Garside, R. and J.Mariani, "Java: First Contact", International
Thomson Computer Press, 1997
http://www.comp.lancs.ac.uk/computing/users/rgg/jfcBook/javaFirstContact.html

Sun's Java tutorial 
http://java.sun.com/docs/books/tutorial/

Request for Answer Clarification by open0102-ga on 08 Jun 2003 18:28 PDT
Hi Endo, Sorry I did not say it clearly 
--------------------------------------------------------------------
"it must be easy to adopt for beginner if
possible". Does this mean that it should be easy for a beginner player
to play or that a beginner coder should be able to understand the
code?
--------------------------------------------------------------------
I mean " a beginner coder should be able to understand the code?".

Actually I have been trying to write " The Alien Spaceship" and I have
finished some parts of it, so I am think about, how about if I post
the parts which I have done and you finish it for me, but there is a
problem, because Google does not provide the function in which I can
attach the files on it,
so what can I do, if you want to do it.

and I am Taiwanese so my first language is Chinese.

thank you very much for your time 
Mark

Clarification of Answer by endo-ga on 08 Jun 2003 19:09 PDT
Hi,

Thanks for the clarification.

I understand now, I'll make sure I comment any code in a very clear
way, you can always ask if there's something you don't understand.
 
>how about if I post the parts which I have done and you finish it for
me, but >there is a problem, because Google does not provide the
function in which I >can attach the files on it,

That sounds great and is probably the best way to learn, if you want
to upload files, you can do so to the following ftp server:

ftp://ftp.membres.lycos.fr 
username: dbky 
password: googleanswers 

If you have any problems or questions, please let me know.

Thanks.
endo

Request for Answer Clarification by open0102-ga on 09 Jun 2003 18:41 PDT
Hi Endo thank you again 

I would like to ask you that if this is what we are going to do
what is the next step for me, Do I need to create a new question?
or do you want to take look the files first which I have created and then
tell me what it will cost me  

thank you very much 
Mark

Clarification of Answer by endo-ga on 09 Jun 2003 19:11 PDT
Hi again,

>Do I need to create a new question? 

Yes I think this is the best solution, if you want to make sure that I
answer the question, please make sure that in the question and/or
subject you put that the question is for me.

>or do you want to take look the files first which I have created and
then tell >me what it will cost me

How about you post your question, I'll work on the files and help you
as much as I can and answer your questions, then I can tell you how
many hours I worked and you decide how much you want to tip me if at
all.

Let me know what you think.

Thanks.
endo

Request for Answer Clarification by open0102-ga on 10 Jun 2003 02:01 PDT
hi Endo I am really sorry 
I can't access to " ftp://ftp.membres.lycos.fr/ " 
I used CuteFTP to access the address above but I couldn't make it 

ftp://ftp.membres.lycos.fr  
username: dbky  
password: googleanswers  

Do you mind, tell me your E-Mail Address so I can send the files to you 
I promise you I will not send you a e-mail without your permission permission

sorry about that 
Mark

Clarification of Answer by endo-ga on 10 Jun 2003 05:03 PDT
Hi,

We are not allowed to give our e-mail addresses.
In cuteFTP just put: ftp.membres.lycos.fr as the hostname and still using
username: dbky   
password: googleanswers   

Please let me know how it goes.

Thanks.
endo

Request for Answer Clarification by open0102-ga on 10 Jun 2003 20:43 PDT
hi Endo I am sorry again, I just can't access to the site which you
provide for me, below was the situation, when i was trying to access
it
-------------------------------------------------------------------
STATUS:>	Connected. Authenticating...
COMMAND:>	USER dbky
	331 Password required for dbky.
COMMAND:>	PASS ********
	530 Access denied.
ERROR:>	Can't log in. Still trying...
ERROR:>	Can't log in. Disconnecting...
-------------------------------------------------------------------
I am sorry about I have made many problems to you 

I will copy and paste the code here below 
--------- First class ----------------
import java.awt.*;
//import java.awt.event.*;
import java.applet.Applet;
//import java.util.*;
//import java.awt.event.MouseListener;
//import java.awt.event.MouseEvent;

public class Aliens extends Applet {

	Label Lhit,Lscore;  //LFlighter;
	TextField Thit, Tscore;
	Button Bstart,Bpause;
	Panel p1,p2;
	int Total = 0;
	Color bgcolor;
	//add Class
	Enemy ClassEnemy = new Enemy();
	Fighter_Plane FP = new Fighter_Plane();

	public void init() {
		bgcolor = Color.white;
		setSize(600,500);
		setLayout(new BorderLayout());
		p1 = new Panel( new GridLayout(2,3));
		p2 = new Panel(new GridLayout(2,1,10,10));

		//LFlighter = new Label("------ The Place for fly flighter ------
",Label.CENTER);

		Lhit = new Label("Hit Rate:");
		Thit = new TextField("0");
		Lscore = new Label("Score:");
		Tscore = new TextField("0");
		Bstart = new Button("Start");
		Bpause = new Button("Pause");

		p2.setBackground(Color.white);
		p2.add(FP);
		p2.add(p1);
		p1.setBackground(bgcolor);
		p1.add(Bstart);
		p1.add(Lhit);
		p1.add(Thit);
		p1.add(Bpause);
		p1.add(Lscore);
		p1.add(Tscore);

		add("South",p2);
		add("Center",ClassEnemy);
	}
}
-------------------- second class -----------------
import java.applet.*;
import java.awt.*;


public class Enemy extends Canvas implements Runnable
{
	int x_position = 30;
	int y_position = 50;
	int x_speed = 1;
	int manager = 20;
	int enemyPosition_x = 750;
	int enemyPosition_y = 300;

	Toolkit toolkit = Toolkit.getDefaultToolkit();


	private Image dbImage,imgEnemy1,imgEnemy2,imgEnemy3,imgEnemy4,imgEnemy5,
	              imgEnemy6,
imgEnemy21,imgEnemy22,imgEnemy23,imgEnemy24,imgEnemy25,
	              imgEnemy26 ;
	private Graphics dbg;

	public Enemy()
	{   setSize(600,600);
		setBackground (Color.black);
		//setBackground = getImage(getDocumentBase(),"Background.jpg");

		imgEnemy1 = toolkit.getImage("EnemyShip.gif");
		imgEnemy2 = toolkit.getImage("EnemyShip.gif");
		imgEnemy3 = toolkit.getImage("EnemyShip.gif");
		imgEnemy4 = toolkit.getImage("EnemyShip.gif");
		imgEnemy5 = toolkit.getImage("EnemyShip.gif");
		imgEnemy6 = toolkit.getImage("EnemyShip.gif");
		imgEnemy21 = toolkit.getImage("EnemyShip.gif");
		imgEnemy22 = toolkit.getImage("EnemyShip.gif");
		imgEnemy23 = toolkit.getImage("EnemyShip.gif");
		imgEnemy24 = toolkit.getImage("EnemyShip.gif");
		imgEnemy25 = toolkit.getImage("EnemyShip.gif");
		imgEnemy26 = toolkit.getImage("EnemyShip.gif");
    start();
	}

	public void start ()
	{
		System.out.println("here");
		Thread th = new Thread (this);
		th.start ();
	}

	public void stop()
	{

	}

	public void destroy()
	{

	}

	public void run ()
	{

		Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		while (true)
		{
			if (x_position > enemyPosition_x - manager)
			{
				x_speed = -1;
			}
			else if (x_position < manager)
			{
				x_speed = +1;
			}

			x_position += x_speed;

			repaint();

			try
			{
				Thread.sleep (80);
			}
			catch (InterruptedException ex)
			{
				// do nothing
			}

			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		    }
	}

	public void update (Graphics g)
	{
		if (dbImage == null)
		{
			dbImage = createImage (this.getSize().width,
this.getSize().height);
			dbg = dbImage.getGraphics ();
		}

		dbg.setColor (getBackground ());
		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

		dbg.setColor (getForeground());
		paint (dbg);

		g.drawImage (dbImage, 0, 0, this);
	}

	public void paint (Graphics g)
	{
 		// First row of Alien Spaceships
		g.drawImage(imgEnemy1,x_position - manager, y_position - manager, 2
* manager, 2 * manager,this);
		g.drawImage(imgEnemy2,x_position - manager - 50, y_position -
manager, 2 * manager , 2 * manager,this);
		g.drawImage(imgEnemy3,x_position - manager - 100, y_position -
manager, 2 * manager, 2 * manager,this);
		g.drawImage(imgEnemy4,x_position - manager - 150, y_position -
manager, 2 * manager , 2 * manager,this);
		g.drawImage(imgEnemy5,x_position - manager - 200, y_position -
manager, 2 * manager , 2 * manager,this);
		g.drawImage(imgEnemy6,x_position - manager - 250, y_position -
manager, 2 * manager , 2 * manager,this);
		// Second row of Alien Spaceships
		g.drawImage(imgEnemy21,x_position - manager, y_position - manager +
50, 2 * manager, 2 * manager,this);
		g.drawImage(imgEnemy22,x_position - manager - 50, y_position -
manager  + 50, 2 * manager , 2 * manager,this);
		g.drawImage(imgEnemy23,x_position - manager - 100, y_position -
manager + 50, 2 * manager, 2 * manager,this);
		g.drawImage(imgEnemy24,x_position - manager - 150, y_position -
manager + 50, 2 * manager , 2 * manager,this);
		g.drawImage(imgEnemy25,x_position - manager - 200, y_position -
manager + 50, 2 * manager , 2 * manager,this);
		g.drawImage(imgEnemy26,x_position - manager - 250, y_position -
manager + 50, 2 * manager , 2 * manager,this);



	}
}
---------------- Third class ----------------------
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
import java.util.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

public class Fighter_Plane extends JApplet
{

  private Image imgFighter;
  Toolkit toolkit = Toolkit.getDefaultToolkit();

 public void Fighter_Plane(){

   imgFighter = toolkit.getImage("Fighter.gif");

   //imgFighter = getImage(getDocumentBase(), "Fighter.gif");
   Fighter_Panel fp = new Fighter_Panel(imgFighter);  //create a panel
   getContentPane().add(fp);  //add panel to applet


 }

 public static void main(String s[]) {
      JFrame f = new JFrame("Fighter Plane");
      f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {System.exit(0);}
      });
      JApplet applet = new Fighter_Plane();
      f.getContentPane().add(applet, BorderLayout.CENTER);
      applet.init();
      f.pack();
      f.setSize(new Dimension(250,250));
      f.setVisible(true);
    }




}

class Fighter_Panel extends JPanel implements MouseListener,
MouseMotionListener{


  private int mouseX=0,mouseY=0;
  private Image imgFighter;


  public Fighter_Panel(Image imgFighter)
  {
    this.imgFighter=imgFighter;
    addMouseMotionListener(this);
    addMouseListener(this);

  }

    public void mouseClicked( MouseEvent e ){}
    public void mousePressed( MouseEvent e ){    }
    public void mouseReleased( MouseEvent e ){}
    public void mouseEntered( MouseEvent e ){}
    public void mouseExited( MouseEvent e ){}
    public void mouseDragged( MouseEvent e ){}

    public void mouseMoved( MouseEvent e ){
      repaint();
      mouseX=e.getX();
      mouseY=e.getY();
      repaint();
    }


    public void paintComponent (Graphics g)
     {
      super.paintComponent(g);
      g.drawImage(imgFighter, mouseX,mouseY,
      imgFighter.getWidth(this),imgFighter.getHeight(this),this);
     }



}
----------- what I want to create -----------------
Game details: 
Alien spaceships travel at random speeds and in random directions 
across the top of the screen in L -> R or R -> L sequence.(there may 
be more than one space ship in view at any one time). The Aliens have
one objective: to drop bombs on your fighter plane. you have one 
objective: to blast the Aliens with your your fighter plane. 
the Aliens drop their bombs as ofter as they wish and you must move 
your fighter plane so as to avoid being hit. 
 
The button of the screen display a fighter plane so you can shoot down
a Alien. 
The L -> R position of which is controlled by the player's mouse. 
(shooting by pressing the LEFT mouse button. 
 
Additional Information: 
This game must be able to: Start a new game, show score, restore a 
Previously saved game, save a current game and Exit. 
NOTE: the able to save games is to be only available in the
application
execution, not applet version. A save game does not store the position
details of the players, it only stores the current score which can 
then be added to next time the games is loaded. 
 
set the speed of the game (1 for beginner up to 10 for super expert) 
set the duration of the game (in minutes). the default should be 2 
minutes. 
set the number of Aliens on screen at any time (from 1 up to 10 ) 
Turn scoring on and of - This option is to allow the player to enter 
his or her name (only if running as an application). 
 
Show a list of the top ten players and their scroes but have this 
option greyed out if the scoring option is turned of or if running as
an Applet. 
 
Display a brief of instructions as to show how to play the game. 
 
use graphics and sound  
this game is to be runnable as both an applet and an application
-------------------------------------------------------------------
thank you very much 
Mark

Request for Answer Clarification by open0102-ga on 10 Jun 2003 20:55 PDT
Hi Endo thank you for your patience
it might be even harder if use the codes which I create 
so if you think it will be more difficult to use my one 
then forget about it, just create a new on 
and If you are going to do that for me please use these two images 
http://tw.briefcase.yahoo.com/open0102 ->  under Share Document 
one is alien ship, another one is Fighter Plane 

tell me how many hours you will spend for creating this game 
just remind you that this game is for studying not for playing 
so don't worry too much about the appearance

if you are you going to create the game then when next you reply to me 
I will create a new question 
Title: Spaceship - Endo 

thank you mcuh very for your time 
Mark

Clarification of Answer by endo-ga on 11 Jun 2003 00:03 PDT
Hi,

I downloaded the 2 pictures from the website.

If this is for studying and learning Java, it is much better to try
and program yourself and ask for help, instead of me writing
everything. Just like this question, try and break up the problem into
little problems, and if you get stuck, you can always ask for help and
I will help.

If you still want me to program the whole game, I can do it on
Saturday or Sunday but not before. Also, it is difficult to say how
many hours are needed to write everything, maybe 10 hours, I don't
know. But even if it is more difficult to use your own code, it is
better because then you will understand Java better.

Hope this helps.

Thanks.
endo

Clarification of Answer by endo-ga on 11 Jun 2003 00:14 PDT
Hi,

I've just noticed your first clarification with the code, it is now up
to you to choose. You can post a new question where you ask for help
with one or more small problems. Or you can post a new question, just
asking me to write the whole game. It really is up to you, but if you
want to learn Java it is better to have problems and try to fix them.

Thanks.

Regards,
endo

Request for Answer Clarification by open0102-ga on 11 Jun 2003 02:28 PDT
Hi Endo 
thank you for your advise, I will remember that 

I have create a new question name " Spaceship - Endo"
Please using the codes I have created if possible 

thak you very much 
Mark

Clarification of Answer by endo-ga on 11 Jun 2003 03:08 PDT
Hi,

Thanks for your new question, I've posted a clarification there, just
to say that I will work on it this weekend. I'll use as much of your
code as possible and put a lot of explanations. Thanks again.

Regards,
endo
open0102-ga rated this answer:5 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy