I'm writing an Snipping Tool application. I have connected another moniter to my laptop. Application must recognize which monitor it currently is running on and capture it's screen. Like when I run the application in the external monitor I need to capture the screen of it.
Code I used to do this needed to be provide the monitor number. But I want the application to recognize it.
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
gs[0].getDisplayMode();
Dimension mySize = new Dimension(width, height);
width = (int) mySize.getWidth();
height = (int) mySize.getHeight();
You can do this:
JFrame frame = new JFrame();
// ...
GraphicsDevice device = frame.getGraphicsConfiguration().getDevice();
Related
I am trying to capture screen in mac os ventura using java.awt.robort but it captures the desktop image every time but not the image of current application running. As shown in https://i.stack.imgur.com/KABpT.png. Also it does not captures the second screen attached to laptop.
Rectangle screenRect = new Rectangle(0, 0, 0, 0);
for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
screenRect = screenRect.union(gd.getDefaultConfiguration().getBounds());
}
BufferedImage screenFullImage = new Robot().createScreenCapture(screenRect);
/*
* final BufferedImage screenFullImage = new Robot() .createScreenCapture(new
* Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
*/
ImageIO.write(screenFullImage, AppConstants.JPG_FORMAT, new File(imageFilePath.toString()));
I'm trying to write a program that would bring the mouse cursor to certain coordinates, according to timings, regardless of the user. I wrote a simple code using Robot, but ran into a problem ... I have two monitors, and the cursor moves incorrectly depending on what monitor it is on at the moment, please tell me how to solve the problem.
The code below is what I was trying to create ...
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();
for(int i=0; i < graphicsDevices.length; i++)
{
System.out.println(graphicsDevices[i]);
}
try {
//Robot robot = new Robot(MouseInfo.getPointerInfo().getDevice());
Robot robot = new Robot();
while(true)
{
robot.mouseMove(-1640, -3);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(10000);
}
} catch (Exception e) {
e.printStackTrace();
}
You should work with the idea of get the resolution and then move from there. You are doing ABSOLUTE moves and they will work different in differents setups.
You should use this code
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
from there you could:
Robot robot = new Robot();
robot.mouseMove (width-10, height+3);
So you would move relatively to the specs of the monitor. I hope I have helped.
This question already has an answer here:
Swing: Obtain Image of JFrame
(1 answer)
Closed 7 years ago.
Heys guys, I've developped a code to take a screen shot of my whole screen, but I want it to take the screen shot of only the things inside my Jframe. Ill be using it to print the image later on by the way. And one of the main problem is, the mouse also comes inside the snapshot. I don't want the mouse or the two buttons at the bottom. I can just change visi of buttons but what should be done for mouse and inside Jframe only shot? Here is my code it takes screen shot of whole screen.
try{
Thread.sleep(1000);
Toolkit tk = Toolkit.getDefaultToolkit(); //Toolkit class returns the default toolkit
Dimension d = tk.getScreenSize();
//Dimension class object stores width & height of the toolkit screen
// toolkit.getScreenSize() determines the size of the screen
Rectangle rec = new Rectangle(0, 0, d.width, d.height);
//Creates a Rectangle with screen dimensions,
Robot ro = new Robot(); //to capture the screen image
BufferedImage img = ro.createScreenCapture(rec);
File f;
f = new File("myimage.jpg"); // File class is used to write the above generated buffered image to a file
ImageIO.write(img, "jpg", f);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
IMHO it is better to make an image of your component (a JFrame is also a Component):
BufferedImage img = new BufferedImage(yourComponent.getWidth(), yourComponent.getHeight(), BufferedImage.TYPE_INT_RGB);
yourComponent.paint(img.getGraphics());
File outputfile = new File("saved.png");
ImageIO.write(img, "png", outputfile);
How to change size in libgdx-android-desktop? I am confuse on window sizeing and not sure how to solve this problem.
So for desktop window i want 500x500 but with android i want full screen so i cant hard code it.
For some reason ANDROID_WIDTH is always equal to WINDOW_WIDTH.
int WINDOW_WIDTH = 500;
int WINDOW_WIDTH = 500;
public void create() {
if (Gdx.app.getType() == ApplicationType.Android) {
int ANDROID_WIDTH = Gdx.graphics.getWidth();
int ANDROID_HEIGHT = Gdx.graphics.getHeight();
camera = new OrthographicCamera(ANDROID_WIDTH, ANDROID_HEIGHT);
camera.translate(ANDROID_WIDTH/2, ANDROID_HEIGHT/2);
camera.update();
} else {
camera = new OrthographicCamera(WINDOW_WIDTH, WINDOW_HEIGHT);
camera.translate(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
camera.update();
}
Gdx.input.setInputProcessor(new GameInputProcessor());
}
You cannot change the size of the windows by changing the camera. They are two separate concepts.
You set the size on desktop application in your main method through config lwjpg .
Android application is full screen anyway.
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "Title";
cfg.useGL20 = true;
cfg.height = 640;
cfg.width = 360;
new LwjglApplication(new MyGame(), cfg);
You can give the Window size on Startup:
new LwjglApplication(yourApplicationListener(), "Title", 500, 500, true/false (useGL2));
You can also Change it in your game using:
Gdx.graphics.setDisplayMode(500, 500, true/false (fullscreen));
You can surround this by an if Statement like stuntmania said:
if (Gdx.app.getType().equals(ApplicationType.Android)) {
Gdx.graphics.setDisplayMode(500, 500, false);
} else {
Gdx.graphics.setDisplayMode(Gdx.graphics.getWidth(),Gdx.graphics.getHeight(), true);
}
EDIT:
In LibGDX 1.8 the Method Gdx.graphics.setDisplayMode has been renamed to Gdx.graphics.setWindowedMode:
API Change: Graphics#setDisplayMode(int, int, boolean) has been renamed to
Graphics#setWindowedMode(int, int). This will NOT allow you to switch to fullscreen anymore,
use Graphics#setFullscreenMode() instead. If the window is in fullscreen mode, it will be
switched to windowed mode on the monitor the window was in fullscreen mode on.
(Source)
do this in your desktop project
cfg.width=500;
cfg.height=500;
and in your main class
int ANDROID_WIDTH = Gdx.graphics.getWidth();
int ANDROID_HEIGHT = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, ANDROID_WIDTH, ANDROID_HEIGHT);
camera.update();
I am trying to write a NXT Robot simulator with lejOS where the robot can avoid obstacles, but I want the obstacles to be generated in the code. The code below allows for the creation of the Circle.jpg, but the code crashes if it tries to use the image it creates. The code works in Intellij IDEA, but not eclipse with a previously generated image. I have tried the following with no results:
Used .png instead of .jpg
Used and image generated by a previous run, meaning it already existed.
Changed type from opaque to translucent, etc.
I am wondering what I am doing that makes the image crash my code when generating the image on the fly?
Update: Added command used to invoke new jpg, and a picture of the error.
public static void obstacleFactory()
{
int width = 30;
int height = 30;
GraphicsEnvironment environment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device =
environment.getDefaultScreenDevice();
GraphicsConfiguration config = device.getDefaultConfiguration();
BufferedImage bufferedImage = config.createCompatibleImage(width, height,
Transparency.TRANSLUCENT);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setColor(Color.yellow);
g2d.fillOval(0, 0, width, height);
g2d.dispose();
RenderedImage rendImage = bufferedImage;
try {
File file = new File("src/sprites/Circle.jpg");
ImageIO.write(rendImage, "jpg", file);
} catch (IOException e) {}
}
And the command use to invoke the jpg is
NxtContext.useObstacle("sprites/Circle.jpg", 250, 475);
This is what eclipse says:
I have no problems with your code in eclipse, except for the ellipse colors.
What do you mean saying "the code crashes"? Do you gent an exception? What's the informaion in it? Could it be that you just don't have a "src/sprites" path?