Results 1 to 13 of 13
  1. #1
    Join Date
    Oct 2011
    Location
    Jakarta.
    Posts
    7
    Rep Power
    0

    Default Java MouseClicked problem.

    i have a problem while using netbeans IDE 7.0.1,
    this is my campus assignment, i got stucked for 3 days because of this,
    what i'm going to do is click an image for going through the next page,
    i made 3 status, first one for normal condition "0", second one for hover "1" and the third one for turn to next page,
    but it ended up with an error after i click the mentioned coordinate,

    here is the screenshot :
    preview error.JPG

    here are the codes :

    Java Code:
    package plantversuszombie;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    
    public class Menu extends JPanel implements MouseMotionListener, MouseListener
    {
        Image BackgroundImage;
        Image BoardUpperImage;
        Image BoardBottomImage;
        Image ButtonImage;
        Image ButtonImageHover;
        int MenuStatus;
        Survival Survival;
        
        public Menu()
        {
            BackgroundImage = new ImageIcon("E:/Document/Coding/NetBeans/PlantVersusZombie/Image/Menu Background.jpg").getImage();
            BoardUpperImage = new ImageIcon("E:/Document/Coding/NetBeans/PlantVersusZombie/Image/Welcome Name Board.png").getImage();
            BoardBottomImage = new ImageIcon("E:/Document/Coding/NetBeans/PlantVersusZombie/Image/Welcome Note Board.png").getImage();
            ButtonImage = new ImageIcon("E:/Document/Coding/NetBeans/PlantVersusZombie/Image/Survival Button.png").getImage();
            ButtonImageHover = new ImageIcon("E:/Document/Coding/NetBeans/PlantVersusZombie/Image/Survival Button Hover.png").getImage();
            addMouseMotionListener(this);
            addMouseListener(this);
            MenuStatus = 0;
        }
    
        public void paintComponent (Graphics g)
        {
            Graphics2D MenuGraphics = (Graphics2D) g;
            MenuGraphics.drawImage(BackgroundImage, 0, 0, null);
            MenuGraphics.drawImage(BoardUpperImage, 0, 0, null);
            MenuGraphics.drawImage(BoardBottomImage, 0, 122, null);
            MenuGraphics.drawImage(ButtonImage, 418, 331, null);
            if(MenuStatus == 0)
            {            
            }
            else if(MenuStatus == 1)
            {
                MenuGraphics.drawImage(ButtonImageHover, 418, 331, null);
            }
            else if(MenuStatus == 2)
            {
                Survival = new Survival();
                add(Survival);
                validate();
            }
            repaint();
        }
        
        public void mouseDragged(MouseEvent e) 
        {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void mouseMoved(MouseEvent e) 
        {
            if(e.getX() >= 418 && e.getX() <= 418 + 262 && e.getY() > 331 && e.getY() <= 331 + 115)
            {
                MenuStatus = 1;
            }
            else
            {
                MenuStatus = 0;
            }
        }
    
        public void actionPerformed(ActionEvent e) 
        {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void mouseClicked(MouseEvent e) 
        {
            if(e.getX() >= 418 && e.getX() <= 418 + 262 && e.getY() > 331 && e.getY() <= 331 + 115)
            {
                MenuStatus = 2;
            }
        }
    
        public void mousePressed(MouseEvent e) 
        {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void mouseReleased(MouseEvent e) 
        {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void mouseEntered(MouseEvent e) 
        {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void mouseExited(MouseEvent e) 
        {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    is there anybody can help ?,
    i'm waiting desprately here,
    the deadline is tomorrow,
    thanks.
    Last edited by DarrylBurke; 10-30-2011 at 08:09 AM. Reason: Added code tags

  2. #2
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Java MouseClicked problem.

    Can you please post your survival class?

  3. #3
    Join Date
    Oct 2011
    Location
    Jakarta.
    Posts
    7
    Rep Power
    0

    Default Re: Java MouseClicked problem.

    wooow, i'm really really glad someone replied this : D,

    here are the codes for survival :

    package plantversuszombie;

    import javax.swing.JPanel;

    public class Survival extends JPanel
    {

    }

  4. #4
    Join Date
    Oct 2011
    Location
    Jakarta.
    Posts
    7
    Rep Power
    0

    Default Re: Java MouseClicked problem.

    mmm i don't really get it,
    the mouseclicked and the other, should i put it in te paint component ?,
    can you give an example ?.

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

    Default Re: Java MouseClicked problem.

    Quote Originally Posted by skaterboy987 View Post
    You have to call
    Java Code:
      public void paintComponent (Graphics g)
    everytime you change the value of menustatus
    No, do not do this. It is not appropriate to call paintComponent directly in most all situations, and this situation is certainly no exception.
    Last edited by Fubarable; 10-30-2011 at 01:42 PM.

  6. #6
    Join Date
    Oct 2011
    Location
    Jakarta.
    Posts
    7
    Rep Power
    0

    Default Re: Java MouseClicked problem.

    soooo what should i doooo,
    looooorddd, it's 18 hours left to the deadlineeeeee,
    it's happens everytime i try to click with the mentioned coordinate : (.

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

    Default Re: Java MouseClicked problem.

    Please hang on while I study your problem...
    But please, no mention of deadlines. That is your problem and not ours and will only dissuade folks from helping.

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

    Default Re: Java MouseClicked problem.

    Can you tell us in greater detail exactly what behavior you're trying to achieve? Also, you have program logic in the paintComponent method here:

    Java Code:
            else if(MenuStatus == 2)
            {
                Survival = new Survival();
                add(Survival);
                validate();
            }
    And then you actually call repaint(); from within paintComponent which doesn't make sense.

    And please watch your language. Kids use this forum. Post deleted.

  9. #9
    Join Date
    Oct 2011
    Location
    Jakarta.
    Posts
    7
    Rep Power
    0

    Default Re: Java MouseClicked problem.

    fiuh, now i have finished the other 2 pages,
    the mouseclicked is the only problem left,
    sorry for the mentioned deadline,
    i just freak'n out here because i feel that there's nothing wrong with my code.

  10. #10
    Join Date
    Oct 2011
    Location
    Jakarta.
    Posts
    7
    Rep Power
    0

    Default Re: Java MouseClicked problem.

    have you seen the screenshot ?,
    what i'm going to do is when i moved my mouse to the mentioned coordinate it can change to the hover image, that's what i'm going to ahchieve with the status 0 and 1,
    and if i clicked the mentioned coordinate i want it to change to next page, wich is survival page,
    the same method works with action performed, that's what i've done with the intro,
    but when i using mouseclicked it turned up like this,
    if i didn't use repaint(); there, the hover doesn't worck, i've tried it before.

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

    Default Re: Java MouseClicked problem.

    I would do my logic in the MouseListener code and only do drawing based on the logical state of the program in the paintComponent method code.

    Here's an example that shows a simple version of what I said.

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Rectangle2D;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.swing.*;
    
    public class MouseEg extends JPanel {
       private static final int PREF_W = 800;
       private static final int PREF_H = 600;
       private Rectangle2D rectangle = new Rectangle2D.Double(100, 100, 300, 300);
       private boolean hover = false;
       
       public MouseEg() {
          MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
          addMouseListener(myMouseAdapter);
          addMouseMotionListener(myMouseAdapter);
       }
       
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
       
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D)g;
          if (hover) {
             g2.setColor(Color.pink);
             g2.fill(rectangle);
          }
          g2.setColor(Color.gray);
          g2.draw(rectangle);
       }
       
       private class MyMouseAdapter extends MouseAdapter {
          @Override
          public void mousePressed(MouseEvent e) {
             if (rectangle.contains(e.getPoint())) {
                // program logic here
                JOptionPane.showMessageDialog(MouseEg.this, "Rectangle clicked");
             }
          }
          
          @Override
          public void mouseMoved(MouseEvent e) {
             if (rectangle.contains(e.getPoint())) {
                hover = true;
             } else {
                hover = false;
             }
             repaint();
          }
       }
    
       private static void createAndShowGui() {
          MouseEg mainPanel = new MouseEg();
    
          JFrame frame = new JFrame("MouseEg");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    The code has an inner private class, MyMouseAdapter, that extends MouseAdapter and thus can be used as a MouseListener and a MouseMotionListener, and it is added as both to the main JPanel. In this listener, if the mouse is hovering inside of a rectangle on the GUI, I change a class boolean field, here called "hover", and call repaint.

    The paintComponent then uses the value of hover to decide how to paint.

    If the user clicks inside of the rectangle, I do my logic inside of the mouse listener, not inside of paintComponent. For example in my code, I show a JOptionPane, however in your code you would crate a new Survival object, add it to the GUI. You'd then want to call revalidate and then repaint on the main JPanel that is getting the new Survival component.

  12. #12
    Join Date
    Oct 2011
    Location
    Jakarta.
    Posts
    7
    Rep Power
    0

    Default Re: Java MouseClicked problem.

    hmmm, so you create a new implements called MyMouseAdapter that contains MouseMotionListener, and MouseListener,
    sou you don't need to write "implements MouseMotionListener, MouseListener", isn't it ?,
    and the if method that i put in the paintcomponent, instead of that you put the method in the MouseMotionListner, and MouseListener ?,
    hmmm, i'll try it,
    really - really thanks for your help,
    i really appreciate it,
    thaaaaaaaaaaaaaaaaaaaaaankkkkksss : D.

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

    Default Re: Java MouseClicked problem.

    The MouseAdapter class implements both MouseListener and MouseMotionListener interfaces (among others), and so it can function as either. Also, since it's an abstract class and not an interface, you don't have to implement all of the methods of the interfaces, just the ones that you want to override, so it looks "cleaner".

    Best of luck.

Similar Threads

  1. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  2. Replies: 4
    Last Post: 08-10-2011, 07:10 AM
  3. Problem Display Jmenubar Java Se6 u23 versus Java SE6 u22
    By Ravanelly in forum Advanced Java
    Replies: 0
    Last Post: 01-07-2011, 09:36 AM
  4. MouseClicked Stop Animation
    By xpngamer in forum New To Java
    Replies: 1
    Last Post: 04-14-2009, 09:38 AM
  5. Replies: 6
    Last Post: 04-05-2009, 11:28 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
  •