Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 05:42 AM
Member
 
Join Date: Jul 2007
Posts: 40
barney is on a distinguished road
Help with game applet in java
Hi, I'm having trouble making a game work. I cant get my mouse to react to my applet at all. I've tried a few different methods, and a few different listeners types but haven't been successful with anything yet. Everything I have tried except for EventListeners has failed. I could use some serious help.
Her is what I have right now.

Code:
import java.awt.*; import java.awt.event.*; import java.applet.*; public class Catcher extends Applet implements MouseMotionListener{ int x_pos; int y_pos; public void init(){ addMouseMotionListener(this) } public void mouseMove(MouseEvent e){ int x_pos=e.getX(); } public void paint(Graphics g){ g.setColor(Color.blue); g.fillRect(x_pos-25,y_pos-5,50,10); } }
Is there anyone that has done Applet coding before that could help?
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-04-2007, 12:34 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Declaring x_pos and y_pos as local variables in paint hid the member variables of the same name. Solution: eliminate the declarations in paint.
In java:
Code:
public void paint(Graphics g) { // Change these - declaration/instantiations of local variables int x_pos=e.getX(); // These hide the member variables int y_pos=e.getY(); // declared above in class scope. // to these - instantiations of member variables x_pos=e.getX(); y_pos=e.getY();
Code:
// <applet code="CatcherRx" width="300" height="300"></applet> import java.awt.*; import java.awt.event.*; import java.applet.*; public class CatcherRx extends Applet implements MouseMotionListener{ int x_pos; int y_pos; public void init(){ addMouseMotionListener(this); } public void mouseMoved(MouseEvent e){ x_pos=e.getX(); y_pos=e.getY(); repaint(); } public void paint(Graphics g){ g.setColor(Color.blue); g.fillRect(x_pos-25,y_pos-5,50,10); } public void mouseDragged(MouseEvent e) {} }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-04-2007, 05:15 AM
Senior Member
 
Join Date: Jul 2007
Posts: 130
cruxblack will become famous soon enough
Just like hardwired just said, the compiler might not give u an error by doing
Code:
public void mouseMove(MouseEvent e){ int x_pos=e.getX(); }
But u r declaring another x_pos in that mouseMove() method, in which the class variable x_pos must be refered as this.x_pos, not a very good on this context though

And btw, if im not mistaken, by implementing MouseMotionListener we must override the method mouseDraggeed() and mouseMoved right?
Just like hardwired just done there, if not the compiler would complain, maybe ur other listeners don't work because u didn't override the correct methods for that listener?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-13-2008, 06:26 AM
Member
 
Join Date: Feb 2008
Posts: 6
winniejr_99 is on a distinguished road
try this program and just analyze it. okay??

import java.awt.*;
import java.applet.*;
// import an extra class for the MouseListener
import java.awt.event.*;

// Tells the applet you will be using the MouseListener methods.

public class MouseClickExample extends Applet implements MouseListener
{
// The X-coordinate and Y-coordinate of the last click.
int xpos;
int ypos;

MyMouseListener ml;

// The coordinates of the rectangle we will draw.
// It is easier to specify this here so that we can later
// use it to see if the mouse is in that area.
int rect1xco,rect1yco,rect1width,rect1height;

// The variable that will tell whether or not the mouse
// is in the applet area.
boolean mouseEntered;

// variable that will be true when the user clicked i the rectangle
// the we will draw.
boolean rect1Clicked;

public void init(){
// Assign values to the rectanagle coordinates.
rect1xco = 20;
rect1yco = 20;
rect1width = 100;
rect1height = 50;

// Add the MouseListener to your applet
addMouseListener(ml);
}

public void paint(Graphics g){
// Rectangle's color
g.setColor(Color.green);

g.fillRect(rect1xco,rect1yco,rect1width,rect1heigh t);

g.setColor(Color.red);

// When the user clicks this will show the coordinates of the click
// at the place of the click.
g.drawString("("+xpos+","+ypos+")",xpos,ypos);

// If the click was in the rectangle show this message
if (rect1Clicked) g.drawString("You clicked in the Rectangle",20,120);
// else this one
else g.drawString("You clicked outside of the rectangle",20,120);

if (mouseEntered) g.drawString("Mouse is in the applet area",20,160);
else g.drawString("Mouse is outside the Applet area",20,160);
}

/* These methods always have to present when you implement MouseListener

public void mouseClicked (MouseEvent me) {}
public void mouseEntered (MouseEvent me) {}
public void mousePressed (MouseEvent me) {}
public void mouseReleased (MouseEvent me) {}
public void mouseExited (MouseEvent me) {}
*/

// This method will be called when the mouse has been clicked.
public void mouseClicked (MouseEvent me) {

// Save the coordinates of the click lke this.
xpos = me.getX();
ypos = me.getY();

// Check if the click was inside the rectangle area.
if (xpos > rect1xco && xpos < rect1xco+rect1width && ypos >rect1yco &&
ypos < rect1yco+rect1height) rect1Clicked = true;
// if it was not then rect1Clicked is false;
else
rect1Clicked = false;
//show the results of the click
repaint();

}

// This is called when the mous has been pressed
public void mousePressed (MouseEvent me) {}

// When it has been released
// not that a click also calls these Mouse-Pressed and Released.
// since they are empty nothing hapens here.
public void mouseReleased (MouseEvent me) {}

// This is executed when the mouse enters the applet. it will only
// be executed again when the mouse has left and then re-entered.
public void mouseEntered (MouseEvent me) {
// Will draw the "inside applet message"
mouseEntered = true;
repaint();
}

// When the Mouse leaves the applet.
public void mouseExited (MouseEvent me) {
// will draw the "outside applet message"
mouseEntered = false;
repaint();
}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
First Java-game: Containers eastviolence New To Java 0 04-04-2008 07:09 PM
Java BattleShip game help mars_red Advanced Java 0 02-12-2008 01:58 AM
Implementing "Game Over" in Minesweeper game based on Gridworld framework. JFlash New To Java 0 11-16-2007 12:02 AM
Help with java text game silvia New To Java 6 07-27-2007 07:58 PM
Help with my game in java lenny New To Java 1 07-23-2007 05:40 PM


All times are GMT +3. The time now is 01:03 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org