Results 1 to 3 of 3
  1. #1
    ptuckley is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default Help with drawing a string using mouse events

    Hey guys, I'm trying to create a string in the position that I click on a JFrame, but I get an error that the values for my x and y cannot be accessed from outside the package and I'm rather confused. I would appreciate any help!

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*;
    
    public class adventure
    extends JFrame implements MouseListener
    	{
    		String message = " ";
    		
    		public adventure()
    			{
    				setSize(500,500);
    				setVisible(true);
    				show();
    				addMouseListener(this);
    			}
    		
    		public void mouseClicked(MouseEvent e)
    			{
    				int x = e.getX(), y = e.getY();
    				message="Click";
    				repaint();
    			}
    		
    
    		public void mouseExited(MouseEvent e) {}
    		public void mouseEntered(MouseEvent e) {}
    		public void mouseReleased(MouseEvent e) {}
    		public void mousePressed(MouseEvent e) {}
    
    		public void paint(Graphics g)
    			{
    				super.paint(g);
    				g.drawString(message, x, y);
    			}
    	}

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

    Default

    Your x and y variables are declared inside the mouseClicked method and are only visible inside of that method. You want to make these variables non-static class variables so that they're visible throughout the class.

  3. #3
    ptuckley is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Help with some basics please (mouse events)
    By tigersarehot in forum AWT / Swing
    Replies: 4
    Last Post: 04-28-2010, 02:22 AM
  2. Replies: 1
    Last Post: 07-16-2009, 02:15 PM
  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

Posting Permissions

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