Results 1 to 7 of 7
  1. #1
    Ericyue is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Exclamation print screen help

    hi there,

    i am new to java and now i am doing a project that nid to print screen (example : 1seconds 1 pic). Below is my code :

    import java.awt.image.BufferedImage;
    import java.awt.Rectangle;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.Robot;
    import java.io.File;
    import javax.imageio.ImageIO;

    class ScreenRecorder {
    public static void main(String args[]) {
    try {
    Boolean a = false;
    Toolkit tool = Toolkit.getDefaultToolkit();
    Dimension d = tool.getScreenSize();
    Rectangle rect = new Rectangle(1440,900);
    Robot robot = new Robot();
    Thread.sleep(2000);

    while (a != true)
    {
    File f = new File("screenshot.jpg");
    BufferedImage img = robot.createScreenCapture(rect);
    ImageIO.write(img,"jpeg",f);
    tool.beep();
    }
    } catch(Exception e){
    e.printStackTrace();
    }
    }
    }


    i can set it to keep print screen but only with a single image and it will keep overwrite it.

    can anyone teach me how to set it so thaat it can print screen in 1s or maybe 0.1s or maybe 0.001s ..

    thanks in advance for replying

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: print screen help

    So you want one pic every second?

    You could use a Timer, TimerTask and Timer.scheduleAtFixedRate(TimerTask, 100, 1000) to repeat the task every second, starting after 0.1 seconds.

    Then you could use Timer.cancel() when you want to stop.

    Free links for you:
    Timer (Java 2 Platform SE v1.4.2)
    TimerTask (Java 2 Platform SE v1.4.2)
    Last edited by ozzyman; 03-23-2012 at 06:02 AM.

  3. #3
    Ericyue is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: print screen help

    thanks ozzyman ..

  4. #4
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: print screen help

    Java Code:
    while (a != true)
    why don't you use:
    Java Code:
    while (true)
    or
    Java Code:
    boolean a = true;
    
    while (a)
    //...
    just my opinion... maybe you have a reason why you use it...

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: print screen help

    If you use a while loop you have two problems:
    1) Your thread doesn't respond until the while loop has finished and
    2) Capturing the images every second would clog up your main thread anyway

    So your program will become less responsive or unresponsive.

    However, when you create a Timer a separate background thread is created just for the Timer's tasks.
    Every TimerTask scheduled to that Timer shares that thread though.

    Read it here:
    Timer (Java Platform SE 6)

  6. #6
    Ericyue is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: print screen help

    thanks for all your reply

    i have modified my program as below


    import java.awt.Toolkit;
    import java.util.Timer;
    import java.util.TimerTask;

    import java.awt.image.BufferedImage;
    import java.awt.Rectangle;
    import java.awt.Dimension;
    import java.awt.Robot;
    import java.io.File;
    import javax.imageio.ImageIO;


    public class Main {
    Toolkit toolkit;

    Timer timer;

    public Main() {
    toolkit = Toolkit.getDefaultToolkit();
    timer = new Timer();
    timer.scheduleAtFixedRate(new RemindTask(), 0,
    1*100);
    }

    class RemindTask extends TimerTask {
    public void run() {

    try {
    Boolean a = true;
    Toolkit tool = Toolkit.getDefaultToolkit();

    Dimension d = tool.getScreenSize();
    Rectangle rect = new Rectangle(1440,900);
    Robot robot = new Robot();
    Thread.sleep(2000);

    while (a )
    {
    File f = new File("screenshot.jpg");
    BufferedImage img = robot.createScreenCapture(rect);
    ImageIO.write(img,"jpeg",f);

    }
    } catch(Exception e){
    e.printStackTrace();
    }




    }
    }

    public static void main(String args[]) {
    new Main();
    }
    }
    this is my new code and the things i wan to ask is

    timer.scheduleAtFixedRate(new RemindTask(), 0,
    1*100);

    the 1*100 is how long ???

    if i wan 0.01s or even 0.001s is there any possible to do that ?

    and sometimes print screen the half of the image will be black. what it will like that ???
    thanks in advance for the reply .:)
    Last edited by Ericyue; 03-27-2012 at 09:33 AM.

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: print screen help

    The time is in milliseconds. In future you can refer to the java docs if you're not sure. In fact I posted the link to it already in my previous post.

    In case you don't know, 1000 milliseconds = 1 seconds, so 100 would be 0.1 seconds.

Similar Threads

  1. Print a receipt to screen
    By africanhacker in forum New To Java
    Replies: 8
    Last Post: 04-28-2011, 07:51 AM
  2. choosing random array to print to screen
    By debugdoug in forum New To Java
    Replies: 4
    Last Post: 04-05-2011, 04:45 PM
  3. Replies: 11
    Last Post: 03-09-2011, 09:18 AM
  4. print on the client screen
    By a_maged in forum Networking
    Replies: 0
    Last Post: 12-17-2007, 05:10 PM
  5. Replies: 1
    Last Post: 08-07-2007, 06:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •