i've got some problem with my Mouse Cursor. I set it inside my MouseMotion Event of a JPanel with this.setCursor(), but it dosen't changed.
After getting out ouf the Window for example on my Desktop and go back inside , the cursor will be changed by any motion.
The Code of the mouse Event of the JPanel is this:
public void mouseMoved(MouseEvent e) {
this.requestFocusInWindow();
this.requestFocus();
this.cL.doMouseMoved(e);
}
The Code of the Method doMouseMoved is this:
public void doMouseMoved(MouseEvent e) {
this.lastMouseEvent = e;
this.sList.setCurrentElements(e.getPoint());
this.setMovedCursor(e);
}
An finally this is the code of the method setMovedCursor:
public void setMovedCursor(MouseEvent e) {
java.awt.Cursor cu = new java.awt.Cursor(java.awt.Cursor.SW_RESIZE_CURSOR);
view.setCursor(cu);
}
(I know that this isn't the best way)
I hope everyone can help me.
Sorry for any mistakes, it's my first post at stackoverflow.
if (sList.getCurrentShapeType() == "rec") {
Not sure if it will fix your problem but don't use "==" to compare objects.
Instead you should be using the equals(...) method.
if ("rec".equals(sList.getCurrentShapeType()) {
Note: I changed the order of the test so you don't have to worry about the getCurrentShapeType() method returning a null value.
else if (k.getBorderByPoint(e.getPoint()) == 4)
Also, I don't know what your getBorderByPoint() method does but why are you comparing it to an integer value. We have no idea what "4" means. Don't use "magic numbers. Instead create variables like: CURSOR_NORTH. Or better yet why not just return the cursor from that method so you don't have to check the value twice.
Related
I'm pretty new in programming, but i need to program the game Minesweeper.
basically the game is running, but there are some parts that wont work at all.
The first problem are the flags. My Minesweeper is able to set flag by right clicking on a field, but everytime i do a left-click on a field with a flag, the field will override the flag.
Field after a right click
Flagged field after a left click
Is there any way to set the flag "absolute" or final or something?
Here is the code:
#Override
public void mouseClicked(MouseEvent arg0) {
if (SwingUtilities.isRightMouseButton(arg0)) {
setFlag();
} else if (SwingUtilities.isLeftMouseButton(arg0)) {
checkMine();
}
public void setFlag() {
button.setEnabled(false);
button.setText(flag);
button.setBackground(Color.LIGHT_GRAY);
}
public void checkMine() {
button.setEnabled(false);
display();
check = false;
if (value == 0)
log.emptyCells();
if (value == -1)
log.fail();
if (value == -3)
log.lucky();
}
I hope that someone here can help me.
tanks :)
When you are using methods with button, you are never specifying which button you are disabling. I recommend adding a JButton parameter to your methods so you can change them.
Also, the check boolean is never initialized as a boolean, unless it's an instance variable you are using, in which case you need to edit your post to include your instance variables.
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 !
Hey all, I'm still relatively new to Java, and looking for a sanity check.
I've been studying this Java port of Cocos2D and noticed that the CCLayer class has built-in hooks to the Android native touch events. That's great, but what I'd really like is for objects like CCSprite to directly respond to touch events without having to listen for those events in the layer and iterate through all the children to find which ones happen to intersect the event's x/y coordinates. So I figured that this would be the perfect chance to test my understanding of how to set up some event handlers and make a subclass of CCSprite that actually listens for touches without needing to go through CCLayer to know about it. Furthermore, I wanted to be able to assign different behaviors to different CCSprite instances on an ad-hoc basis without explicitly subclassing further, much like Android Buttons don't need to be subclassed just to give them a handler for their touch events.
This is what I came up with on a first pass:
// My touch interface for all touchable CCNode objects.
package com.scriptocalypse.cocos2d;
public interface ITouchable {
boolean onCCTouchesBegan();
boolean onCCTouchesEnded();
boolean onCCTouchesMoved();
}
And now the class that uses the ITouchable interface for its callbacks...
public class CCTouchSprite extends CCSprite implements CCTouchDelegateProtocol {
protected ITouchable mTouchable;
public void setTouchable(ITouchable pTouchable){
mTouchable = pTouchable;
boolean enable = mTouchable != null;
this.setIsTouchEnabled(enable);
}
public void setIsTouchable(boolean pEnabled){
// code to enable and disable touches snipped...
}
/////
// And now implementing the CCTouchDelegateProtocol...
/////
public boolean ccTouchesBegan(MotionEvent event) {
Log.d("hi there", "touch me");
if(mTouchable != null){
mTouchable.onCCTouchesBegan();
}
return CCTouchDispatcher.kEventHandled; // TODO Auto-generated method stub
}
public boolean ccTouchesMoved(MotionEvent event) {
if(mTouchable != null){
mTouchable.onCCTouchesMoved();
}
return CCTouchDispatcher.kEventIgnored; // TODO Auto-generated method stub
}
public boolean ccTouchesEnded(MotionEvent event) {
Log.d("hi there", "not touch me");
if(mTouchable != null){
mTouchable.onCCTouchesEnded();
}
return CCTouchDispatcher.kEventIgnored; // TODO Auto-generated method stub
}
}
And finally, instantiate the class and implement the interface...
final CCTouchSprite sprite = new CCTouchSprite(tex);
sprite.setIsTouchEnabled(true);
sprite.setPosition(CGPoint.ccp(160,240));
sprite.setTouchable(new ITouchable(){
#Override
public boolean onCCTouchesBegan() {
Log.d("SWEET SUCCESS", "I got a touch through my interface!");
return true;
}
#Override
public boolean onCCTouchesEnded() {
Log.d("SWEET SUCCESS", "You stopped touching my interface!");
sprite.runAction(CCRotateBy.action(1, 360));
return false;
}
#Override
public boolean onCCTouchesMoved(){
Log.d("SWEET SUCCESS", "You moved the touch");
return false;
}
});
So all of this works. The subclass does successfully register with the Cocos2D touch dispatcher, which successfully calls those ccTouches functions and pass them MotionEvents, which in turn call my Interface functions if the interface has been instantiated.
Is this the "proper" way to do it (Define "it" as you see fit, ranging from using Interfaces to create event handlers to working with Cocos2D, to writing Java at all)? Are there drawbacks to this that I'm not aware of? Is this somehow worse for performance than iterating through all the CCNode objects that are children of CCLayer? If so, how can that possibly be?
I think you have got the basics for setting up a listener right. There are some things I would change though.
First, the setter setIsTouchable. It's weird. You need a listener object to pass touch events to right? So what is this setter going to do when you pass it true (as your example does)? You snipped the code, but setting a boolean field to true does not seem right here as it would put the sprite object in an inconsistent internal state. I would just drop that setter. The getter can just evaluate whether mTouchable is assigned or null.
Second, why limit yourself to one listener? Change mTouchable to mTouchables, being a list of ITouchables. Then change setTouchable to addTouchable and possibly add removeTouchable and clearTouchables methods. This way you can add multiple listeners for different behaviors having to respond to the same events. This is how most other event systems work as well. You then just change isTouchable to check whether the list is empty or not.
scriptoclypse... I really am not completely understanding your question, but you have not had any response and yes interfaces and events are very similar. At this level I can only respond in C#.
I have this constructor;
public UmlDiagramEntity(ReportElement reportElement, int pageIndex, Controller controller) {
super(reportElement.getX1(), reportElement.getY1(), reportElement.getX2(), reportElement.getY2());
setLayout(null);
this.pageIndex = pageIndex;
this.controller = controller;
reportElements = reportElement.getInternalReportElements();
components = new ArrayList<AbstractEntity>();
changedComponentIndex = -1;
PageListener p = new PageListener();
this.addMouseMotionListener(p);
this.addMouseListener(p);
setPage();
}
And I have an update method in the same class;
#Override
public void update(ReportElement reportElement) {
if (changedComponentIndex == -1) {
super.update(reportElement);
} else {
reportElements = reportElement.getInternalReportElements();
if (components.size() == reportElements.size()) {
if (!isCommitted) {
if (reportElement.getType() == ReportElementType.UmlRelation) {
if (checkInvolvementAndSet(changedComponentIndex)) {
anchorEntity(changedComponentIndex);
} else {
resistChanges(changedComponentIndex);
}
return;
}
}
..................goes on
When I follow the flow from the debugger, I see that when update is called, somewhere in the method, the program goes into the constructor and executes it all over again (super, pageIndex, etc.). Why does it go to the constructor :D I didn't tell it to go there.
I can make a deeper analysis and see where it goes to the constructor if you want. By the way, changedComponentIndex is a static variable.
I would find it far more probable that you are seeing it construct two different objects. You'd have to provide more information like a stack trace; here you haven't even shown the constructor being invoked!
The behaviour you describe is pretty much impossible. Either your code is different from what you've shown or you're not debugging the code you think you're debugging. Without complete code that we can run, that's all we can say.
Are you sure that update is not called indirectly from within the constructor, which would result in a breakpoint in update getting triggered.
Try setting a breakpoint at the start of the constructor and at the end, then one in update. When you hit the first constructor breakpoint, hit 'continue' and see which breakpoint gets triggered next.
Is this multi-threaded? Is it possible that the constructor for a different instance created on another thread is being called?
Is there an elegantish way in Swing to find out if there are any tooltips currently being displayed in my frame?
I'm using custom tooltips, so it would be very easy to set a flag in my createToolTip() method, but I can't see a way to find out when the tooltip is gone.
ToolTipManager has a nice flag for this, tipShowing, but of course it's private and they don't seem to offer a way to get to it. hideWindow() doesn't call out to the tooltip component (that I can tell), so I don't see a way there.
Anyone have any good ideas?
Update: I went with reflection. You can see the code here:
private boolean isToolTipVisible() {
// Going to do some nasty reflection to get at this private field. Don't try this at home!
ToolTipManager ttManager = ToolTipManager.sharedInstance();
try {
Field f = ttManager.getClass().getDeclaredField("tipShowing");
f.setAccessible(true);
boolean tipShowing = f.getBoolean(ttManager);
return tipShowing;
} catch (Exception e) {
// We'll keep silent about this for now, but obviously we don't want to hit this
// e.printStackTrace();
return false;
}
}
It appears that the isEnabled() property of the hideTipAction is directly tied to the tipShowing boolean. You could try this:
public boolean isTooltipShowing(JComponent component) {
AbstractAction hideTipAction = (AbstractAction) component.getActionMap().get("hideTip");
return hideTipAction.isEnabled();
}
You probably want to do some sanity checking for nulls, etc. But this should get you pretty close.
EDIT, to your responses:
Short of some ugly reflection code, I don't think you have much choice. You cannot subclass ToolTipManager because of the package private constructor, and the showTipWindow() and hideTipWindow() are also package private, so the Adapter pattern is out as well.
It looks like that is going to require looping over all of the components to see if they have a tooltip. I'm looking for a global value. It may be that a loop is doable, but it seems inefficient.
That's too bad. After an internal discussion, "ugly reflection" was what we came up with as well, but I was hoping someone out there had a better idea.
Since you already have your own createToolTip(), maybe you can try something like this :)
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.addAncestorListener( new AncestorListener() {
public void ancestorAdded( AncestorEvent event ) {
System.out.println( "I'm Visible!..." );
}
public void ancestorRemoved( AncestorEvent event ) {
System.out.println( "...now I'm not." );
}
public void ancestorMoved( AncestorEvent event ) {
// ignore
}
} );
return tip;
}