Results 1 to 7 of 7
- 06-11-2012, 03:03 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 4
- Rep Power
- 0
Java program in Eclipse help (Mouse events in applet)
I've been trying to get this program to work for a few weeks now but just cannot grasp the concept. My teacher isn't one to help very much, he just tells me that I need to read the notes and put programs together (which this is our first mouse input program so I don't know how to do that...). The assignment is called "Boom Applet". This is just a regular assignment that goes along with the other 50 that we did throughout the year so it's not like I'm cheating that bad if I ask for help. I have to create an applet that causes an explosive message ("BOOM") to appear when the mouse is moved over a shape (a small circle that looks like a bomb). The image then resets when the mouse is moved off of the object. It's starting to make me very upset and I feel like it is a lot more simple than I am making it. If you could help me make this code I would appreciate it very much and best answer will be given. Thank you! Ignorant and immature answers will be blocked and reported.
-
Re: Java program in Eclipse help (Mouse events in applet)
- 06-11-2012, 03:40 AM #3
Member
- Join Date
- Jun 2012
- Posts
- 4
- Rep Power
- 0
Re: Java program in Eclipse help (Mouse events in applet)
- 06-11-2012, 03:50 AM #4
Re: Java program in Eclipse help (Mouse events in applet)
Have you started on the assignment yet?
I would take a look at the MouseListener JavaDoc, as that is probably what you will be utilizing.
I'm assuming that the shape is an image that you have inside of a JPanel or some container, but without the code it's hard to say.
Post some code, and we can help you out.
- 06-11-2012, 02:08 PM #5
Member
- Join Date
- Jun 2012
- Posts
- 4
- Rep Power
- 0
Re: Java program in Eclipse help (Mouse events in applet)
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Boom extends Applet
** implements MouseListener, MouseMotionListener {
** int width, height;
** int x, y;*** // the coordinates of the upper-left corner of the box
** int mx, my;* // the most recently recorded mouse coordinates
** boolean isMouseDraggingBox = false;
** boolean isBoom = false;
** Font timesFont;
** public void init() {
***** width = getSize().width;
***** height = getSize().height;
***** setSize(1280,1000);
***** setBackground( Color.white );
***** x = width/2 - 20;
***** y = height/2 - 20;
***** addMouseListener( this );
***** addMouseMotionListener( this );
***** timesFont = new Font("TimesRoman",Font.BOLD,100);
** }
** public void mouseEntered( MouseEvent e ) { }
** public void mouseExited( MouseEvent e ) { }
** public void mouseClicked( MouseEvent e ) { }
** public void mousePressed( MouseEvent e ) {
***** mx = e.getX();
***** my = e.getY();
***** if ( x < mx && mx < x+40 && y < my && my < y+40 ) {
******** isMouseDraggingBox = true;
***** }
***** e.consume();
** }
** public void mouseReleased( MouseEvent e ) {
***** isMouseDraggingBox = false;
***** e.consume();
** }
** public void mouseMoved( MouseEvent e ) {
*** mx = e.getX();
*** my = e.getY();
*** if (mx < 599 && mx > 639 + 40 && my > 467 - 40 && my < 467 + 40){
** ***isBoom = true;
** ***repaint();
** ***e.consume();
** **}
** }
** public void mouseDragged( MouseEvent e ) {
***** if ( isMouseDraggingBox ) {
******** // get the latest mouse position
******** int new_mx = e.getX();
******** int new_my = e.getY();
******** // displace the box by the distance the mouse moved since the last event
******** // Note that "x += ...;" is just shorthand for "x = x + ...;"
******** x += new_mx - mx;
******** y += new_my - my;
******** // update our data
******** mx = new_mx;
******** my = new_my;
******** repaint();
******** e.consume();
***** }
** }
** public void paint( Graphics g ) {
***** g.setColor( Color.black );
***** g.fillRect( 639, 467, 40, 40 );
***** if (isBoom){
*** ** g.drawString("Boom", 100,200);
***** }
** }
}
- 06-11-2012, 03:42 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: Java program in Eclipse help (Mouse events in applet)
Please use [code] tags [/code] to retain formatting and not lots of *'s, which don't really help much.
Does that code work?
If not then what is the problem?Please do not ask for code as refusal often offends.
- 06-11-2012, 04:06 PM #7
Member
- Join Date
- Jun 2012
- Posts
- 4
- Rep Power
- 0
Re: Java program in Eclipse help (Mouse events in applet)
I found my problem everybody. I had a logic error in my mouse moved event. I had it set to be <467 but >507. I just switched the two and it detected the shape perfectly. As for the message to go away, I created another if statement that detected when the mouse was not on the shape and repainted the background. Thank you all for the help.
Similar Threads
-
Java mouse events in gui.
By Xamresor in forum New To JavaReplies: 1Last Post: 12-19-2011, 10:54 PM -
Help with some basics please (mouse events)
By tigersarehot in forum AWT / SwingReplies: 4Last Post: 04-28-2010, 02:22 AM -
Mouse events, are they best or only way to go?
By dbashby in forum New To JavaReplies: 2Last Post: 04-10-2009, 04:34 PM -
Need help with looping mouse Events.
By busdude in forum New To JavaReplies: 1Last Post: 04-08-2009, 08:25 PM -
Demonstration of mouse events
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:45 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks