Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

MousePressed/ MouseUp...

Posted on 2011-10-07
18
Medium Priority
?
415 Views
Last Modified: 2012-05-12
The partial code below runs

str="("+e.getX()+", "+e.getY()+")";

to show a message on the screen on mousePressed().

Question: How to code mouseUp() to make the message to disapear?

Thank you.
public MovableMessagePanel(String s){
	
            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    
                    x=e.getX();
                    y=e.getY();  
                    str="("+e.getX()+", "+e.getY()+")";
                    repaint();
                }
                public void mouseUp(MouseEvent e){
                    
                    x=e.getX();
                    y=e.getY();  
//                    str="("+e.getX()+", "+e.getY()+")";
                    repaint();
                }                
            });

Open in new window

0
Comment
Question by:Mike Eghtebas
  • 7
  • 6
  • 3
  • +1
18 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36931694
Do
str=null; //(Only paint if not empty or null)
repaint();

Open in new window

0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 36931767
How to know when Mouse is up to reset to str=" " ?
0
 
LVL 16

Expert Comment

by:imladris
ID: 36931812
In general, in a GUI environment, one should not assume that what you put on the screen will persist. Your window can, for instance, be minimized or overlayed by another window. In addition the original designers of GUI systems wanted to allow for dynamic, real time, changes to windows.

So, the common idea is that there is central code, or a hierarchy of code, that is responsible for repainting the screen. This code can (and will) be called by the operating system at any time to "refresh" the image in the window. In Java this is even done directly and asynchronously, in a separate thread.

Therefore, in your case, the concept would be that the central paint method (or hierarchy) has access to a state variable (e.g. String mousepos;) that supplies the position to be displayed. Then when you get a Mouse Pressed event you set the mousepos String and force a repaint. For a Mouse Up event you set the mousepos String to null and force a repaint. The repaint will cause the paint code to be executed.
Meanwhile, in the paint code, you check if the string is null and, if it isn't, then you display it. Since a repaint normally starts by blanking the window this will cause the display to be cleared if the string is null, and to show (or change) the string if it is not.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 36931820
>>How to know when Mouse is up to reset to str=" " ?


It will be up when mouseUp is called
0
 
LVL 16

Expert Comment

by:imladris
ID: 36931821
You would set "mousepos" to null in the mouseUp listener method.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 36931878
Could you revise the submitted code?
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 36931893
I am getting:

System.out.println("Down");

but not:

System.out.println("up");

in the code below.
public MovableMessagePanel(String s){
	
            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                    System.out.println("Down");
                    x=e.getX();
                    y=e.getY();  
                    str="("+e.getX()+", "+e.getY()+")";
                    repaint();
                }               
            });
            
            addMouseListener(new MouseAdapter(){
                public void mouseUp(MouseEvent e){
                    System.out.println("Up");
                    x=e.getX();
                    y=e.getY();  
//                    str="("+e.getX()+", "+e.getY()+")";
                    repaint();
                }                
            });

Open in new window

0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 36931921
re:> You would set "mousepos" to null in the mouseUp listener method.

How? Could you revise the code I have submitted above so it will do this?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36932010
Try the following
import java.awt.*;
import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;


public class F extends JFrame {
    private void setGui() {
        try {
            setLocation(0, 100);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            Container cp = getContentPane();
	    cp.add(new MovableMessagePanel("PAINT ME!"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                        F f = new F();
                        f.setGui();
                        f.setSize(200, 200);
                        f.setVisible(true);
                    }
                });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class MovableMessagePanel extends JPanel {
        private int x;
        private int y;
        private String str;

        public MovableMessagePanel(String s) {
            addMouseListener(new MouseAdapter() {
                    public void mousePressed(MouseEvent e) {
                        System.out.println("Down");
                        x = e.getX();
                        y = e.getY();
                        str = "(" + e.getX() + ", " + e.getY() + ")";
                        repaint();
                    }
                });

            addMouseListener(new MouseAdapter() {
                    public void mouseUp(MouseEvent e) {
                        System.out.println("Up");
                        x = e.getX();
                        y = e.getY();
                        str = null;
                        repaint();
                    }
                });
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            if (str != null) {
                g.drawString(str, x, y);
            }
        }
    }
}

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 1000 total points
ID: 36932062
Try this simple code - you press the mose on the window - it shows its position - you reklaese the mouse -
the position string disapperas


import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MousePressedUp extends JFrame implements MouseListener {
           MyMousePanel mmp;
    public MousePressedUp(){

         mmp = new MyMousePanel();
          mmp.addMouseListener(this);
        Container c = this.getContentPane();
        c.add(mmp);
        this.setSize(300,300);
        this.setVisible(true);

    }



  public void mouseClicked(MouseEvent me){}

      public void mousePressed(MouseEvent me){

                  mmp.setString(me.getPoint().getX() + "  "  +  me.getPoint().getY());
           repaint();
      }

      public void mouseReleased(MouseEvent me){
                mmp.setString("");
              repaint();


      }
      public void mouseEntered(MouseEvent me){}
      public void mouseExited(MouseEvent me){}

    public static void main(String[] args) {
        new MousePressedUp();
 }
}

class MyMousePanel extends JPanel {
    String s = "";

  public void setString(String s){
      this.s = s;
  }
    public void paint(Graphics g){

        g.drawString(s, 50,50 );

    }

    

}

Open in new window

0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 36932065
CEHJ,

Thank you for the sample code.

System.out.println("Down"); is firing but
System.out.println("Up"); is not.

The same problem I had originally.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36932078
Dont think there is method MouseUP
There is method mouseReleased()
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36932079
Try my code - it does what you want
0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 36932082
Thank you
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36932117
Sorry - that should have been
addMouseListener(new MouseAdapter() {
 56                     public void mouseReleased(MouseEvent e) {
 57
 58                         System.out.println("Up");
 59                         x = e.getX();
 60                         y = e.getY();
 61                         str = null;
 62                         repaint();
 63                     }
 64                 });

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36932137
Copy/paste error. That's why you should use @Override (following won't compile)
@Override
                    public void mouseUp(MouseEvent e) {

                        System.out.println("Up");
                        x = e.getX();
                        y = e.getY();
                        str = null;
                        repaint();
                    }
                });

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36932144
As for the accepted code:

a. in Swing, you should override paintComponent, not paint
b. don't make an unnecessary drawString call if the String doesn't need to be painted
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question