Why is setCursor() not working? - java

I am testing a new aspect of java, by attempting to make a custom cursor for my game, but it seems as I have run into a problem where in my code setCursor(); is bringing up a compiler error, for the reason that it is not seen as a proper piece of code. I was following tutorials and different guides, which all led to the same problem and I have found no answer for my query.
Toolkit toolKit = Toolkit.getDefaultToolkit();
Image img = toolKit.getImage(getClass().getResource("/res/cursor.png"));
Point point = new Point(0, 0);
Cursor cursor = toolKit.createCustomCursor(img, point, "Cursor");
setCursor(cursor);
Hope someone might be able to assist me, thanks in advance.
Notes:
I am running the latest JRE as of now (1.8).
The compiler error being recieved is as follows:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved
compilation problem:
The method setCursor(Cursor) is undefined for the type Main

The problem was solved thanks to MadProgrammer's last comment:
Then, you need to call setCursor with the instance of JPanel - A runnable example would make it easier.
The problem I had encountered was that I had used a JFrame as my container, instead of a JPanel. To fix my previous code I changed my main container to a JPanel, which in return allowed me to use: JPanelName.setCursor();
Edit: With further testing, I also found that a JFrame can still be used the same way as previously mentioned. The problem with my code above was that I was calling for it as JFrame.setCursor();, which was a static call to a non-static method. This then gave me the impression that I should only use setCursor(); (As seen in my query above). Hope this helps anyone who might have some misunderstandings on the setCursor(); method. Thanks again to MadProgrammer for solving the problem.

Related

Eclipse -> Java -> GUI Builder -> Can't get my UI to show properly in design mode

I am new to Java and Eclipse.
Somehow, my ui is not showing up the right way. Instead of seeing my frame, with the x to close at the top right, etc, and all the controls, I see a frame with no border.
See here: http://s7.postimg.org/miprf5ul7/Noooooooooooo.png
Here's my code: https://www.dropbox.com/s/74mltgsxcs9vjnf/PDF%20Monster.zip
I've spent countless hours working on this.
Please help me restore my UI, I'm desperate.
Thank you,
Whenever you can't view your ui the right way look at the code to see if there's red blocks in the right side. This indicates errors that may cause this.
In my case I had deleted a semicolon by accident somewhere...
If you fix the errors, it should be fine.
Also, you can simply try to comment out the errors to see if you can see the ui.

Autoscroll code isn't working: cannot find symbol

I'm trying to use the standard auto-scroll code I've been seeing copypasta'd everywhere:
DefaultCaret caret = (DefaultCaret)textarea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
It's placed in the same place I've seen it placed in other codes, right after the creation of the textarea it's associated with.
However, when I compile the code, it gives me the error "cannot find symbol", and points at all instances of DefaultCaret, much like if I had not imported the proper thing into my code.
I have imported ALL of javax.swing, after doing some researching it seems like the code works fine for everyone else.
Seems like I'm missing something simple, but I have no clue what it could be.
Am I supposed to define it earlier in the code?
The DefaultCaret class is not in the javax.swing package. It is in the javax.swing.text package.
Reference: http://docs.oracle.com/javase/7/docs/api/javax/swing/text/DefaultCaret.html
If you're still having problems, please post a Minimal Complete Example that demonstrates the problem. The code snippet you provided is likely not enough for others to help you should your problem persist.

Java JPanel movement (keylistener) not working

I've been trying to let an image move up and down in Jpanel for quite
a long while, but it never seems to work.
If I put "y = y - 1" in the view, it makes the image move up, so I
guess the problem is with the keylistener, right?
Could anyone help me with this please? To be honest... I think it will
be a very stupid mistake but I can't find how to get it to work.
Extra information: I am using BlueJ to from it, with 4 classes in the
basic program for my question. I realize that a lot of people on the
internet said to use key bindings and not KeyListener but I don't
understand key bindings as for now...
--- Following the advice of Hovercraft Full Of Eels, I will try to use Keybindings. ---
So Keybindings has 2 'commands' to work: input and action.
Copied this code and changed it for my program:
protected void addBindings() {
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = getInputMap(condition); // What does this do?
KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_UP, Event.KEY_PRESS);
inputMap.put(key, move(0, 2));
The move function:
protected void beweeg (int dx, int dy)
{
view.x = view.x + dx;
view.y = view.y + dy;
}
I have to say... the code system on this forum is incredibly frustrating.
When trying this I get: "cannot find symbol - method getInputMap(int)"
Did I need to declare it somewhere? I don't see anything about this.
You ask:
When trying this I get: "cannot find symbol - method getInputMap(int)" Did I need to declare it somewhere?
This method must be called on an object that derives from JComponent or one of its children such as a JPanel. This error message means that you're making this call inside of a class that does not extend JComponent or one of its children such as a JPanel. A possible solution is to call the method on the object that you want to attach the bindings to, on your JPanel object.
Speaking of links, here are some of mine: link, link, link, link, link, link,...
Keywords: driving rotating turning car
So after a long time of searching and the help of several forums I got the following result, to 'drive' cars and making 'smooth' turns.
Sorry for not using the code provided before, it helped me to understand a lot though. Thanks
I can't figure out how to use the coding on this forum, can anyone insert the following code please? thanks
http://hostcode.sourceforge.net/view/1728
Maybe someone will be able to use it putting it online :)
Kind regards
Niels

sikuli 1.0.2 documentation and ScreenRegion

I'm writing program in Java, using Sikuli. I'm using sikuli-standalone***.jar, and ScreenRegion class inside of it. The problem is in documentation: http://doc.sikuli.org/
I can't find class ScreenRegion there, but I can find Screen, and Region, which have better methods (concluding by their names) than ScreenRegion. But unfortunately I don't have these classes in my library.
Acctually this does not result in compiler error:
Screen screen = screenRegion1.getScreen();
But this does:
Screen screen2 = new Screen();
Error is: "Cannot instantiate type Screen"
ScreenRegions seems OK, but because there is no documentation I don't know how to move/create new ScreenRegion that is moved 50px to the right on global screen.
What should I do?
What I'm doing wrong?
Is there Java documentation for Sikuli?
OK it seems that I haven't seen it first time. This is what I should you (I think):
http://doc.sikuli.org/javadoc/index.html
http://doc.sikuli.org/faq/030-java-dev.html
But these classes does not work with classes in sikuli-standalone, and I'm now totally confused about what should I use. But that's topic for another question.
And this seems to be documentation for sikuli-standalone:
http://code.google.com/p/sikuli-api/w/list

JLayeredPane error when the layer is an Integer

I have a Canvas subclass object that I'm trying to add, along with some other Canvas subclasses, to a JLayeredPane. In the documentation for JLayeredPane, the layer is given as an Integer, e.g.
layeredPane.add(child, new Integer(0));
However, when I use an Integer for the layer, I get some kind of runtime error. (I can't really tell what this error is because my IDE keeps complaining about the lack source for the Swing libraries.) Oddly, when I use the following form:
layeredPane.add(child, 0);
the line actually executes error-free. I'm very new to Java and still haven't figured out how autoboxing works other than to form the opinion that it doesn't work very well. I'm not sure if the bare 0 would be autoboxed in this case.
I wish I could add more detail about this error, but I'm not even seeing an exception. I will work on trimming my code down to a simple example, but I was hoping someone with experience with JLayeredPane has seen this before.
Unfortunately I can't add comment so I will leave reply.
If you work with IDE then you can use design form to create your GUI easily, just drag-&-drop thing....
I have worked with JLayeredPane and if you add component to it you can use add method manually like layeredPane.add(Component, javax.swing.JLayeredPane.PALETTE_LAYER); or layeredPane.add(Component, javax.swing.JLayeredPane.DEFAULT_LAYER);
Use static constant Integer field and read the JLayeredPane API, or some examples of using it on oracle. Its a good feature to learn.
This error had something to do with the fact that I was using BorderLayout on the JLayeredPane. The error went away when I changed to another layout.

Categories