My aim is to read coordinate point values from a graph in a Java program. I don't know if it even needs to be a graph. The user clicks on various parts of a plane, and I need to know which places were clicked pixel-wise so that I can find the distances between those points. Are there any simple Java libraries for accomplishing this task?
Just add a mouse listener to a JPanel.
In the mouse listener, you can do something like the following:
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked at (" + e.getX() +"," +
+ e.getY + ")");
}
Related
Comparing Two Specific Pixels
I am trying to compare two specific pixel values by storing them both and eventually comparing the difference between them.
The code below shows what I currently have, (image_Display being my component) and then when the user clicks a location on the image the pixel location is stored. However, how do I do this if I want to collect numerous pixel values?
image_Display.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
x=e.getX();
y=e.getY();
System.out.println(x + "," + y);
}
Desired output:
Saving the location of 2 separate pixels. I'm sorry this is really obvious - I am new to event's.
How am I able to save the location of a location rather than overwriting it each time?
Thank you
If you need only previous coordinates then why don't keep them like this:
int previusX,previousY;
image_Display.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
previousX=x;
previousY=y;
x=e.getX();
y=e.getY();
System.out.println(x + "," + y+" ["+previousX+","+previousY+"]");
}
}
If you need more - use Collections and store few of MouseEvnets (as much as you need).
I've been trying for the past few days to put simple collision into my simple game. I've looked in lots of places all over the internet but I just haven't found anything to be helpful. I'm just using bounding boxes and all i want the player box to do is stop when it hits the block from any of the four sides (and not get stuck or bounce against the block). Doesn't sound so difficult but I've found it incredibly difficult. I really just wanna know the code that i need to put in the if statement's brackets, I've read and heard all the theory I can take. Please help if you can.
if(bb_collide(player,block)){ //detection works just fine,
//just not sure what to do afterwards
}
else{
player.setX((player.getX() + velx));
player.setY((player.getY() - vely));
player.set_coll(player.getX(),player.getY(),player.getWidth(),player.getHeight());
block.set_coll(block.getX(),block.getY(),block.getWidth(),block.getHeight());
}
I actually figured it out, I was just having a couple of bad days, sorry if I had like a bad attitude or something but i got it to do what i wanted it to. Heres what I did if anyone's interested because it works very well for one moving object (mover) bumping into another stationary object (sitter). It finds the distance between the sides that will collide and sets the velocity to that distance minus a few so it doesnt overlap. I had the bounding boxes rendering so i could see they overlapped. Thanks for your replies!
public void Fix_Vel(Player mover, Terrain sitter){
if(sitter.coll_box.contains(mover.getX() + mover.getHeight() + mover.getVelX(), mover.getY()) ||
sitter.coll_box.contains(mover.getX() + mover.getHeight() + mover.getVelX(), mover.getY() + mover.getHeight()) ){ //mover top right and bottom right coords right collision testing
mover.setVelX(sitter.getX() - (mover.getX() + mover.getWidth()) - 2);
}
if(sitter.coll_box.contains(mover.getX() + mover.getVelX(), mover.getY()) ||
sitter.coll_box.contains(mover.getX() + mover.getVelX(), mover.getY() + mover.getHeight()) ){ //mover top left + bottom left coords left collision testing
mover.setVelX((sitter.getX() + sitter.getWidth()) - mover.getX() + 2);
}
if(sitter.coll_box.contains(mover.getX(), mover.getY() + mover.getVelY()) ||
sitter.coll_box.contains(mover.getX() + mover.getWidth(), mover.getY() + mover.getVelY())){ //mover top left + top right coords top collision testing
mover.setVelY((sitter.getY() + sitter.getHeight()) - mover.getY() + 2);
}
if(sitter.coll_box.contains(mover.getX(), mover.getY() + mover.getHeight() + mover.getVelY()) || //mover bottom left and bottom right coords bottom collision testing
sitter.coll_box.contains(mover.getX() + mover.getWidth(), mover.getY() + mover.getHeight() + mover.getVelY())){
mover.setVelY(sitter.getY() - (mover.getY() + mover.getHeight()) - 2);
jumping = false;
if(mover.getVelY() < 0)
mover.setVelY(0);
}
}
You don't say how each box is controlled, so I'm not sure exactly what you want. If detection works, simply don't allow an object to move if it will cause a collision.
PS: I really just wanna know the code that i need to put in the if statement's brackets, I've read and heard all the theory I can take. To be honest, with that attitude, you're not going to get very far and you're always going to need people's help. Game programming requires a decent amount of mathematical intuition. Trigonometry, calculus and linear algebra are essential.
Maybe you want to do something like this?
if(bb_collide(player,block)){
velx = 0;
vely = 0;
}
else{
player.setX((player.getX() + velx));
player.setY((player.getY() - vely));
player.set_coll(player.getX(),player.getY(),player.getWidth(),player.getHeight());
block.set_coll(block.getX(),block.getY(),block.getWidth(),block.getHeight());
}
I am working on a first person game in Java, and I am trying to get the 3D movement working.
My problem is I would like to capture mouse movement, yet keep the mouse inside the window. After I capture the mouse movement, I figure the best way to keep the mouse in my window is to center the mouse in the window after moving, using Robot.moveMouse(x,y). This works fine, however the movement from the Robot triggers an event in my window which then gets interpreted as a normal event, and thus moves my character in the world.
I've tried various schemes of keeping state and ignoring movements until I am in the center, but they all seem finicky and don't quite detect which events are user vs Robot controlled.
Is there an easy way to detect that a mouse movement came from the Robot?
Is there perhaps a simpler way to solve my problem that I am overlooking?
I solved this by switching to NEWT with JOGL 2.0 RC4. In particular, I use GLWindow and warpPointer instead of an AWT Frame with the Robot.mouseMove. With the switch, I instantly got smooth movements. Some sample code similar to what I'm doing (mileage may vary):
public class MyClass implements MouseListener {
private GLWindow window;
private int centeredX = -1;
private int centeredY = -1;
// ...
public void mouseMoved(MouseEvent e) {
if (centeredX == -1 || centeredY == -1) {
center();
return;
}
int deltaX = e.getX() - centeredX;
int deltaY = e.getY() - centeredY;
// ... Do something with the deltas
centeredX = window.getWidth() / 2;
centeredY = window.getHeight() / 2;
window.warpPointer(centeredX, centeredY);
}
}
Well, I'm not 100% about this, but have you used the getsource() or getComponent() functions on your mouse event? They may return the robot as the source of it. Barring that, I would have a class variable like boolean robotControlling and anytime it takes control of the mouse, set that to true. Then, in you mouseListener, do a if(!robotControlling){...}. Hope this helps.
EDIT: if you have unused mouse buttons in your application (Java has Button 1, Button 2 and Button 3), you could make the robot press that, and in your mouse listener ignore any events with that code pressed. (use evt.getButton() for this) Of course, thats not exactly the cleanest solution :P
I have a map applet, and I have a JLabel following the mouse, whenever the mouse goes over a city the JLable displays the the name of the city and population.
I use the mouseMotionListener's MouseMoved method for that, but i want the label to be there only if the mouse stays still for a couple of seconds above the city.
I dont know if its because I been working on this code for days now, but i cant seem to think of a solution for this using the MouseMoved method, i tried using timers but that didnt work for me (mayb i just did it wrong cause my brain is burned out)
so is there a mouse listener for the mouse staying still? or do you have any recommendations?
here is more or less what I got
public void mouseMoved(MouseEvent evt) {
int x = evt.getX();
int y = evt.getY();
boolean aboveCity = false;
mouseover.setBounds(x+20, y-10, 200, 20); //mouseover is a JLabel
for (int i=0;i<cityCounter;i++){
if (city[i].containsPoint(x,y){
name = city[i].getName();
population = city[i].getPopulation();
aboveCity = true;
}
}
if(aboveCity){
mouseover.setText(name + ", " + population);
}
else{
mouseover.setText("");
}
}
Use a Java javax.swing.Timer. Each time the mouse moves, reset the timer. When the timer goes off, the mouse has been "still" for as long as your timer was set for.
We are using JFreeChart to make XY plots and we have a feature request to do a crosshair that moves along with the mouse and highlights the data point that most closely maps to the x-value of the mouse. You can see a similar example at Google Finance - http://www.google.com/finance?q=INDEXDJX:.DJI,INDEXSP:.INX,INDEXNASDAQ:.IXIC.
Those Google charts only highlight the current value (we want to do that and also show crosshairs), but they show the live mouse interaction we are looking for.
Anyone have any elegant suggestions?
Thanks.
I got this working using a mouse listener and the CrosshairOverlay class. After I get back from holiday travel, I will post my code. It ended up being not too difficult.
Sorry, I forgot about this!
First, you want to calculate the x, y values for where you want your crosshair. For me, I wanted it to move along the points of our line, so I calculated the closest x value and used that data pair for x, y.
Then I call this method:
protected void setCrosshairLocation(double x, Double y) {
Crosshair domainCrosshair;
List domainCrosshairs = crosshairOverlay.getDomainCrosshairs();
if (domainCrosshairs.isEmpty()) {
domainCrosshair = new Crosshair();
domainCrosshair.setPaint(BlueStripeColors.LIGHT_GRAY_C0);
crosshairOverlay.addDomainCrosshair(domainCrosshair);
}
else {
// We only have one at a time
domainCrosshair = (Crosshair) domainCrosshairs.get(0);
}
domainCrosshair.setValue(x);
if (y != null) {
Crosshair rangeCrosshair;
List rangeCrosshairs = crosshairOverlay.getRangeCrosshairs();
if (rangeCrosshairs.isEmpty()) {
rangeCrosshair = new Crosshair();
rangeCrosshair.setPaint(BlueStripeColors.LIGHT_GRAY_C0);
crosshairOverlay.addRangeCrosshair(rangeCrosshair);
}
else {
// We only have one at a time
rangeCrosshair = (Crosshair) rangeCrosshairs.get(0);
}
rangeCrosshair.setValue(y);
}
}
Note that crosshairOverlay is an instance of CrosshairOverlay.
JFreeChart can't render a sub-section of a chart, so you'll want to do something that doesn't require repainting the chart. You could write your chart to a BufferedImage and store that in memory, then have a custom component which uses the buffered chart as the background image, and draws crosshairs and other popup windows over it.
There are methods in JFreeChart to get the data point for a given coordinate on a rendered chart. Don't recall what these are off the top of my head. Depending on your needs, you might consider rendering your own chart data, it's not as hard as you'd think.
The first thing that comes to my mind would be to write a custom Cursor and set it on your chart. It can have a reference to the chart and highlight the x value that's consistent with the Cursor's x/y location.
This worked for me. I set the
chartPanel.addChartMouseListener(new ChartMouseListener() {
public void chartMouseMoved(ChartMouseEvent event)
{
try
{
double[] values = getCrossHairValue(event);
plot.clearRangeMarkers();
plot.clearDomainMarkers();
Marker yMarker = new ValueMarker(values[1]);
yMarker.setPaint(Color.darkGray);
plot.addRangeMarker(yMarker);
Marker xMarker = new ValueMarker(values[0]);
xMarker.setPaint(Color.darkGray);
plot.addDomainMarker(xMarker);
chartPanel.repaint();
} catch (Exception e)
{
}
}
}