Open window on mouse co-ordinates - java

I was wondering if it was possible to open a window where the mouse currently is? I have the current mouse co-ordinate but am unable to find what to do with the x y values when displaying my window.
Hopefully someone could point me in the direction of the appropriate method.
Thanks

If you haven't already, using the MouseInfo class will get the x and y position.
Point location = MouseInfo.getPointerInfo().getLocation();
You specified that you want to use a JFrame in this case, so setting the location of the JFrame to this x and y point will do so.
Point location = MouseInfo.getPointerInfo().getLocation();
int x = (int) location.getX();
int y = (int) location.getY();
JFrame frame = new JFrame(); //this is just the initialization of the window
frame.setLocation(x, y);

user1181445 was right, but I think you can set the frame location without creating the variables x and y:
Point location = MouseInfo.getPointerInfo().getLocation();
JFrame frame = new JFrame(); //this is just the initialization of the window
frame.setLocation(location);

Related

Trouble drawing with MouseDragged in Java

there's a problem drawing or Setting size of a JPanel when i drag the mouse,
the location i setted where i click and the size depending of the drag position(X and Y) drawing a resizable rectangle(JPanel).
private void panelMouseDragged(java.awt.event.MouseEvent evt) {
rSX = (int)MouseInfo.getPointerInfo().getLocation().getX();
rSY = (int)MouseInfo.getPointerInfo().getLocation().getY();
rectanguloDefault.setBounds(rX,rY,rSX-rX,rSY-rY);
}
private void panelMousePressed(java.awt.event.MouseEvent evt) {
rX = (int)MouseInfo.getPointerInfo().getLocation().getX();
rY = (int)MouseInfo.getPointerInfo().getLocation().getY();
rectanguloDefault.setLocation(rX,rY);
}
but when i drag mouse in a negative coordinate of the click(Start of drawing) it disappear.
here a better explain
http://i.picasion.com/resize80/49c88c55d4c11c53c020acfcc4fc2f45.png
but when i drag mouse in a negative coordinate
rectanguloDefault.setBounds(rX,rY,rSX-rX,rSY-rY);
Your width/height calculation always assumes you drag the mouse in a positive direction.
You need to use the absolute value of the two points:
int width = Math.abs(rSX - rX);
int height = Math.abs(rSY - rY);
rectanguloDefault.setBounds(rX, rY, width, height);
Your x/y values will also need to be the minimum of (rX and rSX) and (ry and rSY). You can use the Math.min(...) method for this.

How to get the center x and y of desktop with swing

I want my jframe to open in the center of a person's monitor.
(By default a jframe will open at coordinate. (0,0))
To achieve this, before setting the frame visible, I use this method.
this.setLocation(x,y);
In theory, monitor screen sizes can be different, meaning the center coordinate will be different for almost all computers.
HERES MY QUESTION:
How would I get the center coordinate of the computer monitor running the swing application?
How would I get the center coordinate of the computer monitor running the swing application?
You can get the center coordinate with java.awt.Toolkit:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int centerX = screenSize.width/2;
int centerY = screenSize.height/2;
However, you don't need it.
Just use:
yourFrame.setLocationRelativeTo(null);
And it will be automatically centered
Try it
//call `setSize` first
this.setSize(300, 600);
Toolkit tk = this.getToolkit();
Dimension dim = tk.getScreenSize();
int x = (int) dim.getWidth() / 2 - this.getWidth() / 2;
int y = (int) dim.getHeight() / 2 - this.getHeight() / 2;
this.setLocation(x, y);
Here this represents JFrame class object.
JFrame will display in the center of the screen.
Query the Toolkit object.
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int centerX = screenSize.getWidth() / 2;
int centerY = screenSize.getHeight() / 2;
Swing's documentation states that the window will be centered on screen if setRelativeTo is passed null.
public void setLocationRelativeTo(Component c)
As stated in their docs:
"If the component is null, or the GraphicsConfiguration associated with
this component is null, the window is placed in the center of the screen. The center point can be obtained with the GraphicsEnvironment.getCenterPoint method."
http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setLocationRelativeTo(java.awt.Component)
So simply do:
frame.setLocationRelativeTo(null);
..since your question was really how to get it to open in the center, not how to get the center coordinate.

How to get X,Y coordinates of an image in a JLabel

I have an image of a map placed as the icon of a JLabel.
I am using following code to get the X,Y coordinates of the location where the mouse is clicked.
I have put this code in the MouseClick event of the JLabel.
Point point = MouseInfo.getPointerInfo().getLocation();
double X = point.getX();
double Y = point.getY();
but the coordinates depend on the location of the JFrame form. If the form is moved the coordinates change.
Is there anyway I can freeze the JFrame?
Or
Is there anyway I can get a corner of the image as 0,0 and get the other coordinates relative to that? (So I can calculate the actual coordinates)
getLocation returns the mouse co-ordinates relative to the screen. Use the co-ordinates from the MouseEvent instead
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
double x = e.getX();
double y = e.getY();
...
}
});

How could i set a random location for a button in java swing

how would i randomly position a button on a certain portion of the frame. I have tried to set two variables x and y which are random but it doesn't work, the button disappears i'm guessing it is positioned off the screen.
Thi sis what i have tried:
int y = ran.nextInt(0 - frame.getHeight());
int x = ran.nextInt(0 - frame.getHeight());
I am also getting an error 'AWT-EventQueue -0'
Thanks
You have a negetive value in the bracket.
int y = ran.nextInt(frame.getHeight());
int x = ran.nextInt(frame.getWidth());
not frame.getHeight() but frame.getContentPane.getHeight()
required to use AbsoluteLayout for container where is JButton placed
add (re)validate and repaint to container where is JButton placed
use JPanel as container for JButton

How to use MouseListener and MouseMotionListener in java3D to rotate 3D object?

I m making desktop app in java swing. i made 3D image from my 2D image using PointArray[]. now i want to rotate image using MouseListener and MouseMotionListener.I used MouseRotate object to rotate myImage, but it not works well for that, MouseRotate rotate image with origin(0,0,0). but i want to rotate image using center point of image. means rotate image using center point not origin point. So, How can i do that?
Hmm, it's hard to tell without code, but I think you can just set up a transformation matrix and rotate it with that. Assuming the image is facing the front of the screen, you can try something like this:
public void mouseDragged(MouseEvent e)
{
int dx = e.getX() - x; //x should be a global variable
int dy = e.getY() - y; //same applies here
x = e.getX(); //to set x for the next update loop
y = e.getY();
double rotation = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
Transform3D transform = new Transform3D();
Matrix3d matrix = new Matrix3d();
transformG.getTransform(transform); //assuming the TransformGroup your image is in is transformG
transform.getRotationScale(matrix);
transform.rotZ(rotation);
transformG.setTransform(transform);
}
You can set up the rotation amount differently if you want, but this should give you an idea

Categories