In java press key without frame (or frame in background) - background

I created a program that controls the mouse using the keyboard. I implement KeyListener to a class I called Keyboard, and then I create a JFrame called frame that adds this keyboard class. Here's part of the code to explain what I'm doing:
class Keyboard implements KeyListener
{
//Overrides key methods
}
class MainClass
{
public static void main(String [] args)
{
JFrame frame = new JFrame();
frame.setSize(width, height);
frame.addKeyListener(new Keyboard());
frame.setVisible(true);
}
}
My problem is I want the program to run when the frame is not selected (or even better, without the frame). For example, when I select another window of another program (a browser, Word, etc) the mouse won't move when I use the keyboard. I have to select my program window to control the mouse.
How can I achieve this?
Thanks.

Related

How to receive property change events in an AbstractInformationControl

I am working on an editor plugin and now implementing support for code hovers.
I have extended AbstractInformationControl (and implemented IInformationControlExtension2) to create a new control for showing the hover info.
It works almost fine, but I am unable to receive property change events in my information control. What I try is like this, but the event handler does not fire:
public class MyHoverInfoControl extends AbstractInformationControl implements IInformationControlExtension2 {
public MyHoverInfoControl(Shell parentShell, String string) {
super(parentShell, string);
create();
Activator.getDefault().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent event) {
// TODO Auto-generated method stub
}
});
}
The control itself contains a StyledText. I have also tried to add a KeyListener to this StyledText to see if key events are received, and it only seemed to work if I click into the control (it is not enough to hover over a text to show the control). But property change event does not seem to be received even after the click.
Is it possible to receive property change events for an AbstractInformationControl subclass?

Inherit UserControl and hooking up to basic property events

I'm making a custom TextBox for UWP to simplify Win2D outlined text solution, for that I created a UserControl that contains only a canvas on which I'll draw the text.
Of course I need some properties, like text, outline thickness and color, etc...
I also need some properties that are already exposed by the inherited UserControl like Foreground, FontSize, FontFamily...
So far so good, it seems like I won't need to implement each one of those common properties.
The problem is that I can't find a way to hook up an event when one of those properties changes, as I have to call the Canvas.Invalidate() method to redraw it when the format changes.
Looks like I have to hide all those properties and create new Dependency Properties to call Canvas.Invalidate().
There is no way to do it faster?
Nevermind, the answer was behind the corner.
In the constructor, you can call
RegisterPropertyChangedCallback(DependencyProperty dp, DependencyPropertyChangedCallback callback);
For example:
public OutlinedText()
{
InitializeComponent();
RegisterPropertyChangedCallback(FontFamilyProperty, OnPropertyChanged);
RegisterPropertyChangedCallback(FontSizeProperty, OnPropertyChanged);
}
private void OnPropertyChanged(DependencyObject sender, DependencyProperty dp)
{
OutlinedText instance = sender as OutlinedText;
if (instance != null)
{
//Caching the value into CanvasTextFormat for faster drawn execution
if (dp == FontFamilyProperty)
instance.TextFormat.FontFamily = instance.FontFamily.Source;
else if (dp == FontSizeProperty)
instance.TextFormat.FontSize = (Single)instance.FontSize;
instance.needsResourceRecreation = true;
instance.canvas.Invalidate();
}
}

Change Button Style in Visual Studio Express 2013

The buttons in the application i've created, although set with custom images, are still Windows 7 themed (round borders, color gradience, etc). Is there a way to change them to a more windows classic or window 8 look? templates or anything that can be downloaded?
Basically you are limited to the properties provided by the windows forms button class that its shape depends on the environment (operating system version) you are running.
However, you still can customize your button shape by implementing your own CustomButton class that has the Button class as its base. In that class you have to override the painting methods (and possibly other methods) and using the passed paint event args graphics object to draw your prefered shape.
public class ExampleCustomButton : Button
{
protected override void OnPaintBackground(PaintEventArgs pevent)
{
using (Pen p = new Pen(Color.Yellow))
{
pevent.Graphics.DrawEllipse(p,
Left, Top, Width, Height); //for example
}
}
protected override void OnPaint(PaintEventArgs pevent)
{
using (Pen p = new Pen(Color.Yellow))
{
pevent.Graphics.DrawEllipse(p,
Left, Top, Width, Height); //for example
}
}
}
Please, do not forget to dispose IDisposable Graphics objects.
Good Luck

Changing states when entities intersect

I am making a maze that the player has to navigate with the mouse, but they lose when the hit a wall. I want to make sure the player has the mouse in the right place to start, so I am trying to switch to the game state once the mouse intersects with a square indicator on screen.
In the tutorial I was learning from, they did this:
public static class Point extends AbstractEntity {
public Point(double x, double y, double width, double height) {
super(x, y, width, height);
}
#Override
public void draw() {
glBegin(GL_POINTS);
glVertex2d(x, y);
}
#Override
public void update(int delta) {
// Blank
}
}
Point.setLocation(Mouse.getX(), 480 - Mouse.getY() - 1);
if(Maze1.intersects(Point)){
System.out.println("You would have lost");
}
I tried to do this:
case MAZE:
if(Maze1.intersects (Point) ){
state = State.GAMEOVER;
}
break;
I get the error, "Point cannot be resolved to a variable". Please help.
It looks like when you call Maze1.intersects you are passing the actual Point class rather than an instance of that class. This is why you get the "Point cannot be resolved to a variable" error. You need to create an instance of Point, call setLocation on that instance and then pass that into Maze1.intersects.
For your example to work I would either remove the argument on Maze1.intersects and have it access the Point class directly or change Point to it's no longer static, declare an instance of Point and pass that instance into Maze1.intersect.
Declaring the Point class as static does not mean you can pass the class into method, etc, like a regular variable as it's simply a way of sharing data and behaviour. Also, keep in mind that by declaring Point as static you are saying that there will only ever be one Point.
I would maybe consider dropping the static keyword and creating an instance of Point classed "startingPoint". You can then pass this into Maze1.intersects. Then if you need to determine if the player intersects with other "points" in the maze, then you can create other instances to represent those points.

Drawing to canvas from another class in Cinder

Is there a way I can draw using libcinder without having to put all my code on the draw() method of the main class. I'm working on a big app and it's not convenient in any way to have everything stuffed in one file.
This is an example of what the idea would be:
class MyApp : public AppBasic {
public:
void setup ();
void update ();
void draw ();
private:
vector<MyObject> myObjects;
};
MyApp::draw () {
for (int i = 0; i < myObjects.size(); ++i) {
myObjects[i].render ();
}
}
CINDER_APP_BASIC (MyApp, RendererGL)
/* ------------------ */
class MyObject {
public:
void render ();
};
void MyObject::render () {
Rectf rect (0, 0, 20, 20);
gl::drawSolidRoundedRect(rect, 15.0);
}
Yes, there is a way. Several ways, actually.
Create a class with at least the following methods: void setup(), void update() and void draw(). In the main application you can then create instances of this class, store them in a member variable or std::vector. Then simply call the methods from the main application's setup, update and draw methods respectively.
Use Cinder's event system to connect to its update and draw events. See the ListenerBasic sample: https://github.com/cinder/Cinder/tree/master/samples/ListenerBasic . For a list of all available events, see: https://github.com/cinder/Cinder/blob/master/include/cinder/app/Window.h .
Write or use a scene graph system which handles drawing all your objects. It will call each of your object's draw() method in the correct order, can put objects on top of other objects, can detect which object is under the cursor, etc. A well-known scene graph is http://www.openscenegraph.org/ , but it is not compatible with Cinder. A very basic scene graph can be found in one of my samples: https://github.com/paulhoux/Cinder-Samples/tree/master/SimpleSceneGraph
-Paul