I am trying to make a mouse recorder, I cant seem to get a mouse listener to work with a console, is this possable and how would I go about it Thanks.
Unless you wrote your own console that fired mouse events, I dont' think you're going to be able to do it. What widget are you going to register your mouselistener against otherwise? The console isn't a swing component, therefore, no swing events.
You can do this by using global hooks.
In order to use them you'll need to include some natives or try the same using JNI (see: wikipedia).
Two examples:
http://kra.lc/blog/2011/07/java-global-system-hook/(works well - I would advice to re-indent the c++ content and if you know how to do, merge it into 1-2 files - your eyes will thank you)
http://www.jotschi.de/Technik/2008/01/06/java-global-keyboard-hook-jni.html(never tried, but looks more simple)
Edit:
Example for some playback functionalities:
import java.awt.AWTException;
import java.awt.DisplayMode;
import java.awt.MouseInfo;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.util.Random;
// class instructions
try {
PointerInfo pntInfo = MouseInfo.getPointerInfo();
DisplayMode dispMode = pntInfo.getDevice().getDisplayMode();
int newX = new Random().nextInt( dispMode.getWidth() );
int newY = new Random().nextInt( dispMode.getHeight() );
new Robot( pntInfo.getDevice() ).mouseMove( newX, newY );
} catch ( AWTException exception ) { }
Sorry for my late answer ;)
Related
apologies for my basic question but I am a kinda java newbie and I would love to write a code that will do a keyboard shortcut CTRL + A.
I have imported:
import org.sikuli.hotkey.Keys;
import org.sikuli.script.Key;
and wrote a variety of codes similar to this:
Keys.CTRL + KeyEvent.VK_A / Keys.A / ...
Unfortunately, I did not manage to make it work..
I have two issues:
how to make it work as a keyboard shortcut
how to add code of an "A"
I have read about the modifiers and tried to find a solution here but without a bit of luck.
I was thinking about adding sth like KeyPress and KeyRelease but idk how to make it work.
Any suggestions?
Thank you in advance!
for anyone who is trying to do the same - I think I have found a solution:
import java.awt.*;
import java.awt.event.KeyEvent;
try {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_A);
r.keyRelease(KeyEvent.VK_A);
r.keyRelease(KeyEvent.VK_CONTROL);
} catch (AWTException ex) {
// Exception
}
I am trying to create a program that whenever I run it, it takes a screenshot of Google Chrome. So far, I have figured out how to have it take a screenshot and write it to a file. However, this is just a screenshot of the entire screen. I'd like to take a screenshot of chrome, even if it is just running in the background. Also, I'd like to be able to re-size Google Chrome and run the program to get images with the same dimensions as Google Chrome.
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.AWTException;
import java.awt.Rectangle;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
public class ProgramDriver {
public static void main(String[] args) {
Rectangle windowScreen = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
try {
BufferedImage capture = new Robot().createScreenCapture(windowScreen);
ImageIO.write(capture, "png", new File("screenshot.png"));
} catch (AWTException | IOException e) {
e.printStackTrace();
}
}
}
One way is to use image processing/recognition and recognize a distinctive symbol in your browser.
The other way is to go to the operating system and get the attributes where the window is etc
The third approach is to go with your mouse and check the coordinates/and size "ah now its there" and enter the coordinates into your program, "ah now its there..."
In effect you need the upper left coordinates and the size - any way you can get it
You can put up another question depending on which way you want
I am in need of help. I'm not a very proficient coder, so i'll ask directly.
I am looking to create a script that would be initiated through the pressing of a keyboard button. It would then wait randomly between 10 to 30 seconds and then take a screenshot. The screenshot would be then added to the print queue and printed with a printer.
I am aware of two scripts which do two steps of this, but unsure how to connect them to a seamless thing ( along with a delay mechanism, which is important, also would be good to have a 5 second period between possible initiations of the script so you can't make 50 prints in the queue when you mash the button and wreck the system )
( for information it would be taking screenshots of the output of an interactive system working with generative stuff and video feed, im not yet sure on the software i will use for that maybe vvvv )
Take screenshot
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class screen2image
{
public static void main(String[] args) throws Exception
{
Robot robot = new Robot();
BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));
}
}
// print image
FileInputStream fin = new FileInputStream("YOurImageFileName.PNG");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
job.print(doc, pras);
Many thanks in advance for any help with this. It would be great for my project.
So I've been doing a lot of searching today, trying to figure out how to play videos inside of a JFrame. The reason I want this is because, I'm making a game, and want the option to add movie clips, like in most every good game, such as GW2, Medal of Honor, etc.
So, in my searches, I found JMF, but was completely unable to use it. It is sort of frustrating, but whatever. So, my question is this: Is there a way to play videos WITHOUT installing any other jar's, exe's, etc? For example, run a simple code such as new JFrame(); sort of quick and easy? or is that not possible, but there is a way to do it long and complex?
I've also been looking at other stack overflow stuff, and none of it really fits what i want... If worst comes to worst, i'll just use Xuggler, but i'd rather not.
Also, based on this answer, I plan on making a game engine in the future, so this could be added to it, for additional value.
Thank you in advance :)
PLEASE NOTE: I am not looking for references to JMF, or anything else like that. I'm looking for things such as built in methods/classes to call, or long work arounds, that work pretty good and would be implementable in numerous environments.
EDIT: I was thinking of using JEditorPane, and embedding the video with html, but.... that hasnt been working for me... here's what i've tried there:
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
// jep.setContentType("text/html");
jep.setText("<html><video id=\"sampleMovie\" src=\"C:\\users\\austin\\desktop\\test.mp4\" controls></video></html>");
JScrollPane scrollPane = new JScrollPane(jep);
JFrame f = new JFrame("Test HTML");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setPreferredSize(new Dimension(800,600));
f.setVisible(true);
But this doesnt seem to be working... Please help!
JMF seems to have been around since 1997. No wonder then that, as you said, most of the Java-based players out there rely on it.
So I decided to take a trip back in time using Google's search options and found a result from Feb 2001: a very simple GNU-licensed MPEG-1 player implemented from the ground up using just Java and C. How about that? The following capture is from the website:
I know you emphasised you needed a solution with as little external code as possible. In this case, no additional libraries seem to be required but you have to do some compiling. Plus you're limited to MPEG-1. So, not exactly what you were looking for but perhaps worth a look.
Hope it helps!
if you have a File name "file":
import java.io.*;
import java.net.*;
import javax.swing.*;
//....
//....
//....
try{
mediaURL = file.toURI().toURL();
}
catch(Exception e){
}
if(mediaURL != null){
JFrame mediaTest = new JFrame();
MediaPanel m = new MediaPanel(mediaURL);
mediatTest.add(m);
mediaTest.setVisible(true);
}
Edited:
import javax.media.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
public class MediaPanel extend JPanel{
public MediaPanel( URL mediaURL){
setLayout(new BorderLayout());
try{
Player mp = Manager.createRealizedPlayer( mediaURL);
Component video = mp.getVisualComponent();
Component controls = mp.getControlPaneComponent();
if(video != null){
add(video, BorderLayout.CENTER);
}
if(controls != null){
add(controls, BorderLayout.SOUTH);
}
mediaPlayer.start();
}
catch(Exception e){
}
}
}
This is how I plan to make it:
There will be a small JFrame with start and stop buttons to start and stop recording. Clicking start will start a thread that will create a java.awt.Robot that will take repeated screen shots.
Here is where my problem starts. Robot does take a screenshot but
1. The position of the cursor will not be recorded
2. I dont know where the image will be stored. I intend to store them in a folder whose name will be determined using java.util.Date.
3. I dont understand BufferedImage class one bit.
I have finished making JFrame and implemented the thread. I have package that I got from the internet that converts images to avi movies.
Please help me solve the above mentioned three problems.
Edited:
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
public void captureScreen(String fileName) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "png", new File(fileName));
}
if you could please explain t he above mentioned code with respect to my problem.
The solution for getting the cursor in the screenshot is to:
figure out the position of the cursor.
java.awt.MouseInfo.getPointerInfo().getLocation()
and place a rendered image of the cursor on top of the screenshot.
Take a look at this: http://docs.oracle.com/javase/tutorial/2d/images/index.html