Unselect a DrawLine after its drawn - java

After I draw a DrawLine it stays selected. I would like them to be un-selected after its drawn.
How do I un-select a DrawLine after its drawn?

DrawLine.setEnableSelect(false) (WAG from examining the JavaDocs of an API I'd never heard of before right now).

If you have this method available then use this: DrawLine.setSelectNewLines or you can use setSelected(null) to un-select the newly drawn item. You also need to add DrawLineListener so you can listen to new line to use this function. Sample lines of code will be:
drawLine.addDrawLineListener(new DrawLineListener() {
#Override
public void lineSelected(ChangeEvent ce) {}
#Override
public void lineNew(ChangeEvent ce) {
drawLine.setSelected(null);
}
});

Related

Add listener jDateChooser

i haved create this metod for add listeners in my dataChooser.
public final void añadirEsccuhaDataChoser() {
jDateChooser1.getDateEditor().addPropertyChangeListener((PropertyChangeEvent e) -> {
if ("date".equals(e.getPropertyName())) {
listarviajes1();
}
});
this.add(jDateChooser1);
}
but the result who i have obtened after run the project is this.
datachooser moved
but the my original desing is this.
data chooser original position
the method i haved situate into the public vReservas().
situation method
What can I do to prevent the dataChooser from moving?
this is a solution.
solution
The problem is that I created a method, instead of adding the listener in the JdateChooser's internal code.
The code to add the listener is the same, just place it in the custom code option of the jDateChooser.
the ubication of listener

Getting character to jump in JavaFX

I have tried using a KeyListener to make the character rise which just simply didnt do anything. Is there a better way to do this? If not, does anyone know why the KeyListener isn't working? I would really appreciate some help. Also, I call this method in a Timeline so the bird does not just fall 1 pixel and will not rise by one either
Here is the code for the KeyListener (The bird falling part works fine by the way):
public static void birdJump() {
birdView.relocate(100, fall++);
if(fall > 201)
birdView.setRotate(50);
FlappyBird.root.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if (event.getCode().equals(32)) {
birdView.relocate(100, fall--);
if(fall > 199)
birdView.setRotate(-50);
}
}
});
}
Thanks!
Read the documentation: event.getCode() does not return an int (or a number of any kind), so event.getCode().equals(32) cannot possibly be true.
You want
if (event.getKeyCode() == KeyCode.SPACE) {
// ...
}
In addition to what James_D said, I think it's because you use the KeyEvent on a Layout that does not have the focus by default since it belongs to the Scene, to remedy it you have to give it the focus :
root.setFocusTransversale(true); /* Enable focus on the layout */
root.requestFocus(); /* give the focus to the layout */
I hope this will solve your problem !

Java mouseclick logger

Hi I want to build on java mouse click logger. I need to print the coordinates of mouse clicks in a file. Can you tell me how I can make this. Which APIs to use some examples or links. I need to get all the mouse clicks not only in one window.
you could implement the MouseListener interface and overwrite the mouseClicked function:
public class MyMouseListener implements MouseListener {
// [...]
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(e.getLocationOnScreen());
}
// [...]
}
Adapt that example to your needs. THe getLocationOnScreen Method returns a "Point" object which you can ask for x/y coordinates.

How do I add a MouseListener to a graphics object inside another graphics object?

I'm working on a GUI for a card game and am using the ACM's student graphics library for the sake of familiarity. I have written a program that draws my solitaire game to the screen, and am having trouble making it interactive.
Background:
There are a lot of classes here, and I'll do my best to describe them each.
Top level JFrame containing the application.
GCanvas (that holds all the graphics objects)
SolitaireGameControl (GCompound holding all the other GCompounds making up the solitaire game)
Array of PileViews, a pile of cards (GCompound consisting of an array of Cards)
Cards (GCompound consisting of rectangles and labels)
(GCompound: a collection of graphics objects treated as one object. (If car was a GCompound, it would have GOval[] wheels, GRect body and so when I add it to the canvas, it displays as one object))
A card as seen from the top-level class would look like a bit like this: jFrame.gCanvas.solitaireGameControl.pileViews[pile number].cardView
What I've been trying to do is add a MouseListener to every single card, so that when a card is clicked and a MouseEvent is fired, MouseEvent e.getSource() = the card that was clicked.
Here's how it looks now:
public SolitaireGameControl(SolitaireGame game) {
this.game = game; // Model of the game.
this.pileViews = PileView.getPileViews(game.drawPiles); // ArrayList of PileViews (the pile of cards)
for(PileView pv : pileViews) {
for(CardView cv : pv.cardViews) {
cv.addMouseListener(this); // add a mouseListener to the card
}
}
this.addMouseListener(this); // if I don't include this, nothing happens when I click anything. If I do include this, this whole object is the source.
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(e.getSource()); // should return the card I clicked.
}
When I run this program, the source of every event is SolitaireGameControl, granted I leave in the this.addMouseListener(this);. If I take out this statement, nothing is printed at all, leading me to believe that the mouseListeners I have added are only working one level deep. (The first GCompound on the canvas, not the GCompounds inside it.)
Therefore, my question is as follows: Is there a way to get a MouseListener for a GCompound inside of a GCompound inside of a GCompound, and have MouseEvent's getSource to correctly identify the card? If not, is there a way to restructure my program to make it work as intended? (I know I should really be using a better graphics library for starters.)
That would make sense. From my experience, if I put some components inside a top-level container, the container is the one that receives input events.
Have you tried an approach where you do something like:
/* This is the mouse listener for the top-level container. */
#Override
public void mouseClicked(MouseEvent e) {
for(PileView pv : pileViews) {
for(CardView cv : pv.cardViews) {
if(cv.getBounds().contains(e.getPoint())) {
cv.dispatchEvent(e);
}
}
}
}
... and then handle mouse clicks on a 'CardView' level normally.
When the top-level container receives a mouse event, it checks if the mouse interacted with a card based on the location of the event (if the card's area contains the point). If it did, it passes down the mouse event to the card's mouse listener.
I'm assuming that the elements near the beginning of 'pv.cardViews' are the cards that are more to the front.

Get GWT DialogBox absolute position - onLoad/onAttach/show do not helps

I am stuck in getting an absolute position of DialogBox. I know it is the common problem (and strange workaround) for PopupPanel (which is parent to DialogBox) to set it, but what if I want to get it, what is the exact moment when the box attached to DOM? Neither overriding show nor onAttach nor show does not help:
class MyDialog extends DialogBox {
public MyDialog(. . .) {
ComplexPanel vert = new VerticalPanel();
vert.add("Test");
vert.add(new Button("Close", new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
MyDialog.this.hide();
}
}));
setWidget(vert);
this.addAttachHandler(new AttachEvent.Handler() {
#Override
public void onAttachOrDetach(AttachEvent event) {
if (event.isAttached()) Log.debug("attach:"+MyDialog.this.getAbsoluteLeft() +";"+
MyDialog.this.getAbsoluteTop());
}
});
}
#Override
protected void onLoad() {
super.onLoad();
Log.debug("load:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
}
#Override
public void show() {
super.show();
Log.debug("show:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
}
}
So when I call new MyDialog().show();, all this lines do log 0;0, however dialog is positioned in center of a page. But what I want is the sum of the chain of offsetParent positions. (And they are 0 in these moments even in JavaScript, if use JSNI to check this)
Again, setPopupPositionAndShow allows to set position but not get it :(
Finally, I've got this to work:
#Override
public void setPopupPosition(int left, int top) {
super.setPopupPosition(left, top);
if (this.isAttached()) Log.debug("setPos:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
}
It gets the proper position and I hope it is the right way to do it and setPopupPosition is called every time. You will even call it manually when using setPopupPositionAndShow.
I think it will be wise to let this question stay at SO "for future generations".
Upd. If you plan to call center(...) or some similar method of your dialog, be aware that setPopupPosition will be called twice or more times (may be first time with 0, 0), even if you'll check if it isAttached(). Add some additional check to ensure that positions are correct in current call.

Categories