Constantly updating JFrame text - java

I am new to java development, and decided to create a simple java project that outputs the X and Y coordinates of where the mouse is inside of a jframe. I am unsure how I would use a listener.
package main;
import java.awt.BorderLayout;
import java.awt.MouseInfo;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class Main {
public static void main(String[] args) {
jFrame();
}
public static void jFrame() {
JFrame myFrame = new JFrame("Screen Location");
myFrame.setVisible(true);
myFrame.setBounds(0, 0, 300, 100);
JLabel myText = new JLabel(""+ getTotal(),
SwingConstants.CENTER);
myFrame.getContentPane().add(myText, BorderLayout.CENTER);
}
private static String getTotal() {
for (int i = 0; i < 1;) {
int publicScreenValueX = MouseInfo.getPointerInfo().getLocation().x;
int publicScreenValueY = MouseInfo.getPointerInfo().getLocation().y;
String total = new String(publicScreenValueX + ", " + publicScreenValueY);
return total;
}
return null;
}
}
How would I be able to make the project constantly update the coordinates? Could someone provide a reference on how to do this?
Do note, this is without using a button, or without any input from the user, other than moving the mouse.
Here is an image of what this would look like:
Except the coordinates would always be updating.

Basically all you have to do in order to create a JFrame that handles mouse events is:
1 - Create a class that extends JFrame and implements MouseListener
2 - #Override mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased to monitor the coresponding events. Now every time one of these events occures, the respective function will fire up.
Use MouseEvent.getX(), MouseEvent.getY() to get the coordinates of the window that the mouse event occurs.
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
public class CreateJFrameWindowWithMouseEventHandling extends JFrame implements MouseListener {
private static final long serialVersionUID = 1L;
public CreateJFrameWindowWithMouseEventHandling() {
setTitle("Simple Frame");
addMouseListener(this);
}
#Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
JOptionPane.showMessageDialog(null, "Mouse Clicked at X: " + String.valueOf(x) + " - Y: " + String.valueOf(y));
}
#Override
public void mouseEntered(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Entered frame at X: " + x + " - Y: " + y);
}
#Override
public void mouseExited(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Exited frame at X: " + x + " - Y: " + y);
}
#Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Pressed at X: " + x + " - Y: " + y);
}
#Override
public void mouseReleased(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Released at X: " + x + " - Y: " + y);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new CreateJFrameWindowWithMouseEventHandling();
//Display the window.
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

I would advise on creating a new class that inherits from JFrame and all that, because it's way more Object Oriented friendly.
However, with your code, what you need to do is add a MouseMotionListener to your JFrame, implement all abstract methods and, on the method mouseMoved, change your myText Label's text;
Replace the method below with the one you currently have and it should work.
Please note that, java.awt.event.MouseMotionListener is only going to give you information about mouse movement inside your application window. For events that occur outside that window, you might want to check this other question:
Java mouse motion anywhere on screen
public static void jFrame() {
JFrame myFrame = new JFrame("Screen Location");
myFrame.setLocationRelativeTo(null);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // makes the application close when you close the JFrame
myFrame.setVisible(true);
myFrame.setBounds(0, 0, 300, 100);
JLabel myText = new JLabel(""+ getTotal(), SwingConstants.CENTER);
myFrame.getContentPane().add(myText, BorderLayout.CENTER);
myFrame.addMouseMotionListener(new MouseMotionListener(){
#Override
public void mouseDragged(MouseEvent e) {
// don't delete this method
// all abstract methods must be overridden
}
#Override
public void mouseMoved(MouseEvent e) {
myText.setText(getTotal());
}
});
}

Related

MouseListener on JFrame never being called

I'm trying to use a MouseListener to see when something is clicked in my JFrame, but none of the events (mouseClicked, mouseEntered, etc) are ever called. I'm using a Canvas to draw the game onto the JFrame and I'm wondering if that could be the issue, but I'm not sure how to fix it. I was following this tutorial and my code looks almost exactly the same.
Here's my JFrame class:
public class Display extends JFrame implements MouseListener {
public static int width;
public static int height;
public static final int CARD_SIZE = 100;
public static final int BUFFER_SIZE = 25;
public static final int TITLE_SIZE = 50;
private JFrame frame;
private MyCanvas canvas;
public Display (int r, int c, Card[][] b) {
width = c*CARD_SIZE + (c+2)*BUFFER_SIZE;
System.out.println(width);
height = r*CARD_SIZE + (r+1)*BUFFER_SIZE + TITLE_SIZE*2;
System.out.println(height);
frame = new JFrame("Matching Game");
frame.setLayout(null);
frame.setSize(width, height);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setFocusable(true);
frame.addMouseListener(this);
canvas = new MyCanvas(b);
frame.add(canvas);
frame.setVisible(true);
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at X: " + e.getX() + " - Y: " + e.getY());
}
#Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse Entered frame at X: " + e.getX() + " - Y: " + e.getY());
}
#Override
public void mouseExited(MouseEvent e) {
System.out.println("Mouse Exited frame at X: " + e.getX() + " - Y: " + e.getY());
}
#Override
public void mousePressed(MouseEvent e) {
System.out.println("Mouse Pressed at X: " + e.getX() + " - Y: " + e.getY());
}
#Override
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse Released at X: " + e.getX() + " - Y: " + e.getY());
}
}
The primary thing you were doing wrong was to extend JFrame and also create an explicit instance of one. Do it as follows.
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Display implements MouseListener {
public static int width;
public static int height;
public static final int CARD_SIZE = 100;
public static final int BUFFER_SIZE = 25;
public static final int TITLE_SIZE = 50;
private JPanel canvas;
private JFrame frame;
public static void main(String[] args) {
new Display(3,3,new int[3][3]);
}
public Display (int r, int c, int[][] b) {
width = c*CARD_SIZE + (c+2)*BUFFER_SIZE;
System.out.println(width);
height = r*CARD_SIZE + (r+1)*BUFFER_SIZE + TITLE_SIZE*2;
System.out.println(height);
frame = new JFrame("Matching Game");
// frame.setLayout(null);
// frame.setSize(width, height);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setFocusable(true);
frame.addMouseListener(this);
canvas = new JPanel();
canvas.setPreferredSize(new Dimension(width,height));
frame.add(canvas);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at X: " + e.getX() + " - Y: " + e.getY());
}
#Override
public void mouseEntered(MouseEvent e) {
System.out.println("Mouse Entered frame at X: " + e.getX() + " - Y: " + e.getY());
}
#Override
public void mouseExited(MouseEvent e) {
System.out.println("Mouse Exited frame at X: " + e.getX() + " - Y: " + e.getY());
}
#Override
public void mousePressed(MouseEvent e) {
System.out.println("Mouse Pressed at X: " + e.getX() + " - Y: " + e.getY());
}
#Override
public void mouseReleased(MouseEvent e) {
System.out.println("Mouse Released at X: " + e.getX() + " - Y: " + e.getY());
}
}
Points to consider
don't extend JFrame, just use an instance.
Canvas is old. I recommend using a JPanel to hold the components and/or painting
frame.setLocationRelativeTo(null) centers your frame on the screen.
Since I don't have the Card class I used int[][] to demonstrate this.
Anything else I did, check out the API documentation for explanations.
For more on GUI's, etc, check out the Java Tutorials

java mouselistener on Line2d vector

I am not very experimented with listeners. I have a Vector of Line2D (representating a floorplan walls). I draw them in a JFrame.
I try to set a listener on those lines to be able to set them (change color etc) on click.
I am lost because I don't really know where to put the listener. I tried many things I found on many forums but nothing works really.
Thanks.
Edit1:
hi thank you for your answer it's a kind of big projects with many classes so i will post the code and describe how it wokrs . First . I import an image with a floorplan. I have a class with Actions that extends AbstractAction. I use opencv for for lines extraction . I store the extracted lines in a Vector of Walls (class i created).
Vector<wall> walls
And here is my wall class
//a wall is represented by 2 points (pInitial and pFInal)
private Point pInitial, pFinal;
private double wallAttenuation;
private int wallType;
public wall(Point p1, Point p2) {
// p1 is lower by the height than p2
if (p1.getY() > p2.getY()) {
pInitial = p2;
pFinal = p1;
} else {
pInitial = p1;
pFinal = p2;
}
}
//method to set the attenuation of the wall
public void setWallAttenuation(double a) {
wallAttenuation = a;
}
//methods to set both ends of the wall
public void setPInitial(Point P1) {
pInitial.x=(int)P1.getX();
pInitial.x=(int)P1.getY();
}
public void setPFinal(Point P2) {
pFinal.x=(int)P2.getX();
pFinal.x=(int)P2.getY();
}
//methods to get wall attributes (attenuation , ends and type)
public double getWallAttenuation() {
return wallAttenuation;
}
public Point getPInitial() {
return pInitial;
}
public Point getPFinal() {
return pFinal;
}
public void setWallType(int type) {
wallType = type;
}
public int getWallType() {
return wallType;
}
now in my importAction class, and after processing I have this vector of walls (each wall defiened by start and end point . After that , I open a new Frame in which i draws the lines. I want to be able to click on each wall for further modifications.
First i tried to create a Frame class with mouse event handling like this :
public class CreateJFrameWindowWithMouseEventHandling extends JFrame implements MouseMotionListener,MouseListener {
private static final long serialVersionUID = 1L;
public CreateJFrameWindowWithMouseEventHandling() {
setTitle("Walls calibration");
addMouseListener(this);
}
#Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Clicked at X: " + x + " - Y: " + y);
}
#Override
public void mouseEntered(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Entered frame at X: " + x + " - Y: " + y);
}
#Override
public void mouseExited(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Exited frame at X: " + x + " - Y: " + y);
}
#Override
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Pressed at X: " + x + " - Y: " + y);
}
#Override
public void mouseReleased(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse Released at X: " + x + " - Y: " + y);
}
#Override
public void mouseDragged(MouseEvent e) {
}
#Override
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("Mouse at X: " + x + " - Y: " + y);
repaint();
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new CreateJFrameWindowWithMouseEventHandling();
//Display the window.
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
but i guess it wasnt the right solution . next thing i tried was to create JPanel browse the vector , draw the current line and add a listener. to check if it works i change the cursor if the mouse is over a line :
JFrame wallsEdit = new JFrame("Walls Calibration");
wallsEdit.add(new JPanel() {
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
for(int i=0;i<walls.size();i++)
{
Point p1 = walls.get(i).getPInitial();
Point p2 = editor.walls.get(i).getPFinal();
g2.setColor(Color.black);
g2.setStroke(new BasicStroke(10));
g2.drawLine((int)p1.getX(),(int)p1.getY(), (int)p2.getX(),(int)p2.getY());
walls.get(i).addMouseListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
Point CursorPoint= MouseInfo.getPointerInfo().getLocation();
double x = CursorPoint.getX();
double y = CursorPoint.getY();
Line2D.Double current = new Line2D.Double(p1.getX(),p1.getY(),p2.getX(),p2.getY()) ;
if(current.intersectsLine(x, y, x, y))
{ setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
} else { setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
repaint();
});
}
}
}, BorderLayout.CENTER);
wallsEdit.setAlwaysOnTop(true);
wallsEdit.setVisible(true);
wallsEdit.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
and of course this doesnt work. so tried another thing. i declared my Wall class extends Component and implements mouseMotionListener and MouseListener. inside I wrote the mouseMoved event like for the panel .
hope that i gave all needed details . any help would be appreciated thank you very much

Overrided paint method not being called after repaint is

My problem is essentially creating a physics engine with a few different scenarios. I would like to consolidate the different scenarios into one window by having buttons that run each individually. The frame works properly and the buttons show up and can be pressed; however, that print line in the pain method is never happening and from there I concluded that paint is not being called even after repaint is. I understand that they are not the same but I don't understand why paint isn't being accessed in this instance compared to others.
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JButton;
public class PhysicsEngine extends JPanel{
double x ,y;
JFrame frame;
JPanel pan;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JButton b5;
public static void main(String[] args){
PhysicsEngine gui = new PhysicsEngine();
}
public PhysicsEngine(){
frame = new JFrame("Ball Engine");
pan = new JPanel();
frame.add(pan);
b1 = new JButton("1");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event){
try{
startFirst();
}
catch(InterruptedException exception){}
}
});
pan.add(b1);
b2 = new JButton("2");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try{
startSecond();
}
catch(InterruptedException exception){}
}
});
pan.add(b2);
b3 = new JButton("3");
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try{
startThird();
}
catch(InterruptedException exception){}
}
});
pan.add(b3);
b4 = new JButton("4");
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try{
startFourth();
}
catch(InterruptedException exception){}
}
});
pan.add(b4);
b5 = new JButton("5");
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try{
startFifth();
}
catch(InterruptedException exception){}
}
});
pan.add(b5);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
System.out.println(""+y);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fillOval((int)x, (int)y, 30, 30);
}
public void startFirst() throws InterruptedException{
x = 300;
for(int t = 1;;t++){
//xPos= 0*t*t + 0*t + 300 this is constant at 300
if(y>=615) break; //stops the loop when the ball is off the screen
y = .1*t*t + 0*t + 80; //parametric equation for y
repaint();
Thread.sleep(10);
}
}
public void startSecond() throws InterruptedException{
x = 300;
for(int t = 1;;t++){
//xPos= 0*t*t + 0*t + 300 this is constant at 300
if(y>=615) break; //stops the loop when the ball is off the screen
y = .1*t*t - 10*t + 550; //parametric equation for y
repaint();
Thread.sleep(10);
}
}
public void startThird() throws InterruptedException{
for(int t = 1;;t++){
if(y>=615||x>=615) break; //stops the loop when the ball is off the screen
y = .1*t*t - 10*t + 550; //parametric equation for y
x = 0*t*t + 5*t + 50; //parametric equation for x
repaint();
Thread.sleep(10);
}
}
public void startFourth() throws InterruptedException{
for(int t = 1;;t++){
//xPos= 0*t*t + 0*t + 300 this is constant at 300
if(y>=615||x>=615) break; //stops the loop when the ball is off the screen
y = t*t*t + 50; //given parametric equation for y
x = t - 4; //given parametric equation for x
repaint();
Thread.sleep(10);
}
}
public void startFifth() throws InterruptedException{
for(int t = 1;t<500 /* goes for 5 seconds */;t++){
y = 200*Math.sin(t) + 300; //given parametric equation for y
x = 200*Math.cos(t) + 300; //given parametric equation for x
repaint();
Thread.sleep(10);
}
}
}
The basic problem is you are overriding the paint() method of the PhysicsEngine class. But you never add an instance of this class to the frame.
However, the bigger problem is the structure of your class. Your main class should not be extending a JPanel just so you can create a JFrame. The logic for creating the frame should be in the main() method and then your PysicsEngine panel should contain all the components you want to build for your frame. Also, custom painting should be done by overriding the paintComponent(...) method, not the paint() method.
Read the section from the Swing tutorial on Custom Painting for basic painting information and demos.
Then you can look at other sections in the tutorial for a better way to structure your code. For example the ButtonDemo code found in the How to Use Buttons tutorial will show you how to extend a JPanel and add buttons to it.
repaint() stores a request for refreshing a component, request that will be executed by the GUI thread later on. But a component will be refreshed only if it is visible on screen, alas your PhysicsEngine class doesn't seem to be used as a visible component in some visible GUI hierarchy.
Your paint method should be paintComponent and also should call the paintComponent method of super class.
And you don't add the PhysicsEngine JPanel into the JFrame, and it is very bad approach to create a JFrame in the constructor of JPanel subclass. I create a JFrame in the main method and create a object of PhysicsEngine (JPanel Subclass) and add it into the JFrame. I add the buttons by using this which reference to the PhysicsEngine (JPanel subclass) object.
Here is the working code:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
public class PhysicsEngine extends JPanel {
double x, y;
JPanel pan;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JButton b5;
public static void main(String[] args) {
JFrame frame = new JFrame("Ball Engine");
PhysicsEngine gui = new PhysicsEngine();
frame.add(gui, BorderLayout.CENTER);
frame.setSize(600, 600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public PhysicsEngine() {
b1 = new JButton("1");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
startFirst();
} catch (InterruptedException exception) {
}
}
});
this.add(b1);
b2 = new JButton("2");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
startSecond();
} catch (InterruptedException exception) {
}
}
});
this.add(b2);
b3 = new JButton("3");
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
startThird();
} catch (InterruptedException exception) {
}
}
});
this.add(b3);
b4 = new JButton("4");
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
startFourth();
} catch (InterruptedException exception) {
}
}
});
this.add(b4);
b5 = new JButton("5");
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
startFifth();
} catch (InterruptedException exception) {
}
}
});
this.add(b5);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("" + y);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fillOval((int) x, (int) y, 30, 30);
}
public void startFirst() throws InterruptedException {
x = 300;
for (int t = 1;; t++) {
// xPos= 0*t*t + 0*t + 300 this is constant at 300
if (y >= 615)
break; // stops the loop when the ball is off the screen
y = .1 * t * t + 0 * t + 80; // parametric equation for y
repaint();
Thread.sleep(10);
}
}
public void startSecond() throws InterruptedException {
x = 300;
for (int t = 1;; t++) {
// xPos= 0*t*t + 0*t + 300 this is constant at 300
if (y >= 615)
break; // stops the loop when the ball is off the screen
y = .1 * t * t - 10 * t + 550; // parametric equation for y
repaint();
Thread.sleep(10);
}
}
public void startThird() throws InterruptedException {
for (int t = 1;; t++) {
if (y >= 615 || x >= 615)
break; // stops the loop when the ball is off the screen
y = .1 * t * t - 10 * t + 550; // parametric equation for y
x = 0 * t * t + 5 * t + 50; // parametric equation for x
repaint();
Thread.sleep(10);
}
}
public void startFourth() throws InterruptedException {
for (int t = 1;; t++) {
// xPos= 0*t*t + 0*t + 300 this is constant at 300
if (y >= 615 || x >= 615)
break; // stops the loop when the ball is off the screen
y = t * t * t + 50; // given parametric equation for y
x = t - 4; // given parametric equation for x
repaint();
Thread.sleep(10);
}
}
public void startFifth() throws InterruptedException {
for (int t = 1; t < 500 /* goes for 5 seconds */; t++) {
y = 200 * Math.sin(t) + 300; // given parametric equation for y
x = 200 * Math.cos(t) + 300; // given parametric equation for x
repaint();
Thread.sleep(10);
}
}
}

When clicking label appear 2 times

I try to do make when we click the frame, it shows the coordinates of the click point. And i did this:
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
j1.setLocation(x, y);
j1.setText("(" + x + ", " + y + ")");
}
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
j1.setLocation(x, y);
j1.setText("(" + x + ", " + y + ")");
}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
But when i click it shows the coordinates first at the center then the click point, 2 times. But i just want only one and at the click point. Where did i mistake, what shoul i do?
Just use mousePressed and that's it. When you click the mouse, 3 methods are often called -- mousePressed, mouseReleased and if the mouse does not move between press and release, mouseClicked. You want to respond to just one of these methods.
So change to:
public void mouseClicked(MouseEvent e) {} // leave this empty
public void mousePressed(MouseEvent e) {
int x = e.getX();
int y = e.getY();
j1.setLocation(x, y);
j1.setText("(" + x + ", " + y + ")");
// also be sure to tell your container to re-position components
revalidate();
repaint();
}
As a side recommendation, note that rather than having a class implement MouseListener, you could have it extend MouseAdapter, and that way your class wouldn't have to have all those empty MouseListener interface methods.
For example:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
#SuppressWarnings("serial")
public class MousePosition extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = PREF_W;
// format String for display String
protected static final String FORMAT = "(%d, %d)";
private int xPos = -40;
private int yPos = -40;
private String displayText = "";
public MousePosition() {
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
xPos = e.getX();
yPos = e.getY();
// use FORMAT String to create our display text
displayText = String.format(FORMAT, xPos, yPos);
repaint();
}
});
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString(displayText, xPos, yPos);
}
private static void createAndShowGui() {
MousePosition mainPanel = new MousePosition();
JFrame frame = new JFrame("MousePosition");
frame.setDefaultCloseOperation(JFrame.DISPOSE_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();
}
});
}
}

JFrame wrong location with Ubuntu (Unity ?)

It seems that there is a bug with Ubuntu (maybe only unity). The decoration of the JFrame is taken into account for getLocation() and getSize(), but not for setLocation() and setSize(). This leads to weird behaviour. For instance, if you use pack() after the frame is displayed and the dimensions changed, the frame will go down 20 pixels...
To illustrate a concrete case when it becomes really annoying, I made a SSCCE. It's a JFrame with a basic JPanel. If you drag the panel, the JFrame is supposed to move along.
Under Windows, it works as expected. Under Ubuntu, if I do setUndecorated(true) it will also work fine, but if I let the decoration, the JFrame turn crazy !
public class Test {
private static JFrame mainFrame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
mainFrame = new JFrame("test");
mainFrame.setSize(300,20);
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mainFrame.setVisible(true);
Container pane = mainFrame.getContentPane();
pane.addMouseMotionListener(new MouseMotionListener() {
private int posX = 0, posY = 0;
#Override
public void mouseDragged(MouseEvent e) {
int x = e.getX() - posX + mainFrame.getX();
int y = e.getY() - posY + mainFrame.getY();
mainFrame.setLocation(x, y);
}
#Override
public void mouseMoved(MouseEvent e) {
posX = e.getX();
posY = e.getY();
}
});
}
});
}
}
I don't know how I can fix that. How can I get the size of the windows decoration ? And I have no idea about which versions of Ubuntu are concerned. And if it is only a Unity problem, I don't even know how to find out if my user is using Unity...
Any idea for a workaround ?
Edit :
Ok, MadProgrammer did provide a better code, but the bug still occurs sometimes. I edited my MouseListener accordingly to track the bug :
pane.addMouseMotionListener(new MouseMotionListener() {
private int posX = 0, posY = 0;
#Override
public void mouseDragged(MouseEvent e) {
int x = e.getXOnScreen() - posX;
int y = e.getYOnScreen() - posY;
mainFrame.setLocation(x, y);
System.out.println("drag : border ignored / border considered : "+(mainFrame.getY()+e.getY())+" / "+e.getYOnScreen());
}
#Override
public void mouseMoved(MouseEvent e) {
posX = e.getXOnScreen() - mainFrame.getX();
posY = e.getYOnScreen() - mainFrame.getY();
System.out.println("move : border ignored / border considered : "+e.getY()+" / "+posY);
}
});
Each time that the 2 values are identical, it means that the bug will occur on the next click. Otherwise, the values are different. On other OS, the values are always the same. Actually, they should be or the same always, or always different. I don't understand how they can be sometimes equal and sometimes different...
I don't have Ubuntu to test with, but I've used something similar to this on both MacOS and Windows
import java.awt.Container;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Test {
private static JFrame mainFrame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
mainFrame = new JFrame("test");
mainFrame.setSize(300, 100);
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mainFrame.setVisible(true);
Container pane = mainFrame.getContentPane();
MouseAdapter ma = new MouseAdapter() {
private Point offset;
#Override
public void mouseDragged(MouseEvent e) {
if (offset != null) {
Point pos = e.getLocationOnScreen();
int x = pos.x - offset.x;
int y = pos.y - offset.y;
System.out.println(x + "x" + y);
SwingUtilities.getWindowAncestor(e.getComponent()).setLocation(x, y);
}
}
#Override
public void mousePressed(MouseEvent e) {
Point pos = SwingUtilities.getWindowAncestor(e.getComponent()).getLocation();
// Point pos = e.getComponent().getLocationOnScreen();
offset = new Point(e.getLocationOnScreen());
System.out.println(pos + "/" + offset);
offset.x -= pos.x;
offset.y -= pos.y;
System.out.println(offset);
}
};
pane.addMouseListener(ma);
pane.addMouseMotionListener(ma);
}
});
}
}
This should work for both decorated and undecorated windows, as it takes the difference between the positions of the component (on the screen) and the windows current position. When dragged, it calculates the distance of movement from the click point and updates the window's location accordingly (allowing for the original offset of the click)

Categories