Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By M-Dub4321

Thread: Java program in Eclipse help (Mouse events in applet)

  1. #1
    M-Dub4321 is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Java program in Eclipse help (Mouse events in applet)

    Quote Originally Posted by M-Dub4321 View Post
    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.
    We'll be glad to help, but you must create the code -- we won't create it for you. Please show what you've got so far and ask any specific questions that you have regarding your code, and you'll likely get helpful help.

    Much luck and welcome to the forum.

  3. #3
    M-Dub4321 is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: Java program in Eclipse help (Mouse events in applet)

    Quote Originally Posted by Fubarable View Post
    We'll be glad to help, but you must create the code -- we won't create it for you. Please show what you've got so far and ask any specific questions that you have regarding your code, and you'll likely get helpful help.

    Much luck and welcome to the forum.
    Thank you for the reply. My problems are:
    1. Finding where the shape is
    2. Making the text go away
    But I'll be sure to post what I have tomorrow when I get access to the school computer tomorrow. It should be up again around 8:00. :)

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default 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.

  5. #5
    M-Dub4321 is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: Java program in Eclipse help (Mouse events in applet)

    Quote Originally Posted by Fubarable View Post
    We'll be glad to help, but you must create the code -- we won't create it for you. Please show what you've got so far and ask any specific questions that you have regarding your code, and you'll likely get helpful help.

    Much luck and welcome to the forum.
    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);
    ***** }
    ** }
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default 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.

  7. #7
    M-Dub4321 is offline Member
    Join Date
    Jun 2012
    Posts
    4
    Rep Power
    0

    Default Re: Java program in Eclipse help (Mouse events in applet)

    Quote Originally Posted by Tolls View Post
    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?
    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.
    Fubarable likes this.

Similar Threads

  1. Java mouse events in gui.
    By Xamresor in forum New To Java
    Replies: 1
    Last Post: 12-19-2011, 10:54 PM
  2. Help with some basics please (mouse events)
    By tigersarehot in forum AWT / Swing
    Replies: 4
    Last Post: 04-28-2010, 02:22 AM
  3. Mouse events, are they best or only way to go?
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-10-2009, 04:34 PM
  4. Need help with looping mouse Events.
    By busdude in forum New To Java
    Replies: 1
    Last Post: 04-08-2009, 08:25 PM
  5. Demonstration of mouse events
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:45 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •